initial commit
This commit is contained in:
commit
f48fa210bb
49 changed files with 4058 additions and 0 deletions
60
pkg/server/templates.go
Normal file
60
pkg/server/templates.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// 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, "loginHandler", "index.html", "menu.html", "login.html")
|
||||
s.ts.Parse(tfs, "loginTotpHandler", "index.html", "menu.html", "loginTotp.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")
|
||||
// totp
|
||||
s.ts.Parse(tfs, "userTotpHandler", "index.html", "menu.html", "userTotp.html")
|
||||
|
||||
if s.ts.fatal {
|
||||
err = errors.New("parsing templates failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue