2020-05-24 14:41:19 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-29 19:55:07 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
2020-05-24 22:19:05 +03:00
|
|
|
"net/http"
|
2020-05-24 14:41:19 +03:00
|
|
|
"path"
|
2020-05-24 17:34:32 +03:00
|
|
|
"strings"
|
2020-05-24 14:41:19 +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,
|
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-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)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 22:19:05 +03:00
|
|
|
|
|
|
|
func respondAlert(w http.ResponseWriter, error, success, tplName string, status int) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
if tplName == "" {
|
2020-10-09 22:37:16 +03:00
|
|
|
tplName = config.IndexTemplate
|
2020-05-24 22:19:05 +03:00
|
|
|
}
|
|
|
|
templates[tplName].Execute(w, struct {
|
|
|
|
Error string
|
|
|
|
Success string
|
2020-06-07 20:28:32 +03:00
|
|
|
}{Error: error, Success: success})
|
2020-05-24 22:19:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do better
|
|
|
|
func handleAlerts(w http.ResponseWriter, r *http.Request, tplName string) bool {
|
|
|
|
if err := r.URL.Query().Get("error"); err != "" {
|
|
|
|
if err == "unauthorized" {
|
|
|
|
respondAlert(w, err, "", tplName, http.StatusUnauthorized)
|
|
|
|
} else {
|
|
|
|
respondAlert(w, err, "", tplName, http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if success := r.URL.Query().Get("success"); success != "" {
|
|
|
|
respondAlert(w, "", success, tplName, http.StatusOK)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|