move rendering funcs to handlers.go; use the render function on upload response

This commit is contained in:
2026-11-13 21:17:43 -05:00
parent 80e0efe3c4
commit 422a51d4d0
2 changed files with 15 additions and 18 deletions
+15 -5
View File
@@ -47,7 +47,20 @@ type UploadResponse struct {
FileExtension string `json:"extension"` // useful for things like sharex to append the extension
}
func uploadHandler(db *gorm.DB, cfg Config, templates *template.Template) http.HandlerFunc {
func render(pages *template.Template, w http.ResponseWriter, name string, data any) {
w.Header().Set("Conten-Type", "text/html; charset=utf-8")
if err := pages.ExecuteTemplate(w, name, data); err != nil {
log.Printf("render %s: %v", name, err)
}
}
func staticPage(pages *template.Template, name string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
render(pages, w, name, nil)
}
}
func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
@@ -252,10 +265,7 @@ func uploadHandler(db *gorm.DB, cfg Config, templates *template.Template) http.H
switch returnType {
// html & maybe xml? cant think of other return types
case "html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := templates.ExecuteTemplate(w, "upload.html", response); err != nil {
log.Println("render upload page:", err)
}
render(pages, w, "upload.html", response)
case "redirect":
http.Redirect(w, r, "/"+id, http.StatusSeeOther)
default: