wakapi/routes/routes.go

61 lines
1.2 KiB
Go
Raw Normal View History

package routes
import (
"fmt"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/utils"
"html/template"
"io/ioutil"
"path"
2020-05-24 17:34:32 +03:00
"strings"
)
2020-11-08 14:46:12 +03:00
func Init() {
loadTemplates()
}
var templates map[string]*template.Template
func loadTemplates() {
tplPath := "views"
tpls := template.New("").Funcs(template.FuncMap{
2020-05-24 17:34:32 +03:00
"json": utils.Json,
"date": utils.FormatDateHuman,
"title": strings.Title,
"capitalize": utils.Capitalize,
"toRunes": utils.ToRunes,
"getBasePath": func() string {
return config.Get().Server.BasePath
},
2020-05-24 22:42:15 +03:00
"getVersion": func() string {
return config.Get().Version
2020-05-24 22:42:15 +03:00
},
2020-10-16 17:58:16 +03:00
"getDbType": func() string {
return strings.ToLower(config.Get().Db.Dialect)
},
"htmlSafe": func(html string) template.HTML {
return template.HTML(html)
},
})
templates = make(map[string]*template.Template)
files, err := ioutil.ReadDir(tplPath)
if err != nil {
panic(err)
}
for _, file := range files {
tplName := file.Name()
if file.IsDir() || path.Ext(tplName) != ".html" {
continue
}
tpl, err := tpls.New(tplName).ParseFiles(fmt.Sprintf("%s/%s", tplPath, tplName))
if err != nil {
panic(err)
}
templates[tplName] = tpl
}
}