add packr

This commit is contained in:
sentriz
2019-04-23 14:42:44 +01:00
parent 85f7563cc5
commit 09f758e0e3
5 changed files with 104 additions and 62 deletions

View File

@@ -12,7 +12,7 @@ import (
)
func (c *Controller) ServeLogin(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, r, "login", nil)
renderTemplate(w, r, c.Templates["login"], nil)
}
func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
@@ -66,11 +66,11 @@ func (c *Controller) ServeHome(w http.ResponseWriter, r *http.Request) {
r.Host,
)
data.RequestRoot = fmt.Sprintf("%s://%s", scheme, host)
renderTemplate(w, r, "home", &data)
renderTemplate(w, r, c.Templates["home"], &data)
}
func (c *Controller) ServeChangeOwnPassword(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, r, "change_own_password", nil)
renderTemplate(w, r, c.Templates["change_own_password"], nil)
}
func (c *Controller) ServeChangeOwnPasswordDo(w http.ResponseWriter, r *http.Request) {
@@ -135,7 +135,7 @@ func (c *Controller) ServeChangePassword(w http.ResponseWriter, r *http.Request)
}
var data templateData
data.SelectedUser = &user
renderTemplate(w, r, "change_password", &data)
renderTemplate(w, r, c.Templates["change_password"], &data)
}
func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Request) {
@@ -158,7 +158,7 @@ func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Reques
}
func (c *Controller) ServeCreateUser(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, r, "create_user", nil)
renderTemplate(w, r, c.Templates["create_user"], nil)
}
func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
@@ -200,7 +200,7 @@ func (c *Controller) ServeUpdateLastFMAPIKey(w http.ResponseWriter, r *http.Requ
var data templateData
data.CurrentLastFMAPIKey = c.GetSetting("lastfm_api_key")
data.CurrentLastFMAPISecret = c.GetSetting("lastfm_secret")
renderTemplate(w, r, "update_lastfm_api_key", &data)
renderTemplate(w, r, c.Templates["update_lastfm_api_key"], &data)
}
func (c *Controller) ServeUpdateLastFMAPIKeyDo(w http.ResponseWriter, r *http.Request) {

View File

@@ -7,7 +7,6 @@ import (
"html/template"
"log"
"net/http"
"path/filepath"
"strconv"
"github.com/gorilla/sessions"
@@ -18,45 +17,10 @@ import (
"github.com/wader/gormstore"
)
var (
templates = make(map[string]*template.Template)
)
func init() {
templates["login"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "pages", "login.tmpl"),
))
templates["home"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "home.tmpl"),
))
templates["change_password"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "change_password.tmpl"),
))
templates["change_own_password"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "change_own_password.tmpl"),
))
templates["create_user"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "create_user.tmpl"),
))
templates["update_lastfm_api_key"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "update_lastfm_api_key.tmpl"),
))
}
type Controller struct {
DB *gorm.DB
SStore *gormstore.Store
DB *gorm.DB
SStore *gormstore.Store
Templates map[string]*template.Template
}
func (c *Controller) GetSetting(key string) string {
@@ -170,7 +134,7 @@ func respondError(w http.ResponseWriter, r *http.Request,
}
func renderTemplate(w http.ResponseWriter, r *http.Request,
name string, data *templateData) {
tmpl *template.Template, data *templateData) {
session := r.Context().Value("session").(*sessions.Session)
if data == nil {
data = &templateData{}
@@ -181,7 +145,7 @@ func renderTemplate(w http.ResponseWriter, r *http.Request,
if ok {
data.User = user
}
err := templates[name].ExecuteTemplate(w, "layout", data)
err := tmpl.Execute(w, data)
if err != nil {
http.Error(w, fmt.Sprintf("500 when executing: %v", err), 500)
return