chore(ui): add module file and main stub
- chore(ui): add module file and main stub - feat(ui): add layout, pages and partials - feat(ui): add stylesheet and scripts - feat(ui): add page renderer and static file handler - test(ui): cover rendering, escaping and static serving
This commit is contained in:
1 parent
7089a6f408
commit
5ac8f98f10
26 files changed
+3921
No files matched your search
@@ -0,0 +1,209 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed templates/html
|
||||
var uiTemplateFiles embed.FS
|
||||
|
||||
//go:embed static
|
||||
var uiStaticFiles embed.FS
|
||||
|
||||
const defaultBootstrapCommand = "docker exec -i <container> /app user add <username> admin"
|
||||
|
||||
var uiContentTypes = map[string]string{
|
||||
".css": "text/css; charset=utf-8",
|
||||
".js": "text/javascript; charset=utf-8",
|
||||
".mjs": "text/javascript; charset=utf-8",
|
||||
".svg": "image/svg+xml",
|
||||
".json": "application/json",
|
||||
".txt": "text/plain; charset=utf-8",
|
||||
".ico": "image/x-icon",
|
||||
".png": "image/png",
|
||||
".woff2": "font/woff2",
|
||||
}
|
||||
|
||||
type pageData struct {
|
||||
AppName string
|
||||
Static string
|
||||
Page string
|
||||
Script string
|
||||
Username string
|
||||
Role string
|
||||
Zone string
|
||||
TargetUser string
|
||||
Next string
|
||||
Error string
|
||||
NoActiveAdmin bool
|
||||
BootstrapCommand string
|
||||
}
|
||||
|
||||
func (d pageData) IsAdmin() bool {
|
||||
return d.Role == "admin"
|
||||
}
|
||||
|
||||
func (d pageData) Section() string {
|
||||
switch d.Page {
|
||||
case "zones", "zone", "import":
|
||||
return "zones"
|
||||
case "users", "user":
|
||||
return "users"
|
||||
}
|
||||
return d.Page
|
||||
}
|
||||
|
||||
type pageRenderer struct {
|
||||
templates map[string]*template.Template
|
||||
static fs.FS
|
||||
hash string
|
||||
prefix string
|
||||
}
|
||||
|
||||
func staticHash(fsys fs.FS) (string, error) {
|
||||
sum := sha256.New()
|
||||
err := fs.WalkDir(fsys, ".", func(name string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.IsDir() {
|
||||
return nil
|
||||
}
|
||||
data, err := fs.ReadFile(fsys, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(sum, "%s\x00%d\x00", name, len(data))
|
||||
sum.Write(data)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(sum.Sum(nil))[:16], nil
|
||||
}
|
||||
|
||||
func newPageRenderer() (*pageRenderer, error) {
|
||||
templates, err := fs.Sub(uiTemplateFiles, "templates/html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
static, err := fs.Sub(uiStaticFiles, "static")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newPageRendererFS(templates, static)
|
||||
}
|
||||
|
||||
func newPageRendererFS(templates, static fs.FS) (*pageRenderer, error) {
|
||||
hash, err := staticHash(static)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("hash static files: %w", err)
|
||||
}
|
||||
pages, err := fs.Glob(templates, "pages/*.html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(pages) == 0 {
|
||||
return nil, fmt.Errorf("no page templates found")
|
||||
}
|
||||
renderer := &pageRenderer{
|
||||
templates: make(map[string]*template.Template, len(pages)),
|
||||
static: static,
|
||||
hash: hash,
|
||||
prefix: "/static/" + hash + "/",
|
||||
}
|
||||
for _, file := range pages {
|
||||
name := strings.TrimSuffix(path.Base(file), ".html")
|
||||
tmpl, err := template.New(name).ParseFS(templates, "layout.html", "partials/*.html", file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse page %s: %w", name, err)
|
||||
}
|
||||
renderer.templates[name] = tmpl
|
||||
}
|
||||
return renderer, nil
|
||||
}
|
||||
|
||||
func (p *pageRenderer) staticPrefix() string {
|
||||
return p.prefix
|
||||
}
|
||||
|
||||
func (p *pageRenderer) hasScript(page string) bool {
|
||||
info, err := fs.Stat(p.static, "js/"+page+".js")
|
||||
return err == nil && !info.IsDir()
|
||||
}
|
||||
|
||||
func (p *pageRenderer) render(w http.ResponseWriter, status int, page string, data pageData) {
|
||||
tmpl, ok := p.templates[page]
|
||||
if !ok {
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Static = p.prefix
|
||||
data.Page = page
|
||||
if p.hasScript(page) {
|
||||
data.Script = page
|
||||
}
|
||||
if data.NoActiveAdmin && data.BootstrapCommand == "" {
|
||||
data.BootstrapCommand = defaultBootstrapCommand
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.ExecuteTemplate(&buf, "layout", data); err != nil {
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
|
||||
w.WriteHeader(status)
|
||||
w.Write(buf.Bytes())
|
||||
}
|
||||
|
||||
func (p *pageRenderer) staticHandler() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
w.Header().Set("Allow", "GET, HEAD")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.URL.Path, p.prefix) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
name := strings.TrimPrefix(r.URL.Path, p.prefix)
|
||||
if name == "" || strings.ContainsAny(name, "\\\x00") || !fs.ValidPath(name) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
data, err := fs.ReadFile(p.static, name)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
contentType, ok := uiContentTypes[strings.ToLower(path.Ext(name))]
|
||||
if !ok {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if r.Method == http.MethodGet {
|
||||
w.Write(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user