move rendering funcs to handlers.go; use the render function on upload response
This commit is contained in:
+15
-5
@@ -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:
|
||||
|
||||
@@ -12,19 +12,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
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 main() {
|
||||
config, err := loadConfig()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user