1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: embed assets into binary

Resolves #26
This commit is contained in:
Steven Tang
2021-01-22 23:50:46 +11:00
parent a3d8c4d464
commit 4e6e665e19
7 changed files with 44 additions and 46 deletions

View File

@ -2,6 +2,7 @@ package routes
import (
"fmt"
"github.com/markbates/pkger"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
@ -18,7 +19,7 @@ func Init() {
var templates map[string]*template.Template
func loadTemplates() {
tplPath := "views"
tplPath := "/views"
tpls := template.New("").Funcs(template.FuncMap{
"json": utils.Json,
"date": utils.FormatDateHuman,
@ -44,7 +45,12 @@ func loadTemplates() {
})
templates = make(map[string]*template.Template)
files, err := ioutil.ReadDir(tplPath)
dir, err := pkger.Open(tplPath)
defer dir.Close()
if err != nil {
panic(err)
}
files, err := dir.Readdir(0)
if err != nil {
panic(err)
}
@ -55,7 +61,17 @@ func loadTemplates() {
continue
}
tpl, err := tpls.New(tplName).ParseFiles(fmt.Sprintf("%s/%s", tplPath, tplName))
templateFile, err := pkger.Open(fmt.Sprintf("%s/%s", tplPath, tplName))
defer templateFile.Close()
if err != nil {
panic(err)
}
template, err := ioutil.ReadAll(templateFile)
if err != nil {
panic(err)
}
tpl, err := tpls.New(tplName).Parse(string(template))
if err != nil {
panic(err)
}