Compare commits
No commits in common. "8a2319033eb29e4931a72910e41116c0d850b155" and "b9c0bd863b48f790a51d5764b3e91e66f9f00378" have entirely different histories.
8a2319033e
...
b9c0bd863b
|
@ -2,8 +2,10 @@ package handlers
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -34,11 +36,33 @@ func Initialise(a *config.AppConfig) {
|
|||
app = a
|
||||
}
|
||||
|
||||
// makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl
|
||||
// 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 cacheItem, ok := app.TemplateCache.Cache[name]; ok {
|
||||
strMap = map[string]string{
|
||||
"GeneratedAt": cacheItem.GeneratedAt.Format(time.UnixDate),
|
||||
}
|
||||
} else {
|
||||
strMap = map[string]string{
|
||||
"GeneratedAt": time.Now().Format(time.UnixDate),
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
render.RenderTemplate(w, pageName)
|
||||
templateData := makeBasicTemplateData(pageName)
|
||||
render.RenderTemplate(w, pageName, &templateData)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,63 +1,14 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const dataDir string = "./templates/data/"
|
||||
|
||||
type Link struct {
|
||||
Href template.URL
|
||||
Text string
|
||||
Icon template.URL
|
||||
}
|
||||
|
||||
// TemplateData holds data sent from handlers to templates.
|
||||
type TemplateData struct {
|
||||
StringMap map[string]string `json:"stringMap"`
|
||||
IntMap map[string]int `json:"intMap"`
|
||||
FloatMap map[string]float32 `json:"floatMap"`
|
||||
LinkMap map[string][]Link `json:"linkMap"`
|
||||
Data map[string]interface{} `json:"data"`
|
||||
CSRFToken string `json:"csrftoken"`
|
||||
Flash string `json:"flash"`
|
||||
Warning string `json:"warning"`
|
||||
Error string `json:"error"`
|
||||
StringMap map[string]string
|
||||
IntMap map[string]int
|
||||
FloatMap map[string]float32
|
||||
Data map[string]interface{}
|
||||
CSRFToken string
|
||||
Flash string
|
||||
Warning string
|
||||
Error string
|
||||
}
|
||||
|
||||
// makeBasicTemplateData creates a blank TemplateData containing only the
|
||||
// time the related template was generated
|
||||
func MakeBasicTemplateData(when time.Time) TemplateData {
|
||||
strMap := map[string]string{
|
||||
"GeneratedAt": when.Format(time.UnixDate),
|
||||
}
|
||||
|
||||
templateData := TemplateData{
|
||||
StringMap: strMap,
|
||||
}
|
||||
return templateData
|
||||
}
|
||||
|
||||
// LoadTemplateData loads template data from file. If that
|
||||
// fails, it returns an empty TemplateData and an error
|
||||
func LoadTemplateData(page string) (TemplateData, error) {
|
||||
// TODO: load from something other than JSON
|
||||
var data *TemplateData
|
||||
output := dataDir + strings.ReplaceAll(page, "tmpl", "json")
|
||||
|
||||
file, err := os.ReadFile(output)
|
||||
if err != nil {
|
||||
return TemplateData{}, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(file, &data)
|
||||
if err != nil {
|
||||
return TemplateData{}, err
|
||||
}
|
||||
|
||||
return *data, nil
|
||||
}
|
||||
|
|
|
@ -24,9 +24,7 @@ var app *config.AppConfig
|
|||
func Initialise(a *config.AppConfig) {
|
||||
var err error
|
||||
app = a
|
||||
if app.UseCache {
|
||||
app.TemplateCache, err = GenerateNewTemplateCache()
|
||||
}
|
||||
app.TemplateCache, err = GenerateNewTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||
}
|
||||
|
@ -73,7 +71,7 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
|||
Template: templateSet,
|
||||
GeneratedAt: generatedAt,
|
||||
}
|
||||
app.Logger.Debugf("Generated %s at %v", name, generatedAt.Format(time.UnixDate))
|
||||
app.Logger.Debugf("Generated %s at %v", name, generatedAt.String())
|
||||
}
|
||||
|
||||
// All was good, so return the cache, and no error
|
||||
|
@ -81,44 +79,29 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
|||
}
|
||||
|
||||
// RenderTemplate renders requested template (t), pulling from cache.
|
||||
func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||
if !app.UseCache {
|
||||
c, err := GenerateNewTemplateCache()
|
||||
func RenderTemplate(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
||||
var cache models.TemplateCache
|
||||
if app.UseCache {
|
||||
cache = app.TemplateCache
|
||||
} else {
|
||||
var err error
|
||||
cache, err = GenerateNewTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||
}
|
||||
app.TemplateCache = c
|
||||
app.TemplateCache = cache
|
||||
}
|
||||
|
||||
// Get templates from cache
|
||||
template, ok := app.TemplateCache.Cache[filename]
|
||||
template, ok := cache.Cache[filename]
|
||||
if !ok {
|
||||
app.Logger.Errorf("Couldn't get %s from template cache, dunno what happened, but we're gonna generate a new one", filename)
|
||||
c, err := GenerateNewTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||
}
|
||||
app.TemplateCache = c
|
||||
template = app.TemplateCache.Cache[filename]
|
||||
app.Logger.Fatal(fmt.Sprintf("Couldn't get %s from template cache, bailing out!", filename))
|
||||
}
|
||||
|
||||
// Get template data from file, or generate simple
|
||||
data, err := models.LoadTemplateData(filename)
|
||||
if err == nil {
|
||||
app.Logger.Debug(fmt.Sprintf("Loaded data for template %s.", filename), "data", data)
|
||||
if _, ok := data.StringMap["GeneratedAt"]; !ok {
|
||||
data.StringMap["GeneratedAt"] = app.TemplateCache.Cache[filename].GeneratedAt.Format(time.UnixDate)
|
||||
}
|
||||
} else {
|
||||
app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err)
|
||||
data = models.MakeBasicTemplateData(template.GeneratedAt)
|
||||
}
|
||||
l := data.LinkMap["Personal"]
|
||||
app.Logger.Debugf("%[1]v is %[1]T", l)
|
||||
|
||||
// Execute templates in a new buffer
|
||||
buf := new(bytes.Buffer)
|
||||
err = template.Template.Execute(buf, data)
|
||||
err := template.Template.Execute(buf, data)
|
||||
|
||||
if err != nil {
|
||||
app.Logger.Fatal(fmt.Sprintf("Error executing template %s! Goodbye!", filename), "err", err)
|
||||
}
|
||||
|
|
|
@ -4,27 +4,46 @@
|
|||
<span id="words">
|
||||
<h4>celediel</h4>
|
||||
<span id="bigwords">
|
||||
{{ index .StringMap "about" }}
|
||||
she/her, 1989, queer anarchist, self-taught aspiring developer
|
||||
</span>
|
||||
<h4>links</h4>
|
||||
|
||||
<!-- personal links -->
|
||||
<h4>links</h4>
|
||||
{{ range (index .LinkMap "Personal") }}
|
||||
<a href="{{ .Href }}">
|
||||
<span class="iconify" data-icon="{{ .Icon }}"></span>
|
||||
{{ .Text }}
|
||||
<a href="https://matrix.to/#/@celediel:burning.moe">
|
||||
<span class="iconify" data-icon="tabler:brand-matrix"></span>
|
||||
matrix
|
||||
</a><br />
|
||||
|
||||
<a href="https://slrpnk.net/u/Celediel">
|
||||
<span class="iconify" data-icon="simple-icons:lemmy"></span>
|
||||
lemmy
|
||||
</a><br />
|
||||
|
||||
<a href="https://git.burning.moe/celediel">
|
||||
<span class="iconify" data-icon="mdi:git"></span>
|
||||
self-hosted git
|
||||
</a><br />
|
||||
|
||||
<a href="https://github.com/celediel">
|
||||
<span class="iconify" data-icon="bi:github"></span>
|
||||
github
|
||||
</a><br />
|
||||
{{ end }}
|
||||
|
||||
<!-- web app links -->
|
||||
<h4>hosted apps</h4>
|
||||
{{ range (index .LinkMap "HostedApps") }}
|
||||
<a href="{{ .Href }}">
|
||||
<span class="iconify" data-icon="{{ .Icon }}"></span>
|
||||
{{ .Text }}
|
||||
<a href="https://git.burning.moe/">
|
||||
<span class="iconify" data-icon="mdi:git"></span>
|
||||
self-hosted git
|
||||
</a><br />
|
||||
{{ end }}
|
||||
<br />
|
||||
<a href="https://bin.burning.moe/">
|
||||
<span class="iconify" data-icon="fluent:bin-recycle-20-filled"></span>
|
||||
wastebin
|
||||
</a><br />
|
||||
<a href="https://gist.burning.moe/">
|
||||
<span class="iconify" data-icon="carbon:paste"></span>
|
||||
gist
|
||||
</a>
|
||||
<br /><br />
|
||||
<a href="/">back</a>
|
||||
</span>
|
||||
{{ end -}}
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
{
|
||||
"StringMap": {
|
||||
"about": "she/her, 1989, queer anarchist, self-taught aspiring developer and sysadmin"
|
||||
},
|
||||
"LinkMap": {
|
||||
"Personal": [
|
||||
{
|
||||
"href": "https://matrix.to/#/@celediel:burning.moe",
|
||||
"text": "matrix",
|
||||
"icon": "tabler:brand-matrix"
|
||||
},
|
||||
{
|
||||
"href": "https://slrpnk.net/u/Celediel",
|
||||
"text": "lemmy",
|
||||
"icon": "simple-icons:lemmy"
|
||||
},
|
||||
{
|
||||
"href": "https://git.burning.moe/celediel",
|
||||
"text": "self-hosted git",
|
||||
"icon": "mdi:git"
|
||||
},
|
||||
{
|
||||
"href": "https://github.com/celediel",
|
||||
"text": "github",
|
||||
"icon": "bi:github"
|
||||
}
|
||||
],
|
||||
"HostedApps": [
|
||||
{
|
||||
"href": "https://git.burning.moe/",
|
||||
"text": "self-hosted git",
|
||||
"icon": "mdi:git"
|
||||
},
|
||||
{
|
||||
"href": "https://bin.burning.moe/",
|
||||
"text": "wastebin",
|
||||
"icon": "fluent:bin-recycle-20-filled"
|
||||
},
|
||||
{
|
||||
"href": "https://gist.burning.moe/",
|
||||
"text": "gist",
|
||||
"icon": "carbon:paste"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue