2021-03-20 20:40:40 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 Luke Granger-Brown <depot@lukegb.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
package fuphttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
|
2021-03-21 03:03:15 +00:00
|
|
|
"github.com/google/safehtml"
|
2021-03-20 19:53:43 +00:00
|
|
|
"github.com/google/safehtml/template"
|
|
|
|
"github.com/google/safehtml/template/uncheckedconversions"
|
2021-03-21 03:03:15 +00:00
|
|
|
shuncheckedconversions "github.com/google/safehtml/uncheckedconversions"
|
2021-03-20 19:53:43 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-03-21 03:03:15 +00:00
|
|
|
"hg.lukegb.com/lukegb/depot/web/fup/hashfs"
|
2021-03-20 19:53:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Templates fs.FS
|
|
|
|
Static fs.FS
|
2021-03-21 03:03:15 +00:00
|
|
|
StaticRoot safehtml.TrustedResourceURL
|
2021-03-20 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
indexTmpl *template.Template
|
|
|
|
notFoundTmpl *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) Handler() http.Handler {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusNotFound)
|
|
|
|
a.notFoundTmpl.Execute(rw, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTemplate(t *template.Template, fsys fs.FS, name string) (*template.Template, error) {
|
|
|
|
bs, err := fs.ReadFile(fsys, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading template %q: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.ParseFromTrustedTemplate(
|
|
|
|
uncheckedconversions.TrustedTemplateFromStringKnownToSatisfyTypeContract(string(bs)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:03:15 +00:00
|
|
|
func loadTemplate(fsys fs.FS, name string, funcs template.FuncMap) (*template.Template, error) {
|
|
|
|
t := template.New(name).Funcs(funcs)
|
2021-03-20 19:53:43 +00:00
|
|
|
var err error
|
|
|
|
if t, err = parseTemplate(t, fsys, "base.html"); err != nil {
|
|
|
|
return nil, fmt.Errorf("loading base template: %w", err)
|
|
|
|
}
|
|
|
|
if t, err = parseTemplate(t, fsys, fmt.Sprintf("%s.html", name)); err != nil {
|
|
|
|
return nil, fmt.Errorf("loading leaf template: %w", err)
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx context.Context, cfg *Config) (*Application, error) {
|
|
|
|
a := new(Application)
|
|
|
|
|
|
|
|
tmpls := []struct {
|
|
|
|
t **template.Template
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{&a.indexTmpl, "index"},
|
|
|
|
{&a.notFoundTmpl, "404"},
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:03:15 +00:00
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"static": func(s string) safehtml.TrustedResourceURL {
|
|
|
|
staticPath := s
|
|
|
|
if fs, ok := cfg.Static.(*hashfs.FS); ok {
|
|
|
|
sp, ok := fs.LookupHashedName(staticPath)
|
|
|
|
if ok {
|
|
|
|
staticPath = sp
|
|
|
|
} else {
|
|
|
|
log.Printf("warning: couldn't find static file %v", staticPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return shuncheckedconversions.TrustedResourceURLFromStringKnownToSatisfyTypeContract(cfg.StaticRoot.String() + staticPath)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
for _, tmpl := range tmpls {
|
2021-03-21 03:03:15 +00:00
|
|
|
t, err := loadTemplate(cfg.Templates, tmpl.name, funcMap)
|
2021-03-20 19:53:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("loading template %q: %w", tmpl.name, err)
|
|
|
|
}
|
|
|
|
*tmpl.t = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|