// Copyright (C) 2022 Marius Schellenberger package server import ( "embed" "errors" "html/template" "io/fs" ) //go:embed templates/* var templates embed.FS type TemplateStore struct { m map[string]*template.Template fatal bool } func NewTemplateStore() *TemplateStore { return &TemplateStore{m: make(map[string]*template.Template)} } func (ts *TemplateStore) Parse(fs fs.FS, name string, files ...string) { temp, err := template.ParseFS(fs, files...) if err != nil { ts.fatal = true return } ts.m[name] = temp } func (ts *TemplateStore) Get(name string) *template.Template { return ts.m[name] } func (s *Server) loadTemplates() (err error) { s.ts = NewTemplateStore() tfs, err := fs.Sub(templates, "templates") if err != nil { return err } // pages s.ts.Parse(tfs, "notFoundHandler", "index.html", "menu.html", "notFound.html") s.ts.Parse(tfs, "forbiddenHandler", "index.html", "menu.html", "forbidden.html") s.ts.Parse(tfs, "loginHandler", "index.html", "menu.html", "login.html") s.ts.Parse(tfs, "loginTotpHandler", "index.html", "menu.html", "loginTotp.html") s.ts.Parse(tfs, "consentHandler", "index.html", "menu.html", "consent.html") // users s.ts.Parse(tfs, "usersHandler", "index.html", "menu.html", "users.html") s.ts.Parse(tfs, "userNewHandler", "index.html", "menu.html", "userNew.html") s.ts.Parse(tfs, "userEditHandler", "index.html", "menu.html", "userEdit.html") s.ts.Parse(tfs, "userDelHandler", "index.html", "menu.html", "userDel.html") s.ts.Parse(tfs, "userSessionsHandler", "index.html", "menu.html", "userSessions.html") // totp s.ts.Parse(tfs, "userTotpHandler", "index.html", "menu.html", "userTotp.html") if s.ts.fatal { err = errors.New("parsing templates failed") } return }