hikan.ru/mvc/models/app.go

59 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package models
import (
"html/template"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// App хранит информацию о приложении
type App struct {
Templates *template.Template // Шаблоны страниц
Cache *Cache // Кэш (отрендеренные странички)
Version int64 // Время запуска
}
// Инициализирует приложение
func AppInit() (*App, error) {
a := &App{
Version: time.Now().Unix(),
Cache: CacheInit(),
}
// Загрузка шаблонов
if err := a.loadTemplates(Cfg.TemplatesDir, Cfg.TemplatesExt); err != nil {
log.Fatal(err)
}
return a, nil
}
// Загрузка шаблонов
func (a *App) loadTemplates(templatesPath string, ext string) error {
tmpls := template.New("")
err := filepath.Walk(templatesPath, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() && strings.HasSuffix(f.Name(), ext) {
_, err = tmpls.ParseFiles(path)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
a.Templates = tmpls
return nil
}