exit if unable to load template at init

This commit is contained in:
rmanach 2025-01-03 09:32:27 +01:00
parent 731f1bc5b5
commit 0d48db8b94
4 changed files with 6 additions and 30 deletions

View File

@ -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
}

View File

@ -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
})

View File

@ -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 {

View File

@ -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
})