|
|
@ -3,25 +3,68 @@ package httputils
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Handler = func(http.ResponseWriter, *http.Request)
|
|
|
|
type Handler = func(http.ResponseWriter, *http.Request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const templateFolder = "templates"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var templatePaths, templateModTimes, _ = getTemplates()
|
|
|
|
|
|
|
|
var templates *template.Template = template.Must(template.ParseFiles(templatePaths...))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getTemplates() ([]string, map[string]time.Time, error) {
|
|
|
|
|
|
|
|
var modTimes map[string]time.Time = make(map[string]time.Time)
|
|
|
|
|
|
|
|
err := filepath.Walk(templateFolder, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() && strings.HasSuffix(path, ".html") {
|
|
|
|
|
|
|
|
modTimes[path] = info.ModTime()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
paths := make([]string, len(modTimes))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for path := range modTimes {
|
|
|
|
|
|
|
|
paths[i] = path
|
|
|
|
|
|
|
|
i++
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths, modTimes, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func reloadTemplateIfModified(path string) {
|
|
|
|
|
|
|
|
fileInfo, _ := os.Stat(path)
|
|
|
|
|
|
|
|
modTime := fileInfo.ModTime()
|
|
|
|
|
|
|
|
if modTime.After(templateModTimes[path]) {
|
|
|
|
|
|
|
|
fmt.Printf("Reloading template %s...\n", path)
|
|
|
|
|
|
|
|
templates.ParseFiles(path)
|
|
|
|
|
|
|
|
templateModTimes[path] = modTime
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func reloadTemplatesIfModified() {
|
|
|
|
|
|
|
|
for _, path := range templatePaths {
|
|
|
|
|
|
|
|
reloadTemplateIfModified(path)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const reloadTemplates = true
|
|
|
|
|
|
|
|
|
|
|
|
func GenerateHandler(
|
|
|
|
func GenerateHandler(
|
|
|
|
file string,
|
|
|
|
file string,
|
|
|
|
handler func(http.ResponseWriter, *http.Request) bool,
|
|
|
|
handler func(http.ResponseWriter, *http.Request) bool,
|
|
|
|
data func(http.ResponseWriter, *http.Request) any,
|
|
|
|
data func(http.ResponseWriter, *http.Request) any,
|
|
|
|
methods []string,
|
|
|
|
methods []string,
|
|
|
|
) Handler {
|
|
|
|
) Handler {
|
|
|
|
var tmpl *template.Template
|
|
|
|
|
|
|
|
//if file != "" {
|
|
|
|
|
|
|
|
// tmpl = template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file)))
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Remove in production, enables live template reloading
|
|
|
|
// All templates must be reloaded in case of dependencies
|
|
|
|
if file != "" {
|
|
|
|
if reloadTemplates {
|
|
|
|
tmpl = template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file)))
|
|
|
|
reloadTemplatesIfModified()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, method := range methods {
|
|
|
|
for _, method := range methods {
|
|
|
|
if method == r.Method {
|
|
|
|
if method == r.Method {
|
|
|
@ -32,9 +75,9 @@ func GenerateHandler(
|
|
|
|
return
|
|
|
|
return
|
|
|
|
ok:
|
|
|
|
ok:
|
|
|
|
renderTemplate := handler(w, r)
|
|
|
|
renderTemplate := handler(w, r)
|
|
|
|
if renderTemplate && tmpl != nil {
|
|
|
|
if renderTemplate && file != "" {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
tmpl.Execute(w, data(w, r))
|
|
|
|
templates.ExecuteTemplate(w, file, data(w, r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|