From 0d48db8b94b597cc2fe43741394cf74013d8b40d Mon Sep 17 00:00:00 2001 From: rmanach Date: Fri, 3 Jan 2025 09:32:27 +0100 Subject: [PATCH] exit if unable to load template at init --- handlers/login/handler.go | 18 +----------------- handlers/login/templates/templates.go | 5 +++-- handlers/upload/handler.go | 10 ---------- handlers/upload/templates/templates.go | 3 ++- 4 files changed, 6 insertions(+), 30 deletions(-) diff --git a/handlers/login/handler.go b/handlers/login/handler.go index ecbff85..afc2b68 100644 --- a/handlers/login/handler.go +++ b/handlers/login/handler.go @@ -99,11 +99,6 @@ func extractLoginForm(r *http.Request) LoginForm { func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) { loginForm := templates.GetLoginForm() - if loginForm == nil { - log.Error().Msg("unable to load login form") - http.Error(w, "unexpected error occurred", http.StatusInternalServerError) - return - } lf := extractLoginForm(r) if lf.HasErrors() { @@ -119,6 +114,7 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) } if ok := lf.ValidCredentials(); !ok { + log.Warn().Str("username", lf.Username.Value).Msg("bad credentials") lf.Error = ErrInvalidCredentials buf := bytes.NewBufferString("") @@ -144,12 +140,6 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) http.SetCookie(w, cookie) loginSuccess := templates.GetLoginSuccess() - if loginSuccess == nil { - log.Error().Msg("unable to load login success") - http.Error(w, "unexpected error occurred", http.StatusInternalServerError) - return - } - fmt.Fprint(w, loginSuccess.Tree.Root.String()) } @@ -163,12 +153,6 @@ func getLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) if s.IsLogged(r) { loginSuccess := templates.GetLoginSuccess() - if loginSuccess == nil { - log.Error().Msg("unable to load login success") - http.Error(w, "unexpected error occurred", http.StatusInternalServerError) - return - } - fmt.Fprint(w, loginSuccess.Tree.Root.String()) return } diff --git a/handlers/login/templates/templates.go b/handlers/login/templates/templates.go index aa64c1f..1cc06b9 100644 --- a/handlers/login/templates/templates.go +++ b/handlers/login/templates/templates.go @@ -3,6 +3,7 @@ package templates import ( _ "embed" "html/template" + "os" "sync" "github.com/rs/zerolog/log" @@ -27,7 +28,7 @@ var GetLoginForm = sync.OnceValue[*template.Template](func() *template.Template tmpl, err := template.New("loginForm").Funcs(funcMap).Parse(form) if err != nil { log.Err(err).Msg("unable to parse login form") - return nil + os.Exit(1) } return tmpl }) @@ -36,7 +37,7 @@ var GetLoginSuccess = sync.OnceValue[*template.Template](func() *template.Templa tmpl, err := template.New("loginSuccess").Parse(success) if err != nil { log.Err(err).Msg("unable to parse login success") - return nil + os.Exit(1) } return tmpl }) diff --git a/handlers/upload/handler.go b/handlers/upload/handler.go index 19899b2..c1232b8 100644 --- a/handlers/upload/handler.go +++ b/handlers/upload/handler.go @@ -166,11 +166,6 @@ func extractBookForm(r *http.Request) BookForm { func postUploadFile(w http.ResponseWriter, r *http.Request, s *services.SessionStore) { uploadForm := templates.GetUploadForm() - if uploadForm == nil { - log.Error().Msg("unable to load upload form") - http.Error(w, "unexpected error occurred", http.StatusInternalServerError) - return - } if !s.IsLogged(r) { buf := bytes.NewBufferString("") @@ -238,11 +233,6 @@ func postUploadFile(w http.ResponseWriter, r *http.Request, s *services.SessionS func getUploadFile(w http.ResponseWriter, _ *http.Request) { uploadForm := templates.GetUploadForm() - if uploadForm == nil { - log.Error().Msg("unable to load upload form") - http.Error(w, "unexpected error occurred", http.StatusInternalServerError) - return - } buf := bytes.NewBufferString("") if err := uploadForm.Execute(buf, &BookForm{}); err != nil { diff --git a/handlers/upload/templates/templates.go b/handlers/upload/templates/templates.go index 5d6e649..1490773 100644 --- a/handlers/upload/templates/templates.go +++ b/handlers/upload/templates/templates.go @@ -4,6 +4,7 @@ import ( _ "embed" "html/template" "mime/multipart" + "os" "strconv" "strings" "sync" @@ -40,7 +41,7 @@ var GetUploadForm = sync.OnceValue[*template.Template](func() *template.Template tmpl, err := template.New("uploadForm").Funcs(funcMap).Parse(form) if err != nil { log.Err(err).Msg("unable to parse upload form") - return nil + os.Exit(1) } return tmpl })