vanity/router/templates.go

60 lines
1.0 KiB
Go

package router
import (
"embed"
"html/template"
"io/fs"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"go.jolheiser.com/overlay"
"go.jolheiser.com/vanity/cmd/flags"
)
var (
//go:embed templates
templateFS embed.FS
ofs = overlay.MustNew(customRoot(), templateFS)
Version string
)
func customRoot() string {
bin, err := os.Executable()
if err != nil {
bin = ""
}
customPath := os.Getenv("VANITY_CUSTOM")
if customPath == "" {
customPath = filepath.Join(bin, "custom")
}
return customPath
}
func tmpl(name string) (*template.Template, error) {
return template.New(name).Funcs(funcMap).ParseFS(ofs, "templates/base.tmpl", "templates/"+name)
}
func static() http.Handler {
sub, err := fs.Sub(ofs, "templates/static")
if err != nil {
return nil
}
return http.FileServer(http.FS(sub))
}
var funcMap = template.FuncMap{
"AppVer": func() string {
return Version
},
"GoVer": func() string {
return runtime.Version()
},
"Domain": func() string {
return strings.TrimSuffix(flags.Domain, "/")
},
}