Add dev flag to indicate development runs
Used to reload the templates on each page view.
This commit is contained in:
parent
1536d97d8d
commit
fe36b1d23c
2 changed files with 46 additions and 3 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
@ -53,9 +54,13 @@ func GetStatus(h handler) Status {
|
|||
return s
|
||||
}
|
||||
|
||||
type TemplateExecutor interface {
|
||||
ExecuteTemplate(io.Writer, string, interface{}) error
|
||||
}
|
||||
|
||||
type Template struct {
|
||||
html *html_tmpl.Template
|
||||
opds *txt_tmpl.Template
|
||||
html TemplateExecutor
|
||||
opds TemplateExecutor
|
||||
}
|
||||
|
||||
func InitTemplate(assetsPath string) *Template {
|
||||
|
@ -78,6 +83,38 @@ func InitTemplate(assetsPath string) *Template {
|
|||
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 {
|
||||
var res []byte
|
||||
var err error
|
||||
|
|
8
main.go
8
main.go
|
@ -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")
|
||||
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")
|
||||
dev = flag.Bool("dev", false, "development mode (reload templates on each page view)")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
|
@ -63,7 +64,12 @@ func main() {
|
|||
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)
|
||||
trantor.InitUpload(db, store)
|
||||
trantor.InitTasks(db, *loggerConfig)
|
||||
|
|
Reference in a new issue