burning.moe/internal/handlers/handlers.go
Lilian Jónsdóttir 8a2319033e refactor how template data is handled + load template data from file
Generating template data is now handled by templatedata instead of handlers.
Renderer doesn't recieve any data, but now asks for data from templatedata.
Additionally, template data is loaded from file (too lazy to split that into its own commit).
Templates in ./templates/name.page.tmpl will have data loaded from ./templates/name.page.json
2024-01-23 16:10:15 -08:00

45 lines
971 B
Go

package handlers
import (
"net/http"
"git.burning.moe/celediel/burning.moe/internal/config"
"git.burning.moe/celediel/burning.moe/internal/render"
)
// Handler holds data required for handlers.
type Handler struct {
Handles string
Handler func(w http.ResponseWriter, r *http.Request)
}
var app *config.AppConfig
// The actual handlers
var Handlers = []Handler{
// /about
{
Handles: "/about",
Handler: makeBasicHandler("about"),
},
// / comes last
{
Handles: "/",
Handler: makeBasicHandler("home"),
},
}
// Initialise the handlers package.
func Initialise(a *config.AppConfig) {
app = a
}
// makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl
func makeBasicHandler(name string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
app.Logger.Infof("Got request for %s page", name)
pageName := name + ".page.tmpl"
render.RenderTemplate(w, pageName)
}
}