generated from ElnuDev/go-project
dict: MVP
This commit is contained in:
parent
f8ccf62570
commit
01204ffc81
9 changed files with 191 additions and 4 deletions
|
@ -10,7 +10,7 @@ type Handler = func(http.ResponseWriter, *http.Request)
|
|||
|
||||
func GenerateHandler(
|
||||
file string,
|
||||
handler func(http.ResponseWriter, *http.Request),
|
||||
handler func(http.ResponseWriter, *http.Request) bool,
|
||||
data func(http.ResponseWriter, *http.Request) any,
|
||||
methods []string,
|
||||
) Handler {
|
||||
|
@ -19,6 +19,7 @@ func GenerateHandler(
|
|||
tmpl = template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file)))
|
||||
}
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl = template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file)))
|
||||
for _, method := range methods {
|
||||
if method == r.Method {
|
||||
goto ok
|
||||
|
@ -27,8 +28,8 @@ func GenerateHandler(
|
|||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
ok:
|
||||
handler(w, r)
|
||||
if tmpl != nil {
|
||||
renderTemplate := handler(w, r)
|
||||
if renderTemplate && tmpl != nil {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
tmpl.Execute(w, data(w, r))
|
||||
}
|
||||
|
|
Reference in a new issue