import gorilla/handlers

This commit is contained in:
kenneth
2023-12-04 09:38:57 +00:00
parent 30c5ed2452
commit 3e80490a2e
5 changed files with 57 additions and 9 deletions

View File

@@ -35,6 +35,42 @@ func (server *Server) renderLayout(w http.ResponseWriter, r *http.Request, data
})
tpl := template.Must(t.Clone())
// compress
// "github.com/tdewolff/minify/v2"
// "github.com/tdewolff/minify/v2/css"
// "github.com/tdewolff/minify/v2/html"
// "github.com/tdewolff/minify/v2/js"
// m := minify.New()
// m.AddFunc("text/css", css.Minify)
// m.AddFunc("text/html", html.Minify)
// m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
// pages := []string{
// tmpl,
// "base/header.html.tmpl",
// "base/footer.html.tmpl",
// }
// for _, page := range pages {
// b, err := fs.ReadFile(server.templateFS, page)
// if err != nil {
// logger.Logger.Errorf("fs read file: %s, %v", page, err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// mb, err := m.Bytes("text/html", b)
// if err != nil {
// logger.Logger.Errorf("minify bytes: %s, %v", page, err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// tpl, err = tpl.Parse(string(mb))
// if err != nil {
// logger.Logger.Errorf("template parse: %s, %v", page, err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
// }
tpl, err := tpl.ParseFS(server.templateFS, tmpl, "base/header.html.tmpl", "base/footer.html.tmpl")
if err != nil {
logger.Logger.Errorf("template parse: %s, %v", tmpl, err)
@@ -42,8 +78,7 @@ func (server *Server) renderLayout(w http.ResponseWriter, r *http.Request, data
return
}
err = tpl.Execute(w, data)
if err != nil {
if err := tpl.Execute(w, data); err != nil {
logger.Logger.Errorf("template execute: %s, %v", tmpl, err)
w.WriteHeader(http.StatusInternalServerError)
return

View File

@@ -14,6 +14,7 @@ import (
"time"
"github.com/gorilla/csrf"
hds "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/zhang2092/mediahls/internal/db"
@@ -70,13 +71,19 @@ func (server *Server) setupRouter() {
[]byte(securecookie.GenerateRandomKey(32)),
csrf.Secure(false),
csrf.HttpOnly(true),
csrf.FieldName("csrf_token"),
csrf.CookieName("authorize_csrf"),
)
router.Use(csrfMiddleware)
router.HandleFunc("/register", server.registerView).Methods(http.MethodGet)
router.HandleFunc("/register", server.register).Methods(http.MethodPost)
router.HandleFunc("/login", server.loginView).Methods(http.MethodGet)
router.HandleFunc("/login", server.login).Methods(http.MethodPost)
router.Handle("/register", hds.MethodHandler{
http.MethodGet: http.HandlerFunc(server.registerView),
http.MethodPost: http.HandlerFunc(server.register),
})
router.Handle("/login", hds.MethodHandler{
http.MethodGet: http.HandlerFunc(server.loginView),
http.MethodPost: http.HandlerFunc(server.login),
})
router.HandleFunc("/logout", server.logout).Methods(http.MethodGet)
router.HandleFunc("/", server.homeView).Methods(http.MethodGet)
@@ -106,7 +113,7 @@ func (server *Server) setupRouter() {
func (server *Server) Start(db *sql.DB) {
srv := &http.Server{
Addr: server.conf.ServerAddress,
Handler: server.router,
Handler: hds.CompressHandler(server.router),
}
go func() {