2020-05-24 14:41:19 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2021-04-11 13:42:43 +03:00
|
|
|
"io/fs"
|
2020-05-24 14:41:19 +03:00
|
|
|
"io/ioutil"
|
2021-02-03 22:53:27 +03:00
|
|
|
"net/http"
|
2020-05-24 14:41:19 +03:00
|
|
|
"path"
|
2020-05-24 17:34:32 +03:00
|
|
|
"strings"
|
2021-04-11 13:42:43 +03:00
|
|
|
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"github.com/muety/wakapi/views"
|
2020-05-24 14:41:19 +03:00
|
|
|
)
|
|
|
|
|
2020-11-08 14:46:12 +03:00
|
|
|
func Init() {
|
2020-05-24 14:41:19 +03:00
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
2021-02-03 22:53:27 +03:00
|
|
|
type action func(w http.ResponseWriter, r *http.Request) (int, string, string)
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
var templates map[string]*template.Template
|
|
|
|
|
|
|
|
func loadTemplates() {
|
2021-01-24 02:13:37 +03:00
|
|
|
const tplPath = "/views"
|
2020-05-24 14:41:19 +03:00
|
|
|
tpls := template.New("").Funcs(template.FuncMap{
|
2021-02-13 14:59:59 +03:00
|
|
|
"json": utils.Json,
|
|
|
|
"date": utils.FormatDateHuman,
|
|
|
|
"simpledate": utils.FormatDate,
|
|
|
|
"simpledatetime": utils.FormatDateTime,
|
|
|
|
"title": strings.Title,
|
|
|
|
"join": strings.Join,
|
|
|
|
"add": utils.Add,
|
|
|
|
"capitalize": utils.Capitalize,
|
|
|
|
"toRunes": utils.ToRunes,
|
|
|
|
"entityTypes": models.SummaryTypes,
|
|
|
|
"typeName": typeName,
|
2021-02-21 15:02:11 +03:00
|
|
|
"isDev": func() bool {
|
|
|
|
return config.Get().IsDev()
|
|
|
|
},
|
2020-05-24 18:32:26 +03:00
|
|
|
"getBasePath": func() string {
|
2020-10-04 11:37:38 +03:00
|
|
|
return config.Get().Server.BasePath
|
2020-05-24 18:32:26 +03:00
|
|
|
},
|
2020-05-24 22:42:15 +03:00
|
|
|
"getVersion": func() string {
|
2020-09-29 19:55:07 +03:00
|
|
|
return config.Get().Version
|
2020-05-24 22:42:15 +03:00
|
|
|
},
|
2020-10-16 17:58:16 +03:00
|
|
|
"getDbType": func() string {
|
2021-01-18 23:34:08 +03:00
|
|
|
return strings.ToLower(config.Get().Db.Type)
|
2020-10-16 17:58:16 +03:00
|
|
|
},
|
2020-05-30 21:41:27 +03:00
|
|
|
"htmlSafe": func(html string) template.HTML {
|
|
|
|
return template.HTML(html)
|
|
|
|
},
|
2020-05-24 14:41:19 +03:00
|
|
|
})
|
|
|
|
templates = make(map[string]*template.Template)
|
|
|
|
|
2021-04-11 13:42:43 +03:00
|
|
|
files, err := fs.ReadDir(views.TemplateFiles, ".")
|
2020-05-24 14:41:19 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
tplName := file.Name()
|
|
|
|
if file.IsDir() || path.Ext(tplName) != ".html" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:42:43 +03:00
|
|
|
templateFile, err := views.TemplateFiles.Open(tplName)
|
2021-01-22 15:50:46 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-24 23:39:35 +03:00
|
|
|
templateData, err := ioutil.ReadAll(templateFile)
|
2021-01-22 15:50:46 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-01-24 23:39:35 +03:00
|
|
|
templateFile.Close()
|
|
|
|
|
|
|
|
tpl, err := tpls.New(tplName).Parse(string(templateData))
|
2020-05-24 14:41:19 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
templates[tplName] = tpl
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 02:26:52 +03:00
|
|
|
|
|
|
|
func typeName(t uint8) string {
|
|
|
|
if t == models.SummaryProject {
|
|
|
|
return "project"
|
|
|
|
}
|
|
|
|
if t == models.SummaryLanguage {
|
|
|
|
return "language"
|
|
|
|
}
|
|
|
|
if t == models.SummaryEditor {
|
|
|
|
return "editor"
|
|
|
|
}
|
|
|
|
if t == models.SummaryOS {
|
|
|
|
return "operating system"
|
|
|
|
}
|
|
|
|
if t == models.SummaryMachine {
|
|
|
|
return "machine"
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
2021-02-12 20:37:30 +03:00
|
|
|
|
|
|
|
func defaultErrorRedirectTarget() string {
|
|
|
|
return fmt.Sprintf("%s/?error=unauthorized", config.Get().Server.BasePath)
|
|
|
|
}
|