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
|
2024-01-27 19:26:46 -05:00
|
|
|
Handler func(writer http.ResponseWriter, request *http.Request)
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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: "/apps",
|
|
|
|
Handler: makeLinksHandler("apps"),
|
2024-01-22 23:10:14 -05:00
|
|
|
},
|
2024-01-27 19:27:13 -05:00
|
|
|
{
|
|
|
|
Handles: "/projects",
|
|
|
|
Handler: makeLinksHandler("projects"),
|
|
|
|
},
|
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
|
2024-01-27 19:26:46 -05:00
|
|
|
func HomeHandler(writer http.ResponseWriter, request *http.Request) {
|
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)
|
2024-01-27 19:26:46 -05:00
|
|
|
render.RenderTemplateWithData(writer, "home.page.tmpl", &d)
|
2024-01-24 03:20:07 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-01-27 19:52:18 -05:00
|
|
|
var robots strings.Builder
|
|
|
|
robots.WriteString(fmt.Sprintf("User-agent: %s\nAllow: /\n", request.UserAgent()))
|
2024-01-27 19:28:03 -05:00
|
|
|
|
|
|
|
for _, handler := range Handlers {
|
2024-01-27 19:52:18 -05:00
|
|
|
robots.WriteString(fmt.Sprintf("Allow: %s\n", handler.Handles))
|
2024-01-27 19:28:03 -05:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:52:18 -05:00
|
|
|
robots.WriteString("Disallow: /*\n")
|
2024-01-27 19:28:03 -05:00
|
|
|
|
2024-01-27 19:52:18 -05:00
|
|
|
fmt.Fprint(writer, robots.String())
|
2024-01-27 19:28:03 -05:00
|
|
|
}
|
|
|
|
|
2024-01-23 19:10:15 -05:00
|
|
|
// 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) {
|
2024-01-23 00:55:03 -05:00
|
|
|
pageName := name + ".page.tmpl"
|
2024-01-27 19:26:46 -05:00
|
|
|
render.RenderTemplate(writer, 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`
|
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) {
|
2024-01-24 03:20:07 -05:00
|
|
|
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)
|
2024-01-24 03:20:07 -05:00
|
|
|
}
|
|
|
|
}
|