exit if unable to load template at init
This commit is contained in:
parent
731f1bc5b5
commit
0d48db8b94
@ -99,11 +99,6 @@ func extractLoginForm(r *http.Request) LoginForm {
|
|||||||
|
|
||||||
func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
||||||
loginForm := templates.GetLoginForm()
|
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)
|
lf := extractLoginForm(r)
|
||||||
if lf.HasErrors() {
|
if lf.HasErrors() {
|
||||||
@ -119,6 +114,7 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ok := lf.ValidCredentials(); !ok {
|
if ok := lf.ValidCredentials(); !ok {
|
||||||
|
log.Warn().Str("username", lf.Username.Value).Msg("bad credentials")
|
||||||
lf.Error = ErrInvalidCredentials
|
lf.Error = ErrInvalidCredentials
|
||||||
|
|
||||||
buf := bytes.NewBufferString("")
|
buf := bytes.NewBufferString("")
|
||||||
@ -144,12 +140,6 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore)
|
|||||||
http.SetCookie(w, cookie)
|
http.SetCookie(w, cookie)
|
||||||
|
|
||||||
loginSuccess := templates.GetLoginSuccess()
|
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())
|
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) {
|
if s.IsLogged(r) {
|
||||||
loginSuccess := templates.GetLoginSuccess()
|
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())
|
fmt.Fprint(w, loginSuccess.Tree.Root.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package templates
|
|||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"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)
|
tmpl, err := template.New("loginForm").Funcs(funcMap).Parse(form)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("unable to parse login form")
|
log.Err(err).Msg("unable to parse login form")
|
||||||
return nil
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return tmpl
|
return tmpl
|
||||||
})
|
})
|
||||||
@ -36,7 +37,7 @@ var GetLoginSuccess = sync.OnceValue[*template.Template](func() *template.Templa
|
|||||||
tmpl, err := template.New("loginSuccess").Parse(success)
|
tmpl, err := template.New("loginSuccess").Parse(success)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("unable to parse login success")
|
log.Err(err).Msg("unable to parse login success")
|
||||||
return nil
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return tmpl
|
return tmpl
|
||||||
})
|
})
|
||||||
|
|||||||
@ -166,11 +166,6 @@ func extractBookForm(r *http.Request) BookForm {
|
|||||||
|
|
||||||
func postUploadFile(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
func postUploadFile(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
||||||
uploadForm := templates.GetUploadForm()
|
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) {
|
if !s.IsLogged(r) {
|
||||||
buf := bytes.NewBufferString("")
|
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) {
|
func getUploadFile(w http.ResponseWriter, _ *http.Request) {
|
||||||
uploadForm := templates.GetUploadForm()
|
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("")
|
buf := bytes.NewBufferString("")
|
||||||
if err := uploadForm.Execute(buf, &BookForm{}); err != nil {
|
if err := uploadForm.Execute(buf, &BookForm{}); err != nil {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -40,7 +41,7 @@ var GetUploadForm = sync.OnceValue[*template.Template](func() *template.Template
|
|||||||
tmpl, err := template.New("uploadForm").Funcs(funcMap).Parse(form)
|
tmpl, err := template.New("uploadForm").Funcs(funcMap).Parse(form)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("unable to parse upload form")
|
log.Err(err).Msg("unable to parse upload form")
|
||||||
return nil
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return tmpl
|
return tmpl
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user