fup: add a template function for getting paths to static assets
We add the hash of the file to the static assets, so they can be cached indefinitely. This also, however, means that we need some way of referring to them.
This commit is contained in:
parent
5846385513
commit
e21db7a061
2 changed files with 29 additions and 4 deletions
|
@ -10,15 +10,18 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/google/safehtml"
|
||||||
"github.com/google/safehtml/template"
|
"github.com/google/safehtml/template"
|
||||||
"github.com/google/safehtml/template/uncheckedconversions"
|
"github.com/google/safehtml/template/uncheckedconversions"
|
||||||
|
shuncheckedconversions "github.com/google/safehtml/uncheckedconversions"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"hg.lukegb.com/lukegb/depot/web/fup/hashfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Templates fs.FS
|
Templates fs.FS
|
||||||
Static fs.FS
|
Static fs.FS
|
||||||
StaticRoot string
|
StaticRoot safehtml.TrustedResourceURL
|
||||||
}
|
}
|
||||||
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
|
@ -48,8 +51,8 @@ func parseTemplate(t *template.Template, fsys fs.FS, name string) (*template.Tem
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadTemplate(fsys fs.FS, name string) (*template.Template, error) {
|
func loadTemplate(fsys fs.FS, name string, funcs template.FuncMap) (*template.Template, error) {
|
||||||
t := template.New(name)
|
t := template.New(name).Funcs(funcs)
|
||||||
var err error
|
var err error
|
||||||
if t, err = parseTemplate(t, fsys, "base.html"); err != nil {
|
if t, err = parseTemplate(t, fsys, "base.html"); err != nil {
|
||||||
return nil, fmt.Errorf("loading base template: %w", err)
|
return nil, fmt.Errorf("loading base template: %w", err)
|
||||||
|
@ -71,8 +74,23 @@ func New(ctx context.Context, cfg *Config) (*Application, error) {
|
||||||
{&a.notFoundTmpl, "404"},
|
{&a.notFoundTmpl, "404"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, tmpl := range tmpls {
|
for _, tmpl := range tmpls {
|
||||||
t, err := loadTemplate(cfg.Templates, tmpl.name)
|
t, err := loadTemplate(cfg.Templates, tmpl.name, funcMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("loading template %q: %w", tmpl.name, err)
|
return nil, fmt.Errorf("loading template %q: %w", tmpl.name, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,5 +6,12 @@ SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<link rel="stylesheet" type="text/css" href="{{static "css/base.css"}}">
|
||||||
|
<script type="module" src="{{ static "js/base.js"}}"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
{{block "main" .}}{{end}}
|
{{block "main" .}}{{end}}
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue