2024-01-22 23:10:14 -05:00
|
|
|
package models
|
|
|
|
|
2024-01-23 19:10:15 -05:00
|
|
|
import (
|
2024-01-23 21:14:15 -05:00
|
|
|
"errors"
|
2024-01-23 19:10:15 -05:00
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-01-23 21:14:15 -05:00
|
|
|
|
2024-01-23 21:40:22 -05:00
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
2024-01-23 19:10:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const dataDir string = "./templates/data/"
|
|
|
|
|
2024-01-23 21:14:15 -05:00
|
|
|
// in order of precedence
|
|
|
|
var dataExtensions = [4]string{"yml", "yaml", "toml", "json"}
|
|
|
|
|
2024-01-23 19:10:15 -05:00
|
|
|
type Link struct {
|
2024-01-23 21:14:15 -05:00
|
|
|
Href, Icon template.URL
|
|
|
|
Text string
|
2024-01-23 19:10:15 -05:00
|
|
|
}
|
|
|
|
|
2024-01-22 23:10:14 -05:00
|
|
|
// TemplateData holds data sent from handlers to templates.
|
|
|
|
type TemplateData struct {
|
2024-01-23 21:14:15 -05:00
|
|
|
StringMap map[string]string `json:"StringMap" yaml:"StringMap" toml:"StringMap"`
|
|
|
|
IntMap map[string]int `json:"IntMap" yaml:"IntMap" toml:"IntMap"`
|
|
|
|
FloatMap map[string]float32 `json:"FloatMap" yaml:"FloatMap" toml:"FloatMap"`
|
|
|
|
LinkMap map[string][]Link `json:"LinkMap" yaml:"LinkMap" toml:"LinkMap"`
|
|
|
|
Data map[string]interface{} `json:"Data" yaml:"Data" toml:"Data"`
|
|
|
|
CSRFToken string `json:"Csrftoken" yaml:"Csrftoken" toml:"Csrftoken"`
|
|
|
|
Flash string `json:"Flash" yaml:"Flash" toml:"Flash"`
|
|
|
|
Warning string `json:"Warning" yaml:"Warning" toml:"Warning"`
|
|
|
|
Error string `json:"Error" yaml:"Error" toml:"Error"`
|
2024-01-22 23:10:14 -05:00
|
|
|
}
|
|
|
|
|
2024-01-23 19:10:15 -05:00
|
|
|
// 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) {
|
2024-01-23 21:14:15 -05:00
|
|
|
var data TemplateData
|
|
|
|
output := dataDir + strings.ReplaceAll(page, "tmpl", "")
|
2024-01-23 19:10:15 -05:00
|
|
|
|
2024-01-23 21:14:15 -05:00
|
|
|
for _, extension := range dataExtensions {
|
|
|
|
if info, err := os.Stat(output + extension); err == nil && !info.IsDir() {
|
2024-01-23 21:40:22 -05:00
|
|
|
err = cleanenv.ReadConfig(output+extension, &data)
|
2024-01-23 21:14:15 -05:00
|
|
|
if err == nil {
|
|
|
|
// don't try anymore files
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 19:10:15 -05:00
|
|
|
}
|
|
|
|
|
2024-01-23 21:14:15 -05:00
|
|
|
// couldn't load anything from file
|
|
|
|
return TemplateData{}, errors.New("Couldn't load data from file")
|
|
|
|
}
|