Compare commits

..

No commits in common. "33b1b850de94a12c683d902f7bfa003cb8641d3b" and "a7f6bb35e71d674b3a9098a9815a0117107880e9" have entirely different histories.

4 changed files with 15 additions and 57 deletions

View file

@ -1,17 +1,3 @@
# burning.moe
Code for the [burning.moe](https://burning.moe) homepage
## technologies used
- [go](https://go.dev)
- [chi](https://github.com/go-chi/chi)
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
- [mage](https://github.com/magefile/mage)
## why?
why not?
Improving my go skills, and learning to write web apps in go, was a
good opportunity to rewrite my ancient and basic website while having
it functionally not change at all.
Code for the burning.moe homepage

View file

@ -2,7 +2,6 @@ package handlers
import (
"net/http"
"time"
"git.burning.moe/celediel/burning.moe/internal/config"
"git.burning.moe/celediel/burning.moe/internal/models"
@ -22,12 +21,18 @@ var Handlers = []Handler{
// /about
{
Handles: "/about",
Handler: makeBasicHandler("about"),
Handler: func(w http.ResponseWriter, r *http.Request) {
app.Logger.Info("Got request for about page.")
render.RenderTemplate(w, "about.page", &models.TemplateData{})
},
},
// / comes last
{
Handles: "/",
Handler: makeBasicHandler("home"),
Handler: func(w http.ResponseWriter, r *http.Request) {
app.Logger.Info("Got request for homepage.")
render.RenderTemplate(w, "home.page", &models.TemplateData{})
},
},
}
@ -35,32 +40,3 @@ var Handlers = []Handler{
func Initialise(a *config.AppConfig) {
app = a
}
// 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)
}
}

View file

@ -48,7 +48,6 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
for _, page := range pages {
name := filepath.Base(page)
app.Logger.Info("Generating template " + name)
generatedAt := time.Now()
templateSet, err := template.New(name).ParseFiles(page)
if err != nil {
@ -69,9 +68,8 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
}
cache.Cache[name] = models.TemplateCacheItem{
Template: templateSet,
GeneratedAt: generatedAt,
GeneratedAt: time.Now(),
}
app.Logger.Debugf("Generated %s at %v", name, generatedAt.String())
}
// All was good, so return the cache, and no error
@ -79,7 +77,8 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
}
// RenderTemplate renders requested template (t), pulling from cache.
func RenderTemplate(w http.ResponseWriter, filename string, data *models.TemplateData) {
func RenderTemplate(w http.ResponseWriter, t string, data *models.TemplateData) {
filename := t + ".tmpl"
var cache models.TemplateCache
if app.UseCache {
cache = app.TemplateCache
@ -89,7 +88,6 @@ func RenderTemplate(w http.ResponseWriter, filename string, data *models.Templat
if err != nil {
app.Logger.Fatal("Error generating template cache, bailing out!")
}
app.TemplateCache = cache
}
// Get templates from cache
@ -103,11 +101,11 @@ func RenderTemplate(w http.ResponseWriter, filename string, data *models.Templat
err := template.Template.Execute(buf, data)
if err != nil {
app.Logger.Fatal(fmt.Sprintf("Error executing template %s! Goodbye!", filename), "err", err)
app.Logger.Fatal("Error executing template %s! Goodbye!", "err", err)
}
_, err = buf.WriteTo(w)
if err != nil {
app.Logger.Error(fmt.Sprintf("Error writing template %s!", filename), "err", err)
app.Logger.Error("Error writing template %s!\n", "err", err)
}
}

View file

@ -20,11 +20,9 @@
{{- block "js" . }}
{{ end -}}
<div id="leftfooter">
template generated on {{index .StringMap "GeneratedAt"}}
note: please do not set fire to cute girls
</div>
<div id="rightfooter">
note: please do not set fire to cute girls
<br />
powered by
<a href="https://www.debian.org/">debian</a>
and