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