simplify static page rendering; fix qt discovery
This commit is contained in:
@@ -12,6 +12,19 @@ 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 {
|
||||||
@@ -57,7 +70,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch strings.ToLower(filepath.Ext(qt.Name())) {
|
switch strings.ToLower(filepath.Ext(qt.Name())) {
|
||||||
case ".jpeg", "jpg", ".jxl", ".png", ".webp", "avif", ".gif":
|
case ".jpeg", "jpg", ".jxl", ".png", ".webp", ".avif", ".gif":
|
||||||
qts = append(qts, "/qts/"+url.PathEscape(qt.Name()))
|
qts = append(qts, "/qts/"+url.PathEscape(qt.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,25 +113,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
render(pages, w, "home.html", data)
|
||||||
if err := pages.ExecuteTemplate(w, "home.html", data); err != nil {
|
|
||||||
log.Println("render home page:", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
mux.HandleFunc("GET /tos", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
|
|
||||||
if err := pages.ExecuteTemplate(w, "tos.html", nil); err != nil {
|
|
||||||
log.Println("render terms page:", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
mux.HandleFunc("GET /privacy", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
|
|
||||||
if err := pages.ExecuteTemplate(w, "privacy.html", nil); err != nil {
|
|
||||||
log.Println("render terms page:", err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
mux.HandleFunc("GET /tos", staticPage(pages, "tos.html"))
|
||||||
|
mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html"))
|
||||||
mux.HandleFunc("GET /{id}", contentHandler(db, config))
|
mux.HandleFunc("GET /{id}", contentHandler(db, config))
|
||||||
mux.HandleFunc("POST /upload", uploadHandler(db, config, pages))
|
mux.HandleFunc("POST /upload", uploadHandler(db, config, pages))
|
||||||
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
|
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
|
||||||
|
|||||||
Reference in New Issue
Block a user