gowiki/pkg/server/templates.go
2021-02-18 22:52:28 +01:00

67 lines
2.6 KiB
Go

// Copyright (C) 2021 Marius Schellenberger
package server
import (
"embed"
"errors"
"git.giftfish.de/ston1th/gowiki/pkg/log"
"html/template"
"io/fs"
)
//go:embed templates/*
var templates embed.FS
func (s *HTTPServer) loadTemplates() {
var fatal bool
s.templ = make(map[string]*template.Template)
s.res = make(map[string][]byte)
tfs, err := fs.Sub(templates, "templates")
if err != nil {
log.Fatal("parse: ", err)
}
parse := func(html ...string) (temp *template.Template) {
var err error
temp, err = template.ParseFS(tfs, html...)
if err != nil {
log.Println("parse:", err)
fatal = true
}
return
}
// pages
s.templ["notFoundHandler"] = parse("index.html", "menu.html", "notFound.html")
s.templ["forbiddenHandler"] = parse("index.html", "menu.html", "forbidden.html")
s.templ["loginHandler"] = parse("index.html", "menu.html", "login.html")
s.templ["loginTotpHandler"] = parse("index.html", "menu.html", "loginTotp.html")
s.templ["searchHandler"] = parse("index.html", "menu.html", "search.html")
s.templ["pageHandler"] = parse("index.html", "menu.html", "page.html")
s.templ["pageNewHandler"] = parse("index.html", "menu.html", "pageNew.html")
s.templ["pageEditHandler"] = parse("index.html", "menu.html", "pageEdit.html")
s.templ["pageViewHandler"] = parse("index.html", "menu.html", "pageView.html")
s.templ["pageMDHandler"] = parse("index.html", "menu.html", "pageMD.html")
s.templ["pageShareHandler"] = parse("index.html", "menu.html", "pageShare.html")
s.templ["pageBlacklistHandler"] = parse("index.html", "menu.html", "pageBlacklist.html")
s.templ["pageDelHandler"] = parse("index.html", "menu.html", "pageDel.html")
// sections
s.templ["sectionHandler"] = parse("index.html", "menu.html", "section.html")
s.templ["sectionsHandler"] = parse("index.html", "menu.html", "sections.html")
s.templ["sectionNewHandler"] = parse("index.html", "menu.html", "sectionNew.html")
s.templ["sectionEditHandler"] = parse("index.html", "menu.html", "sectionEdit.html")
s.templ["sectionDelHandler"] = parse("index.html", "menu.html", "sectionDel.html")
// users
s.templ["usersHandler"] = parse("index.html", "menu.html", "users.html")
s.templ["userNewHandler"] = parse("index.html", "menu.html", "userNew.html")
s.templ["userEditHandler"] = parse("index.html", "menu.html", "userEdit.html")
s.templ["userDelHandler"] = parse("index.html", "menu.html", "userDel.html")
// totp
s.templ["userTotpHandler"] = parse("index.html", "menu.html", "userTotp.html")
// stats
s.templ["statsHandler"] = parse("index.html", "menu.html", "stats.html")
if fatal {
log.Fatal("parse: ", errors.New("parsing templates failed"))
}
}