2024-01-22 23:10:14 -05:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-01-24 03:20:07 -05:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2024-01-22 23:10:14 -05:00
|
|
|
"net/http"
|
2024-01-24 03:20:07 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
2024-01-22 23:10:14 -05:00
|
|
|
|
|
|
|
"git.burning.moe/celediel/burning.moe/internal/config"
|
2024-01-24 03:20:07 -05:00
|
|
|
"git.burning.moe/celediel/burning.moe/internal/models"
|
2024-01-22 23:10:14 -05:00
|
|
|
"git.burning.moe/celediel/burning.moe/internal/render"
|
2024-01-24 03:20:07 -05:00
|
|
|
"git.burning.moe/celediel/burning.moe/internal/td"
|
2024-01-22 23:10:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
{
|
|
|
|
Handles: "/about",
|
2024-01-23 00:55:03 -05:00
|
|
|
Handler: makeBasicHandler("about"),
|
2024-01-22 23:10:14 -05:00
|
|
|
},
|
|
|
|
{
|
2024-01-24 03:20:07 -05:00
|
|
|
Handles: "/projects",
|
|
|
|
Handler: makeLinksHandler("projects"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Handles: "/apps",
|
|
|
|
Handler: makeLinksHandler("apps"),
|
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
|
|
|
|
2024-01-24 03:20:07 -05:00
|
|
|
// HomeHandler handles /, generating data from Handlers
|
|
|
|
func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
app.Logger.Info("Got request for homepage")
|
2024-01-24 03:31:37 -05:00
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
2024-01-24 03:31:37 -05:00
|
|
|
} else {
|
|
|
|
d.StringMap = map[string]string{
|
|
|
|
"GeneratedAt": t.GeneratedAt.Format(time.UnixDate),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 03:20:07 -05:00
|
|
|
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)
|
|
|
|
render.RenderTemplateWithData(w, "home.page.tmpl", &d)
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:10:15 -05:00
|
|
|
// makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl
|
2024-01-23 00:55:03 -05:00
|
|
|
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"
|
2024-01-23 19:10:15 -05:00
|
|
|
render.RenderTemplate(w, pageName)
|
2024-01-23 00:55:03 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 03:20:07 -05:00
|
|
|
|
|
|
|
// makeLinksHandler returns a handler for links.tmpl with template data from `name`
|
|
|
|
func makeLinksHandler(name string) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *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)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Logger.Infof("Got request for %s links page", name)
|
|
|
|
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)
|
|
|
|
render.RenderTemplateWithData(w, page, &data)
|
|
|
|
}
|
|
|
|
}
|