2024-01-22 23:10:14 -05:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-01-23 00:55:03 -05:00
|
|
|
"time"
|
2024-01-22 23:10:14 -05:00
|
|
|
|
|
|
|
"git.burning.moe/celediel/burning.moe/internal/config"
|
|
|
|
"git.burning.moe/celediel/burning.moe/internal/models"
|
|
|
|
"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",
|
2024-01-23 00:55:03 -05:00
|
|
|
Handler: makeBasicHandler("about"),
|
2024-01-22 23:10:14 -05:00
|
|
|
},
|
|
|
|
// / comes last
|
|
|
|
{
|
|
|
|
Handles: "/",
|
2024-01-23 00:55:03 -05:00
|
|
|
Handler: makeBasicHandler("home"),
|
2024-01-22 23:10:14 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the handlers package.
|
|
|
|
func Initialise(a *config.AppConfig) {
|
|
|
|
app = a
|
|
|
|
}
|
2024-01-23 00:55:03 -05:00
|
|
|
|
|
|
|
// makeBasicTemplateData creates a blank TemplateData containing only the
|
|
|
|
// time the related template was generated
|
|
|
|
func makeBasicTemplateData(name string) models.TemplateData {
|
|
|
|
var strMap map[string]string
|
|
|
|
if _, ok := app.TemplateCache.Cache[name]; ok {
|
|
|
|
strMap = map[string]string{
|
|
|
|
"GeneratedAt": app.TemplateCache.Cache[name].GeneratedAt.Format(time.UnixDate),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strMap = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
templateData := models.TemplateData{
|
|
|
|
StringMap: strMap,
|
|
|
|
}
|
|
|
|
return templateData
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeBasicHandler creates a basic handler that builds from a .page.tmpl
|
|
|
|
// file, and sends only the time the template was generated as TemplateData
|
|
|
|
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"
|
|
|
|
templateData := makeBasicTemplateData(pageName)
|
|
|
|
render.RenderTemplate(w, pageName, &templateData)
|
|
|
|
}
|
|
|
|
}
|