Better API handling

This commit is contained in:
Elnu 2023-07-21 17:13:07 -07:00
parent 61c1ce5502
commit f558d7f0c1
4 changed files with 62 additions and 52 deletions

View file

@ -56,7 +56,7 @@ func reloadTemplatesIfModified() {
const reloadTemplates = true
func GenerateHandler(
file string,
file interface{}, // either string or func() string
handler func(http.ResponseWriter, *http.Request) bool,
data func(http.ResponseWriter, *http.Request) any,
methods []string,
@ -76,8 +76,15 @@ func GenerateHandler(
ok:
renderTemplate := handler(w, r)
if renderTemplate && file != "" {
var file_path string
switch file.(type) {
case string:
file_path = file.(string)
case func(http.ResponseWriter, *http.Request) string:
file_path = file.(func(http.ResponseWriter, *http.Request) string)(w, r)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
templates.ExecuteTemplate(w, file, data(w, r))
templates.ExecuteTemplate(w, file_path, data(w, r))
}
}
}