Files
rs_website/internal/handler/handler.go

129 lines
3.2 KiB
Go

// Package handler contains all HTTP request handlers.
package handler
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"ridgwaysystems.org/website/internal/blog"
)
// Handler holds shared dependencies for all HTTP handlers.
type Handler struct {
store *blog.Store
dataDir string
templates map[string]*template.Template
siteURL string
contactEmail string
devMode bool
}
// New creates a Handler. dataDir is the path to the data/ directory.
func New(store *blog.Store, dataDir string) *Handler {
h := &Handler{
store: store,
dataDir: dataDir,
siteURL: getenv("SITE_URL", "https://ridgwaysystems.org"),
contactEmail: getenv("CONTACT_EMAIL", "hire@ridgwaysystems.org"),
devMode: os.Getenv("DEV") == "1",
}
if !h.devMode {
h.templates = mustLoadTemplates()
}
return h
}
func getenv(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
// tmpl returns the template set for a given name, reloading in dev mode.
func (h *Handler) tmpl(name string) *template.Template {
if h.devMode {
return mustLoadTemplates()[name]
}
return h.templates[name]
}
func mustLoadTemplates() map[string]*template.Template {
m := make(map[string]*template.Template)
funcMap := template.FuncMap{
"formatDate": formatDate,
"joinTags": joinTags,
}
base := "templates/base.html"
pages := []struct {
name string
file string
}{
{"index", "templates/index.html"},
{"blog", "templates/blog.html"},
{"post", "templates/post.html"},
{"infrastructure", "templates/infrastructure.html"},
{"status", "templates/status.html"},
{"about", "templates/about.html"},
{"hire", "templates/hire.html"},
{"resume", "templates/resume.html"},
{"error", "templates/error.html"},
{"admin-login", "templates/admin/login.html"},
{"admin-dashboard", "templates/admin/dashboard.html"},
{"admin-editor", "templates/admin/editor.html"},
{"admin-status", "templates/admin/status.html"},
{"admin-uploads", "templates/admin/uploads.html"},
}
for _, p := range pages {
t, err := template.New(filepath.Base(p.file)).Funcs(funcMap).ParseFiles(base, p.file)
if err != nil {
log.Fatalf("template %s: %v", p.name, err)
}
m[p.name] = t
}
return m
}
func (h *Handler) render(w http.ResponseWriter, name string, data any) {
t := h.tmpl(name)
if t == nil {
http.Error(w, "template not found: "+name, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := t.ExecuteTemplate(w, "base", data); err != nil {
log.Printf("render %s: %v", name, err)
}
}
// errorData is passed to the error template.
type errorData struct {
Code int
Title string
Message string
}
func (h *Handler) renderErr(w http.ResponseWriter, code int, msg string) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(code)
data := errorData{Code: code, Title: http.StatusText(code), Message: msg}
t := h.tmpl("error")
if t == nil {
fmt.Fprintf(w, "<html><body><h1>%d %s</h1><p>%s</p><a href='/'>Home</a></body></html>",
code, http.StatusText(code), msg)
return
}
if err := t.ExecuteTemplate(w, "base", data); err != nil {
log.Printf("renderErr %d: %v", code, err)
}
}