directory/assets/embed.go

46 lines
749 B
Go
Raw Normal View History

2024-06-01 23:00:14 +00:00
package assets
import (
"embed"
"net/http"
"github.com/CloudyKit/jet/v6"
)
//go:embed templates/*.jet
var templatesFS embed.FS
//go:embed static/*
var staticFS embed.FS
var Views *jet.Set
func init() {
loader := jet.NewInMemLoader()
// Load templates from embed.FS
templateFiles, err := templatesFS.ReadDir("templates")
if err != nil {
panic(err)
}
for _, tmpl := range templateFiles {
if tmpl.IsDir() {
continue
}
tmplPath := "templates/" + tmpl.Name()
tmplContent, err := templatesFS.ReadFile(tmplPath)
if err != nil {
panic(err)
}
name := tmpl.Name()
loader.Set(name, string(tmplContent))
}
Views = jet.NewSet(loader)
}
func StaticHandler() http.Handler {
return http.FileServer(http.FS(staticFS))
}