burning.moe/internal/handlers/handlers.go

119 lines
3.2 KiB
Go
Raw Normal View History

package handlers
import (
"fmt"
"html/template"
"net/http"
"strings"
"time"
"git.burning.moe/celediel/burning.moe/internal/config"
"git.burning.moe/celediel/burning.moe/internal/models"
"git.burning.moe/celediel/burning.moe/internal/render"
"git.burning.moe/celediel/burning.moe/internal/td"
)
// Handler holds data required for handlers.
type Handler struct {
Handles string
2024-01-27 19:26:46 -05:00
Handler func(writer http.ResponseWriter, request *http.Request)
}
var app *config.AppConfig
// The actual handlers
var Handlers = []Handler{
{
Handles: "/about",
Handler: makeBasicHandler("about"),
},
{
Handles: "/apps",
Handler: makeLinksHandler("apps"),
},
2024-01-27 19:27:13 -05:00
{
Handles: "/projects",
Handler: makeLinksHandler("projects"),
},
}
// Initialise the handlers package.
func Initialise(a *config.AppConfig) {
app = a
}
// HomeHandler handles /, generating data from Handlers
2024-01-27 19:26:46 -05:00
func HomeHandler(writer http.ResponseWriter, request *http.Request) {
page := "home.page.tmpl"
d := models.TemplateData{}
t, err := render.GetTemplateFromCache(page)
if err != nil {
app.Logger.Error(fmt.Sprintf("couldn't get %s from cache", page), "err", err)
2024-01-24 03:56:16 -05:00
d.StringMap = map[string]string{
"GeneratedAt": time.Now().Format(time.UnixDate),
}
} else {
d.StringMap = map[string]string{
"GeneratedAt": t.GeneratedAt.Format(time.UnixDate),
}
}
var pages []models.Link = []models.Link{}
for _, handler := range Handlers {
href := strings.TrimPrefix(handler.Handles, "/")
pages = append(pages, models.Link{
Href: template.URL(href),
Text: href,
})
}
d.LinkMap = make(map[string][]models.Link)
d.LinkMap["Pages"] = pages
app.Logger.Debug("handling home with some data", "data", &d)
2024-01-27 19:26:46 -05:00
render.RenderTemplateWithData(writer, "home.page.tmpl", &d)
}
2024-01-27 19:28:03 -05:00
// RobotHandler creates a handler for robots.txt out of existing Handlers
func RobotHandler(writer http.ResponseWriter, request *http.Request) {
robots := fmt.Sprintf("User-agent: %s\nAllow: /\n", request.UserAgent())
for _, handler := range Handlers {
robots += fmt.Sprintf("Allow: %s\n", handler.Handles)
}
robots += "Disallow: /*\n"
fmt.Fprint(writer, robots)
}
// makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl
2024-01-27 19:26:46 -05:00
func makeBasicHandler(name string) func(writer http.ResponseWriter, request *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
pageName := name + ".page.tmpl"
2024-01-27 19:26:46 -05:00
render.RenderTemplate(writer, pageName)
}
}
// makeLinksHandler returns a handler for links.tmpl with template data from `name`
2024-01-27 19:26:46 -05:00
func makeLinksHandler(name string) func(writer http.ResponseWriter, request *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
page := "links.tmpl"
template, err := render.GetTemplateFromCache(page)
if err != nil {
app.Logger.Error(fmt.Sprintf("couldn't get %s from cache", page), "err", err)
}
data, err := td.LoadTemplateData(name)
if err != nil {
app.Logger.Fatal("couldn't load template data for "+name, "err", err)
} else {
data.StringMap["GeneratedAt"] = template.GeneratedAt.Format(time.UnixDate)
}
app.Logger.Debug("handling a links page", "data", &data)
2024-01-27 19:26:46 -05:00
render.RenderTemplateWithData(writer, page, &data)
}
}