clean up models
move template data functions to their own package
This commit is contained in:
parent
dc51b06b51
commit
64b40c3d82
|
@ -1,20 +1,8 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import "html/template"
|
||||||
"errors"
|
|
||||||
"html/template"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ilyakaznacheev/cleanenv"
|
|
||||||
)
|
|
||||||
|
|
||||||
const dataDir string = "./templates/data/"
|
|
||||||
|
|
||||||
// in order of precedence
|
|
||||||
var dataExtensions = [4]string{"yml", "yaml", "toml", "json"}
|
|
||||||
|
|
||||||
|
// Link holds an http link, text to display, and an iconify icon class
|
||||||
type Link struct {
|
type Link struct {
|
||||||
Href, Icon template.URL
|
Href, Icon template.URL
|
||||||
Text string
|
Text string
|
||||||
|
@ -32,36 +20,3 @@ type TemplateData struct {
|
||||||
Warning string `json:"Warning" yaml:"Warning" toml:"Warning"`
|
Warning string `json:"Warning" yaml:"Warning" toml:"Warning"`
|
||||||
Error string `json:"Error" yaml:"Error" toml:"Error"`
|
Error string `json:"Error" yaml:"Error" toml:"Error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
|
||||||
var data TemplateData
|
|
||||||
output := dataDir + strings.ReplaceAll(page, "tmpl", "")
|
|
||||||
|
|
||||||
for _, extension := range dataExtensions {
|
|
||||||
if info, err := os.Stat(output + extension); err == nil && !info.IsDir() {
|
|
||||||
err = cleanenv.ReadConfig(output+extension, &data)
|
|
||||||
if err == nil {
|
|
||||||
// don't try anymore files
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// couldn't load anything from file
|
|
||||||
return TemplateData{}, errors.New("Couldn't load data from file")
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"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"
|
||||||
|
"git.burning.moe/celediel/burning.moe/internal/td"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -103,7 +104,7 @@ func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get template data from file, or generate simple
|
// Get template data from file, or generate simple
|
||||||
data, err := models.LoadTemplateData(filename)
|
data, err := td.LoadTemplateData(filename)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
app.Logger.Debug(fmt.Sprintf("Loaded data for template %s.", filename), "data", data)
|
app.Logger.Debug(fmt.Sprintf("Loaded data for template %s.", filename), "data", data)
|
||||||
if _, ok := data.StringMap["GeneratedAt"]; !ok {
|
if _, ok := data.StringMap["GeneratedAt"]; !ok {
|
||||||
|
@ -111,7 +112,7 @@ func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err)
|
app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err)
|
||||||
data = models.MakeBasicTemplateData(template.GeneratedAt)
|
data = td.MakeBasicTemplateData(template.GeneratedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute templates in a new buffer
|
// Execute templates in a new buffer
|
||||||
|
|
50
internal/td/td.go
Normal file
50
internal/td/td.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package td
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.burning.moe/celediel/burning.moe/internal/models"
|
||||||
|
|
||||||
|
"github.com/ilyakaznacheev/cleanenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const dataDir string = "./templates/data/"
|
||||||
|
|
||||||
|
// in order of precedence
|
||||||
|
var dataExtensions = [4]string{"yml", "yaml", "toml", "json"}
|
||||||
|
|
||||||
|
// makeBasicTemplateData creates a blank TemplateData containing only the
|
||||||
|
// time the related template was generated
|
||||||
|
func MakeBasicTemplateData(when time.Time) models.TemplateData {
|
||||||
|
strMap := map[string]string{
|
||||||
|
"GeneratedAt": when.Format(time.UnixDate),
|
||||||
|
}
|
||||||
|
|
||||||
|
templateData := models.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) (models.TemplateData, error) {
|
||||||
|
var data models.TemplateData
|
||||||
|
output := dataDir + strings.ReplaceAll(page, "tmpl", "")
|
||||||
|
|
||||||
|
for _, extension := range dataExtensions {
|
||||||
|
if info, err := os.Stat(output + extension); err == nil && !info.IsDir() {
|
||||||
|
err = cleanenv.ReadConfig(output+extension, &data)
|
||||||
|
if err == nil {
|
||||||
|
// don't try anymore files
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// couldn't load anything from file
|
||||||
|
return models.TemplateData{}, errors.New("Couldn't load data from file")
|
||||||
|
}
|
Loading…
Reference in a new issue