mirror of
https://github.com/mainnika/nikita-tokarch-uk.git
synced 2026-05-25 01:03:35 +00:00
Parse compiled-in go templates on app start
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
|||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/config"
|
"code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/config"
|
||||||
|
|
||||||
|
_ "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "nightly"
|
var Version = "nightly"
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package templates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"embed"
|
||||||
|
"html/template"
|
||||||
|
"io/fs"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Static go-templates source
|
||||||
|
var content embed.FS
|
||||||
|
|
||||||
|
// List of compiled go-templates
|
||||||
|
var Templates *template.Template
|
||||||
|
|
||||||
|
// Load embeded templates
|
||||||
|
func init() {
|
||||||
|
|
||||||
|
Templates = template.New("")
|
||||||
|
|
||||||
|
tmplNames, err := fs.Glob(content, "*.go.tmpl")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
for _, name := range tmplNames {
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
tmplContent, err := content.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
size, err := buf.ReadFrom(tmplContent)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
tmpl, err := Templates.New(name).Parse(buf.String())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("Found template: %s, size:%d", tmpl.Name(), size)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user