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
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
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 {
|
switch returnType {
|
||||||
// html & maybe xml? cant think of other return types
|
// html & maybe xml? cant think of other return types
|
||||||
case "html":
|
case "html":
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
render(pages, w, "upload.html", response)
|
||||||
if err := templates.ExecuteTemplate(w, "upload.html", response); err != nil {
|
|
||||||
log.Println("render upload page:", err)
|
|
||||||
}
|
|
||||||
case "redirect":
|
case "redirect":
|
||||||
http.Redirect(w, r, "/"+id, http.StatusSeeOther)
|
http.Redirect(w, r, "/"+id, http.StatusSeeOther)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -12,19 +12,6 @@ import (
|
|||||||
"strings"
|
"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() {
|
func main() {
|
||||||
config, err := loadConfig()
|
config, err := loadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user