add back dev build tag for local file access without embedding

This commit is contained in:
Henry 2021-02-22 15:26:51 +01:00
parent b5330884ab
commit f0e61e7189
9 changed files with 81 additions and 54 deletions

View File

@ -4,4 +4,18 @@
package web
import (
"net/http"
"path/filepath"
"go.mindeco.de/goutils"
)
const Production = false
// absolute path of where this package is located
var pkgDir = goutils.MustLocatePackage("github.com/ssb-ngi-pointer/go-ssb-room/web")
var Templates = http.Dir(filepath.Join(pkgDir, "templates"))
var Assets = http.Dir(filepath.Join(pkgDir, "assets"))

43
web/embedded_prod.go Normal file
View File

@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
// +build !dev
package web
import (
"embed"
"io/fs"
"log"
"net/http"
)
// Production can be used to determain different aspects at compile time (like hot template reloading)
const Production = true
var (
Assets http.FileSystem
Templates http.FileSystem
)
// correct the paths by stripping their prefixes
func init() {
var err error
prefixedAssets, err := fs.Sub(embedAssets, "assets")
if err != nil {
log.Fatal(err)
}
Assets = http.FS(prefixedAssets)
prefixedTemplates, err := fs.Sub(embedTemplates, "templates")
if err != nil {
log.Fatal(err)
}
Templates = http.FS(prefixedTemplates)
}
//go:embed templates/*
var embedTemplates embed.FS
//go:embed assets/*
var embedAssets embed.FS

View File

@ -55,7 +55,7 @@ func newSession(t *testing.T) *testSession {
}
testFuncs["is_logged_in"] = func() *admindb.User { return nil }
r, err := render.New(http.FS(web.Templates),
r, err := render.New(web.Templates,
render.SetLogger(log),
render.BaseTemplates("templates/base.tmpl"),
render.AddTemplates(append(HTMLTemplates, "templates/error.tmpl")...),

View File

@ -15,9 +15,9 @@ import (
)
var HTMLTemplates = []string{
"templates/admin/dashboard.tmpl",
"templates/admin/allow-list.tmpl",
"templates/admin/allow-list-remove-confirm.tmpl",
"admin/dashboard.tmpl",
"admin/allow-list.tmpl",
"admin/allow-list-remove-confirm.tmpl",
}
// Handler supplies the elevated access pages to known users.
@ -25,7 +25,7 @@ var HTMLTemplates = []string{
func Handler(r *render.Renderer, roomState *roomstate.Manager, al admindb.AllowListService) http.Handler {
mux := &http.ServeMux{}
mux.HandleFunc("/dashboard", r.HTML("templates/admin/dashboard.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
mux.HandleFunc("/dashboard", r.HTML("admin/dashboard.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
lst := roomState.List()
return struct {
Clients []string
@ -38,9 +38,9 @@ func Handler(r *render.Renderer, roomState *roomstate.Manager, al admindb.AllowL
al: al,
}
mux.HandleFunc("/members", r.HTML("templates/admin/allow-list.tmpl", ah.overview))
mux.HandleFunc("/members", r.HTML("admin/allow-list.tmpl", ah.overview))
mux.HandleFunc("/members/add", ah.add)
mux.HandleFunc("/members/remove/confirm", r.HTML("templates/admin/allow-list-remove-confirm.tmpl", ah.removeConfirm))
mux.HandleFunc("/members/remove/confirm", r.HTML("admin/allow-list-remove-confirm.tmpl", ah.removeConfirm))
mux.HandleFunc("/members/remove", ah.remove)
return customStripPrefix("/admin", mux)

View File

@ -16,7 +16,7 @@ import (
)
var HTMLTemplates = []string{
"templates/auth/fallback_sign_in.tmpl",
"auth/fallback_sign_in.tmpl",
}
func Handler(m *mux.Router, r *render.Renderer, a *auth.Handler) http.Handler {
@ -24,7 +24,7 @@ func Handler(m *mux.Router, r *render.Renderer, a *auth.Handler) http.Handler {
m = router.Auth(nil)
}
m.Get(router.AuthFallbackSignInForm).Handler(r.HTML("templates/auth/fallback_sign_in.tmpl", func(w http.ResponseWriter, req *http.Request) (interface{}, error) {
m.Get(router.AuthFallbackSignInForm).Handler(r.HTML("auth/fallback_sign_in.tmpl", func(w http.ResponseWriter, req *http.Request) (interface{}, error) {
return map[string]interface{}{
csrf.TemplateTag: csrf.TemplateField(req),
}, nil

View File

@ -44,29 +44,14 @@ func New(
var a *auth.Handler
embeddedFS := web.Templates
// f, err := embeddedFS.Open("templates/base.tmpl")
// if err != nil {
// etr, err := embeddedFS.ReadDir("templates")
// if err != nil {
// panic(err)
// }
// for i, e := range etr {
// fmt.Println(i, e)
// }
// return nil, fmt.Errorf("couldn't open base: %w", err)
// }
// f.Close()
r, err := render.New(http.FS(embeddedFS),
r, err := render.New(web.Templates,
render.SetLogger(logger),
render.BaseTemplates("templates/base.tmpl"),
render.BaseTemplates("base.tmpl"),
render.AddTemplates(concatTemplates(
[]string{
"templates/landing/index.tmpl",
"templates/landing/about.tmpl",
"templates/error.tmpl",
"landing/index.tmpl",
"landing/about.tmpl",
"error.tmpl",
},
news.HTMLTemplates,
roomsAuth.HTMLTemplates,
@ -145,7 +130,7 @@ func New(
msg = ih.LocalizeSimple("ErrorAlreadyAdded")
}
r.HTML("templates/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
r.HTML("error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
return errorTemplateData{
Err: msg,
// TODO: localize?
@ -155,7 +140,7 @@ func New(
}).ServeHTTP(rw, req)
}
notAuthorizedH := r.HTML("templates/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
notAuthorizedH := r.HTML("error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
statusCode := http.StatusUnauthorized
rw.WriteHeader(statusCode)
return errorTemplateData{
@ -200,12 +185,12 @@ func New(
adminHandler := a.Authenticate(admin.Handler(r, roomState, al))
mainMux.Handle("/admin/", adminHandler)
m.Get(router.CompleteIndex).Handler(r.StaticHTML("templates/landing/index.tmpl"))
m.Get(router.CompleteAbout).Handler(r.StaticHTML("templates/landing/about.tmpl"))
m.Get(router.CompleteIndex).Handler(r.StaticHTML("landing/index.tmpl"))
m.Get(router.CompleteAbout).Handler(r.StaticHTML("landing/about.tmpl"))
m.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.FS(web.Assets))))
m.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(web.Assets)))
m.NotFoundHandler = r.HTML("templates/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
m.NotFoundHandler = r.HTML("error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
rw.WriteHeader(http.StatusNotFound)
msg := i18n.LocalizerFromRequest(locHelper, req).LocalizeSimple("PageNotFound")
return errorTemplateData{http.StatusNotFound, "Not Found", msg}, nil

View File

@ -11,8 +11,8 @@ import (
)
var HTMLTemplates = []string{
"templates/news/overview.tmpl",
"templates/news/post.tmpl",
"news/overview.tmpl",
"news/post.tmpl",
}
// Handler creates a http.Handler with all the archives routes attached to it
@ -21,8 +21,8 @@ func Handler(m *mux.Router, r *render.Renderer) http.Handler {
m = router.News(nil)
}
m.Get(router.NewsOverview).Handler(r.HTML("templates/news/overview.tmpl", showOverview))
m.Get(router.NewsPost).Handler(r.HTML("templates/news/post.tmpl", showPost))
m.Get(router.NewsOverview).Handler(r.HTML("news/overview.tmpl", showOverview))
m.Get(router.NewsPost).Handler(r.HTML("news/post.tmpl", showPost))
return m
}

View File

@ -1,8 +0,0 @@
// SPDX-License-Identifier: MIT
// +build !dev
package web
// Production can be used to determain different aspects at compile time (like hot template reloading)
const Production = true

View File

@ -3,7 +3,6 @@
package web
import (
"embed"
"fmt"
"html/template"
"io/ioutil"
@ -23,12 +22,6 @@ import (
"go.mindeco.de/logging"
)
//go:embed templates/*
var Templates embed.FS
//go:embed assets/*
var Assets embed.FS
// TemplateFuncs returns a map of template functions
func TemplateFuncs(m *mux.Router) template.FuncMap {
return template.FuncMap{