1
0
dwelling-upload/web/web.go

80 lines
1.6 KiB
Go
Raw Normal View History

package web
import (
"dwelling-upload/pkg/logging"
"embed"
"html/template"
"io/fs"
"net/http"
"github.com/Joker/jade"
)
var compiledTemplates map[string]*template.Template
//go:embed assets
var assetsDir embed.FS
func Assets() http.FileSystem {
f, _ := fs.Sub(assetsDir, "assets")
return http.FS(f)
}
func AssetsGetFile(path string) ([]byte, error) {
return assetsDir.ReadFile("assets/" + path)
}
//go:embed templates
var templatesDir embed.FS
func Template(name string) *template.Template {
return compiledTemplates[name]
}
func CompileTemplates(lErr *logging.Logger) {
compiledTemplates = make(map[string]*template.Template)
t, _ := fs.Sub(templatesDir, "templates")
templatesFS := http.FS(t)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["index"], err = template.New("index").Parse(indexStr)
if err != nil {
lErr.Fatalln(err)
}
uploadedStr, err := jade.ParseFileFromFileSystem("uploaded.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["uploaded"], err = template.New("uploaded").Parse(uploadedStr)
if err != nil {
lErr.Fatalln(err)
}
notfoundStr, err := jade.ParseFileFromFileSystem("404.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["404"], err = template.New("404").Parse(notfoundStr)
if err != nil {
lErr.Fatalln(err)
}
nospaceStr, err := jade.ParseFileFromFileSystem("nospace.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["nospace"], err = template.New("nospace").Parse(nospaceStr)
if err != nil {
lErr.Fatalln(err)
}
}