Add dev flag to indicate development runs

Used to reload the templates on each page view.
This commit is contained in:
Las Zenow 2020-12-03 12:58:37 +00:00
parent 1536d97d8d
commit fe36b1d23c
2 changed files with 46 additions and 3 deletions

View file

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io"
"net/http" "net/http"
"path" "path"
"strings" "strings"
@ -53,9 +54,13 @@ func GetStatus(h handler) Status {
return s return s
} }
type TemplateExecutor interface {
ExecuteTemplate(io.Writer, string, interface{}) error
}
type Template struct { type Template struct {
html *html_tmpl.Template html TemplateExecutor
opds *txt_tmpl.Template opds TemplateExecutor
} }
func InitTemplate(assetsPath string) *Template { func InitTemplate(assetsPath string) *Template {
@ -78,6 +83,38 @@ func InitTemplate(assetsPath string) *Template {
return &t return &t
} }
type DevTemplateExecutor struct {
assetsPath string
tpe string
}
func InitDevTemplate(assetsPath string) *Template {
return &Template{
html: DevTemplateExecutor{assetsPath, "html"},
opds: DevTemplateExecutor{assetsPath, "txt"},
}
}
func (e DevTemplateExecutor) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
templatePath := path.Join(e.assetsPath, "templates")
file := path.Join(templatePath, name)
var t TemplateExecutor
switch e.tpe {
case "html":
included_files := []string{file}
for _, f := range []string{"header.html", "footer.html", "book_list.html"} {
included_files = append(included_files, path.Join(templatePath, f))
}
t = html_tmpl.Must(html_tmpl.New("html").Funcs(html_tmpl.FuncMap{
"strings_join": strings.Join,
}).ParseFiles(included_files...))
case "txt":
t = txt_tmpl.Must(txt_tmpl.ParseFiles(file))
}
return t.ExecuteTemplate(wr, name, data)
}
func loadJson(w http.ResponseWriter, tmpl string, data interface{}) error { func loadJson(w http.ResponseWriter, tmpl string, data interface{}) error {
var res []byte var res []byte
var err error var err error

View file

@ -25,6 +25,7 @@ func main() {
setAdminUser = flag.String("set-admin-user", "", "create/update this user to be admin if set-admin-pass is also specified") setAdminUser = flag.String("set-admin-user", "", "create/update this user to be admin if set-admin-pass is also specified")
setAdminPass = flag.String("set-admin-pass", "", "create/update admin user password if set-admin-user is also specified") setAdminPass = flag.String("set-admin-pass", "", "create/update admin user password if set-admin-user is also specified")
ro = flag.Bool("ro", false, "read only mode") ro = flag.Bool("ro", false, "read only mode")
dev = flag.Bool("dev", false, "development mode (reload templates on each page view)")
) )
flag.Parse() flag.Parse()
@ -63,7 +64,12 @@ func main() {
db = database.RO(db) db = database.RO(db)
} }
template := trantor.InitTemplate(*assetsPath) var template *trantor.Template
if *dev {
template = trantor.InitDevTemplate(*assetsPath)
} else {
template = trantor.InitTemplate(*assetsPath)
}
sg := trantor.InitStats(db, store, template, *ro) sg := trantor.InitStats(db, store, template, *ro)
trantor.InitUpload(db, store) trantor.InitUpload(db, store)
trantor.InitTasks(db, *loggerConfig) trantor.InitTasks(db, *loggerConfig)