Compare commits
6 commits
a7f6bb35e7
...
33b1b850de
Author | SHA1 | Date | |
---|---|---|---|
Lilian Jónsdóttir | 33b1b850de | ||
Lilian Jónsdóttir | 7544f60e74 | ||
Lilian Jónsdóttir | e17e186c1d | ||
Lilian Jónsdóttir | 4f97975986 | ||
Lilian Jónsdóttir | f3f3f4406c | ||
Lilian Jónsdóttir | 7bfeb5f940 |
16
README.md
16
README.md
|
@ -1,3 +1,17 @@
|
||||||
# burning.moe
|
# burning.moe
|
||||||
|
|
||||||
Code for the burning.moe homepage
|
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.
|
||||||
|
|
|
@ -2,6 +2,7 @@ package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.burning.moe/celediel/burning.moe/internal/config"
|
"git.burning.moe/celediel/burning.moe/internal/config"
|
||||||
"git.burning.moe/celediel/burning.moe/internal/models"
|
"git.burning.moe/celediel/burning.moe/internal/models"
|
||||||
|
@ -21,18 +22,12 @@ var Handlers = []Handler{
|
||||||
// /about
|
// /about
|
||||||
{
|
{
|
||||||
Handles: "/about",
|
Handles: "/about",
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
Handler: makeBasicHandler("about"),
|
||||||
app.Logger.Info("Got request for about page.")
|
|
||||||
render.RenderTemplate(w, "about.page", &models.TemplateData{})
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
// / comes last
|
// / comes last
|
||||||
{
|
{
|
||||||
Handles: "/",
|
Handles: "/",
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
Handler: makeBasicHandler("home"),
|
||||||
app.Logger.Info("Got request for homepage.")
|
|
||||||
render.RenderTemplate(w, "home.page", &models.TemplateData{})
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,3 +35,32 @@ var Handlers = []Handler{
|
||||||
func Initialise(a *config.AppConfig) {
|
func Initialise(a *config.AppConfig) {
|
||||||
app = a
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
name := filepath.Base(page)
|
name := filepath.Base(page)
|
||||||
app.Logger.Info("Generating template " + name)
|
app.Logger.Info("Generating template " + name)
|
||||||
|
generatedAt := time.Now()
|
||||||
|
|
||||||
templateSet, err := template.New(name).ParseFiles(page)
|
templateSet, err := template.New(name).ParseFiles(page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -68,8 +69,9 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
||||||
}
|
}
|
||||||
cache.Cache[name] = models.TemplateCacheItem{
|
cache.Cache[name] = models.TemplateCacheItem{
|
||||||
Template: templateSet,
|
Template: templateSet,
|
||||||
GeneratedAt: time.Now(),
|
GeneratedAt: generatedAt,
|
||||||
}
|
}
|
||||||
|
app.Logger.Debugf("Generated %s at %v", name, generatedAt.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// All was good, so return the cache, and no error
|
// All was good, so return the cache, and no error
|
||||||
|
@ -77,8 +79,7 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderTemplate renders requested template (t), pulling from cache.
|
// RenderTemplate renders requested template (t), pulling from cache.
|
||||||
func RenderTemplate(w http.ResponseWriter, t string, data *models.TemplateData) {
|
func RenderTemplate(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
||||||
filename := t + ".tmpl"
|
|
||||||
var cache models.TemplateCache
|
var cache models.TemplateCache
|
||||||
if app.UseCache {
|
if app.UseCache {
|
||||||
cache = app.TemplateCache
|
cache = app.TemplateCache
|
||||||
|
@ -88,6 +89,7 @@ func RenderTemplate(w http.ResponseWriter, t string, data *models.TemplateData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||||
}
|
}
|
||||||
|
app.TemplateCache = cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get templates from cache
|
// Get templates from cache
|
||||||
|
@ -101,11 +103,11 @@ func RenderTemplate(w http.ResponseWriter, t string, data *models.TemplateData)
|
||||||
err := template.Template.Execute(buf, data)
|
err := template.Template.Execute(buf, data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Fatal("Error executing template %s! Goodbye!", "err", err)
|
app.Logger.Fatal(fmt.Sprintf("Error executing template %s! Goodbye!", filename), "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = buf.WriteTo(w)
|
_, err = buf.WriteTo(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Error writing template %s!\n", "err", err)
|
app.Logger.Error(fmt.Sprintf("Error writing template %s!", filename), "err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,11 @@
|
||||||
{{- block "js" . }}
|
{{- block "js" . }}
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
<div id="leftfooter">
|
<div id="leftfooter">
|
||||||
note: please do not set fire to cute girls
|
template generated on {{index .StringMap "GeneratedAt"}}
|
||||||
</div>
|
</div>
|
||||||
<div id="rightfooter">
|
<div id="rightfooter">
|
||||||
|
note: please do not set fire to cute girls
|
||||||
|
<br />
|
||||||
powered by
|
powered by
|
||||||
<a href="https://www.debian.org/">debian</a>
|
<a href="https://www.debian.org/">debian</a>
|
||||||
and
|
and
|
||||||
|
|
Loading…
Reference in a new issue