use embed package of go1.16 for embedding assets (fixes #20)

This commit is contained in:
Henry 2021-02-16 19:06:11 +01:00
parent 448f681acb
commit dd575aa0c7
8 changed files with 40 additions and 354 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/ssb-ngi-pointer/go-ssb-room
go 1.15
go 1.16
require (
github.com/BurntSushi/toml v0.3.1

File diff suppressed because one or more lines are too long

View File

@ -11,8 +11,7 @@ to use this pass '-tags dev' to your go build or test commands.
package web
import (
"net/http"
"path/filepath"
"embed"
"go.mindeco.de/goutils"
)
@ -22,6 +21,12 @@ 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.FileSystem = http.Dir(filepath.Join(pkgDir, "templates"))
//go:embed templates/*
var Templates embed.FS
var Assets http.FileSystem = http.Dir(filepath.Join(pkgDir, "assets"))
// var Templates http.FileSystem = http.Dir(filepath.Join(pkgDir, "templates"))
//go:embed assets/*
var Assets embed.FS
// var Assets http.FileSystem = http.Dir(filepath.Join(pkgDir, "assets"))

View File

@ -15,9 +15,9 @@ import (
)
var HTMLTemplates = []string{
"/admin/dashboard.tmpl",
"/admin/allow-list.tmpl",
"/admin/allow-list-remove-confirm.tmpl",
"templates/admin/dashboard.tmpl",
"templates/admin/allow-list.tmpl",
"templates/admin/allow-list-remove-confirm.tmpl",
}
// Handler supplies the elevated access pages to known users.

View File

@ -16,7 +16,7 @@ import (
)
var HTMLTemplates = []string{
"/auth/fallback_sign_in.tmpl",
"templates/auth/fallback_sign_in.tmpl",
}
func Handler(m *mux.Router, r *render.Renderer, a *auth.Handler) http.Handler {

View File

@ -44,14 +44,29 @@ func New(
var a *auth.Handler
r, err := render.New(web.Templates,
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),
render.SetLogger(logger),
render.BaseTemplates("/base.tmpl"),
render.BaseTemplates("templates/base.tmpl"),
render.AddTemplates(concatTemplates(
[]string{
"/landing/index.tmpl",
"/landing/about.tmpl",
"/error.tmpl",
"templates/landing/index.tmpl",
"templates/landing/about.tmpl",
"templates/error.tmpl",
},
news.HTMLTemplates,
roomsAuth.HTMLTemplates,
@ -130,7 +145,7 @@ func New(
msg = ih.LocalizeSimple("ErrorAlreadyAdded")
}
r.HTML("/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
r.HTML("templates/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
return errorTemplateData{
Err: msg,
// TODO: localize?
@ -140,7 +155,7 @@ func New(
}).ServeHTTP(rw, req)
}
notAuthorizedH := r.HTML("/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
notAuthorizedH := r.HTML("templates/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
statusCode := http.StatusUnauthorized
rw.WriteHeader(statusCode)
return errorTemplateData{
@ -188,9 +203,9 @@ func New(
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(web.Assets)))
m.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.FS(web.Assets))))
m.NotFoundHandler = r.HTML("/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
m.NotFoundHandler = r.HTML("templates/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{
"/news/overview.tmpl",
"/news/post.tmpl",
"templates/news/overview.tmpl",
"templates/news/post.tmpl",
}
// Handler creates a http.Handler with all the archives routes attached to it

File diff suppressed because one or more lines are too long