Skip to content

Commit

Permalink
add back dev build tag for local file access without embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptix committed Feb 22, 2021
1 parent b533088 commit f0e61e7
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 54 deletions.
14 changes: 14 additions & 0 deletions web/embedded_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 43 additions & 0 deletions web/embedded_prod.go
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion web/handlers/admin/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")...),
Expand Down
12 changes: 6 additions & 6 deletions web/handlers/admin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ 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.
// It is not registering on the mux router like other pages to clean up the authorize flow.
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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions web/handlers/auth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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 {
if m == nil {
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
Expand Down
37 changes: 11 additions & 26 deletions web/handlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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?
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions web/handlers/news/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
8 changes: 0 additions & 8 deletions web/production.go

This file was deleted.

7 changes: 0 additions & 7 deletions web/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package web

import (
"embed"
"fmt"
"html/template"
"io/ioutil"
Expand All @@ -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{
Expand Down

0 comments on commit f0e61e7

Please sign in to comment.