refactor templates
This commit is contained in:
@@ -51,8 +51,14 @@ func main() {
|
||||
*musicPath,
|
||||
*listenAddr,
|
||||
)
|
||||
s.SetupAdmin()
|
||||
s.SetupSubsonic()
|
||||
err = s.SetupAdmin()
|
||||
if err != nil {
|
||||
log.Fatalf("error setting up admin routes: %v\n", err)
|
||||
}
|
||||
err = s.SetupSubsonic()
|
||||
if err != nil {
|
||||
log.Fatalf("error setting up subsonic routes: %v\n", err)
|
||||
}
|
||||
log.Printf("starting server at %s", *listenAddr)
|
||||
if err := s.ListenAndServe(); err != nil {
|
||||
log.Fatalf("error starting server: %v\n", err)
|
||||
|
||||
1
go.mod
1
go.mod
@@ -21,6 +21,7 @@ require (
|
||||
github.com/peterbourgon/ff v1.2.0
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be
|
||||
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b
|
||||
github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755
|
||||
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd // indirect
|
||||
google.golang.org/appengine v1.5.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -110,6 +110,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
|
||||
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
|
||||
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=
|
||||
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b h1:4kg1wyftSKxLtnPAvcRWakIPpokB9w780/KwrNLnfPA=
|
||||
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
10
server/assets_src/templates/partials/box.tmpl
Normal file
10
server/assets_src/templates/partials/box.tmpl
Normal file
@@ -0,0 +1,10 @@
|
||||
{{ define "box" }}
|
||||
<div class="padded box mono">
|
||||
<div class="box-title">
|
||||
<i class="mdi mdi-chart-arc"></i> {{ .Title }}
|
||||
</div>
|
||||
<div class="block-right">
|
||||
{{ .Title }}
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func (c *Controller) ServeLogin(w http.ResponseWriter, r *http.Request) {
|
||||
renderTemplate(w, r, c.Templates["login"], nil)
|
||||
renderTemplate(w, r, c.Templates["login.tmpl"], nil)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -71,11 +71,11 @@ func (c *Controller) ServeHome(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host,
|
||||
)
|
||||
data.RequestRoot = fmt.Sprintf("%s://%s", scheme, host)
|
||||
renderTemplate(w, r, c.Templates["home"], data)
|
||||
renderTemplate(w, r, c.Templates["home.tmpl"], data)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeChangeOwnPassword(w http.ResponseWriter, r *http.Request) {
|
||||
renderTemplate(w, r, c.Templates["change_own_password"], nil)
|
||||
renderTemplate(w, r, c.Templates["change_own_password.tmpl"], nil)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeChangeOwnPasswordDo(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -143,7 +143,7 @@ func (c *Controller) ServeChangePassword(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
data := &templateData{}
|
||||
data.SelectedUser = user
|
||||
renderTemplate(w, r, c.Templates["change_password"], data)
|
||||
renderTemplate(w, r, c.Templates["change_password.tmpl"], data)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeChangePasswordDo(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -184,7 +184,7 @@ func (c *Controller) ServeDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
data := &templateData{}
|
||||
data.SelectedUser = user
|
||||
renderTemplate(w, r, c.Templates["delete_user"], data)
|
||||
renderTemplate(w, r, c.Templates["delete_user.tmpl"], data)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeDeleteUserDo(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -198,7 +198,7 @@ func (c *Controller) ServeDeleteUserDo(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Controller) ServeCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
renderTemplate(w, r, c.Templates["create_user"], nil)
|
||||
renderTemplate(w, r, c.Templates["create_user.tmpl"], nil)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -240,7 +240,7 @@ func (c *Controller) ServeUpdateLastFMAPIKey(w http.ResponseWriter, r *http.Requ
|
||||
data := &templateData{}
|
||||
data.CurrentLastFMAPIKey = c.GetSetting("lastfm_api_key")
|
||||
data.CurrentLastFMAPISecret = c.GetSetting("lastfm_secret")
|
||||
renderTemplate(w, r, c.Templates["update_lastfm_api_key"], data)
|
||||
renderTemplate(w, r, c.Templates["update_lastfm_api_key.tmpl"], data)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeUpdateLastFMAPIKeyDo(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
@@ -38,7 +38,8 @@ func renderTemplate(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
err := tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("500 when executing: %v", err), 500)
|
||||
log.Println("error executing template: %v\n", err)
|
||||
http.Redirect(w, r, "/", 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,21 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gorilla/securecookie"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/shurcooL/httpfs/html/vfstemplate"
|
||||
"github.com/shurcooL/httpfs/path/vfspath"
|
||||
"github.com/wader/gormstore"
|
||||
)
|
||||
|
||||
var (
|
||||
tmplFuncMap = template.FuncMap{
|
||||
"humandate": humanize.Time,
|
||||
}
|
||||
)
|
||||
|
||||
func extendFrom(tmpl *template.Template, key string) *template.Template {
|
||||
strT, ok := assets.String(key)
|
||||
if !ok {
|
||||
log.Fatalf("error when reading %q from assets", key)
|
||||
}
|
||||
if tmpl == nil {
|
||||
tmpl = template.New("layout").Funcs(tmplFuncMap)
|
||||
} else {
|
||||
tmpl = template.Must(tmpl.Clone())
|
||||
}
|
||||
newT, err := tmpl.Parse(strT)
|
||||
if err != nil {
|
||||
log.Fatalf("error when parsing template: %v", err)
|
||||
}
|
||||
return newT
|
||||
}
|
||||
|
||||
func (s *Server) SetupAdmin() {
|
||||
func (s *Server) SetupAdmin() error {
|
||||
sessionKey := []byte(s.GetSetting("session_key"))
|
||||
if len(sessionKey) == 0 {
|
||||
sessionKey = securecookie.GenerateRandomKey(32)
|
||||
@@ -43,16 +24,34 @@ func (s *Server) SetupAdmin() {
|
||||
s.SessDB = gormstore.New(s.DB, sessionKey)
|
||||
go s.SessDB.PeriodicCleanup(time.Hour, nil)
|
||||
//
|
||||
layoutT := extendFrom(nil, "/templates/layout.tmpl")
|
||||
userT := extendFrom(layoutT, "/templates/user.tmpl")
|
||||
s.Templates = map[string]*template.Template{
|
||||
"login": extendFrom(layoutT, "/templates/pages/login.tmpl"),
|
||||
"home": extendFrom(userT, "/templates/pages/home.tmpl"),
|
||||
"change_own_password": extendFrom(userT, "/templates/pages/change_own_password.tmpl"),
|
||||
"change_password": extendFrom(userT, "/templates/pages/change_password.tmpl"),
|
||||
"delete_user": extendFrom(userT, "/templates/pages/delete_user.tmpl"),
|
||||
"create_user": extendFrom(userT, "/templates/pages/create_user.tmpl"),
|
||||
"update_lastfm_api_key": extendFrom(userT, "/templates/pages/update_lastfm_api_key.tmpl"),
|
||||
tmplBase := template.
|
||||
New("layout").
|
||||
Funcs(template.FuncMap{
|
||||
"humandate": humanize.Time,
|
||||
})
|
||||
tmplLayouts, err := vfstemplate.ParseGlob(assets, tmplBase, "/templates/layouts/*")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parsing layouts")
|
||||
}
|
||||
tmplPartials, err := vfstemplate.ParseGlob(assets, tmplLayouts, "/templates/partials/*")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parsing partials")
|
||||
}
|
||||
pages, err := vfspath.Glob(assets, "/templates/pages/*")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parsing pages")
|
||||
}
|
||||
s.Templates = make(map[string]*template.Template)
|
||||
for _, page := range pages {
|
||||
tmplStr, ok := assets.String(page)
|
||||
if !ok {
|
||||
return fmt.Errorf("getting template %q from assets: %v\n", page)
|
||||
}
|
||||
tmplBaseClone := template.Must(tmplPartials.Clone())
|
||||
tmplWithPage := template.Must(tmplBaseClone.Parse(tmplStr))
|
||||
tmplWithPartial := template.Must(tmplWithPage.Parse(tmplStr))
|
||||
shortName := filepath.Base(page)
|
||||
s.Templates[shortName] = tmplWithPartial
|
||||
}
|
||||
//
|
||||
withPublicWare := newChain(
|
||||
@@ -90,4 +89,5 @@ func (s *Server) SetupAdmin() {
|
||||
s.mux.HandleFunc("/admin/create_user_do", withAdminWare(s.ServeCreateUserDo))
|
||||
s.mux.HandleFunc("/admin/update_lastfm_api_key", withAdminWare(s.ServeUpdateLastFMAPIKey))
|
||||
s.mux.HandleFunc("/admin/update_lastfm_api_key_do", withAdminWare(s.ServeUpdateLastFMAPIKeyDo))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package server
|
||||
|
||||
func (s *Server) SetupSubsonic() {
|
||||
func (s *Server) SetupSubsonic() error {
|
||||
withWare := newChain(
|
||||
s.WithLogging,
|
||||
s.WithCORS,
|
||||
@@ -45,4 +45,5 @@ func (s *Server) SetupSubsonic() {
|
||||
s.mux.HandleFunc("/rest/getAlbumList.view", withWare(s.GetAlbumList))
|
||||
s.mux.HandleFunc("/rest/search2", withWare(s.SearchTwo))
|
||||
s.mux.HandleFunc("/rest/search2.view", withWare(s.SearchTwo))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user