1
0

Template is being embed directly and all code for directory was removed.

This commit is contained in:
Alexander Andreev 2022-05-24 19:42:47 +04:00
parent 74ae6f4213
commit f2d5b5e8c5
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -4,43 +4,36 @@ import (
"dwelling-radio/pkg/logging" "dwelling-radio/pkg/logging"
"embed" "embed"
"html/template" "html/template"
"io/fs"
"net/http" "net/http"
"github.com/Joker/jade" "github.com/Joker/jade"
) )
var compiledTemplates map[string]*template.Template
//go:embed assets //go:embed assets
var assetsDir embed.FS var assetsDir embed.FS
func Assets() http.FileSystem { func Assets() http.FileSystem {
return http.FS(assetsDir) f, _ := fs.Sub(assetsDir, "assets")
return http.FS(f)
} }
func AssetsGetFile(path string) ([]byte, error) { func AssetsGetFile(path string) ([]byte, error) {
return assetsDir.ReadFile(path) return assetsDir.ReadFile("assets/" + path)
} }
//go:embed templates //go:embed templates/index.jade
var templatesDir embed.FS var indexJadeTpl []byte
var IndexTemplate *template.Template
func Template(name string) *template.Template {
return compiledTemplates[name]
}
func CompileTemplates(lErr *logging.Logger) { func CompileTemplates(lErr *logging.Logger) {
compiledTemplates = make(map[string]*template.Template) indexParsedTpl, err := jade.Parse("index", indexJadeTpl)
indexStr, err := jade.ParseFileFromFileSystem("index.jade", http.FS(templatesDir))
if err != nil { if err != nil {
lErr.Fatalln(err) lErr.Fatalln(err)
} }
indexTpl, err := template.New("index").Parse(indexStr) IndexTemplate, err = template.New("index").Parse(indexParsedTpl)
if err != nil { if err != nil {
lErr.Fatalln(err) lErr.Fatalln(err)
} }
compiledTemplates["index"] = indexTpl
} }