1
0

Embed templates and assets code was moved to a separate package.

This commit is contained in:
Alexander Andreev 2022-05-24 18:30:14 +04:00
parent a335ae4803
commit 6484b72342
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

46
web/web.go Normal file
View File

@ -0,0 +1,46 @@
package web
import (
"dwelling-radio/pkg/logging"
"embed"
"html/template"
"net/http"
"github.com/Joker/jade"
)
var compiledTemplates map[string]*template.Template
//go:embed assets
var assetsDir embed.FS
func Assets() http.FileSystem {
return http.FS(assetsDir)
}
func AssetsGetFile(path string) ([]byte, error) {
return assetsDir.ReadFile(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)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", http.FS(templatesDir))
if err != nil {
lErr.Fatalln(err)
}
indexTpl, err := template.New("index").Parse(indexStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["index"] = indexTpl
}