Embed templates and assets code was moved to a separate package.
This commit is contained in:
parent
a335ae4803
commit
6484b72342
46
web/web.go
Normal file
46
web/web.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user