2024-01-22 23:10:14 -05:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.burning.moe/celediel/burning.moe/internal/config"
|
|
|
|
"git.burning.moe/celediel/burning.moe/internal/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
templatesDir string = "./templates/"
|
|
|
|
layoutGlob string = "*.layout.tmpl"
|
|
|
|
pageGlob string = "*.page.tmpl"
|
|
|
|
)
|
|
|
|
|
|
|
|
var app *config.AppConfig
|
|
|
|
|
|
|
|
// Initialise the render package.
|
|
|
|
func Initialise(a *config.AppConfig) {
|
|
|
|
var err error
|
|
|
|
app = a
|
2024-01-23 16:53:59 -05:00
|
|
|
if app.UseCache {
|
|
|
|
app.TemplateCache, err = GenerateNewTemplateCache()
|
|
|
|
}
|
2024-01-22 23:10:14 -05:00
|
|
|
if err != nil {
|
|
|
|
app.Logger.Fatal("Error generating template cache, bailing out!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateNewTemplateCache generates a new template cache.
|
|
|
|
func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
|
|
|
// start with an empty map
|
|
|
|
cache := models.TemplateCache{}
|
|
|
|
cache.Cache = map[string]models.TemplateCacheItem{}
|
|
|
|
|
|
|
|
// Generate a list of pages based on globs
|
|
|
|
pages, err := filepath.Glob(templatesDir + pageGlob)
|
|
|
|
|
|
|
|
// a nice try catch would be pretty cool right about here
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate each page, parsing the file and adding it to the cache
|
|
|
|
for _, page := range pages {
|
|
|
|
name := filepath.Base(page)
|
|
|
|
app.Logger.Info("Generating template " + name)
|
2024-01-23 00:52:42 -05:00
|
|
|
generatedAt := time.Now()
|
2024-01-22 23:10:14 -05:00
|
|
|
|
|
|
|
templateSet, err := template.New(name).ParseFiles(page)
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Glob and parse any layouts found
|
|
|
|
layouts, err := filepath.Glob(templatesDir + layoutGlob)
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(layouts) > 0 {
|
|
|
|
templateSet, err = templateSet.ParseGlob(templatesDir + layoutGlob)
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cache.Cache[name] = models.TemplateCacheItem{
|
|
|
|
Template: templateSet,
|
2024-01-23 00:52:42 -05:00
|
|
|
GeneratedAt: generatedAt,
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
2024-01-23 16:54:25 -05:00
|
|
|
app.Logger.Debugf("Generated %s at %v", name, generatedAt.Format(time.UnixDate))
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// All was good, so return the cache, and no error
|
|
|
|
return cache, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderTemplate renders requested template (t), pulling from cache.
|
2024-01-23 00:55:03 -05:00
|
|
|
func RenderTemplate(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
2024-01-23 17:07:16 -05:00
|
|
|
if !app.UseCache {
|
|
|
|
c, err := GenerateNewTemplateCache()
|
2024-01-22 23:10:14 -05:00
|
|
|
if err != nil {
|
|
|
|
app.Logger.Fatal("Error generating template cache, bailing out!")
|
|
|
|
}
|
2024-01-23 17:07:16 -05:00
|
|
|
app.TemplateCache = c
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get templates from cache
|
2024-01-23 17:07:16 -05:00
|
|
|
template, ok := app.TemplateCache.Cache[filename]
|
2024-01-22 23:10:14 -05:00
|
|
|
if !ok {
|
|
|
|
app.Logger.Fatal(fmt.Sprintf("Couldn't get %s from template cache, bailing out!", filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute templates in a new buffer
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := template.Template.Execute(buf, data)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-23 00:54:33 -05:00
|
|
|
app.Logger.Fatal(fmt.Sprintf("Error executing template %s! Goodbye!", filename), "err", err)
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = buf.WriteTo(w)
|
|
|
|
if err != nil {
|
2024-01-23 00:54:33 -05:00
|
|
|
app.Logger.Error(fmt.Sprintf("Error writing template %s!", filename), "err", err)
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
|
|
|
}
|