summaryrefslogtreecommitdiff
path: root/internal/templatemanager
diff options
from:
to:
context:
space:
mode:
authorGravatar Saya Andy <145215889+SayaAndy@users.noreply.github.com> 2026-03-31 00:54:20 +0700
committerGravatar GitHub <noreply@github.com> 2026-03-31 00:54:20 +0700
commitabc9e2fc1e93fba0b274f31b95340c411a73dd6d (patch)
tree3d0b70016ce8f160203629da07c1a8ae3edff8af /internal/templatemanager
parent8d0385546b0a5f4f3b0832fce1b4c0a10fa0a927 (diff)
parenta8f566cbeb8358aca7fb4532d8b69c513ef197f3 (diff)
downloadweb-0.13.0.tar.gz
web-0.13.0.zip
v0.13.0 (#25)v0.13.0
- [feat: enable etag middleware](https://github.com/SayaAndy/saya-today-web/commit/c7742b341a46360d16dd9035c046f004d0fe760c) - feat: enable compress middleware - feat: trim end slash of each request url (except for root) - [feat: add linked data](https://github.com/SayaAndy/saya-today-web/commit/a8f566cbeb8358aca7fb4532d8b69c513ef197f3) - [feat: build sitemap.xml](https://github.com/SayaAndy/saya-today-web/commit/7a40c01dd14f04787f5052d0c89e0d6fb9ce597f) - [feat: generate robots.txt](https://github.com/SayaAndy/saya-today-web/commit/be906e1cc4ec3e92bcb14ee08256b53f3a4b44e2) - [feat: specify canonical hrefs for each page](https://github.com/SayaAndy/saya-today-web/commit/a0ace41ad3b0f4661e98d8fa8ef254369cc80d1e) - [feat: use semantic html tags for pages](https://github.com/SayaAndy/saya-today-web/commit/2bcf8ee39555ea98be309b85547c797b0d172b84) - feat: specify more meta properties for search engines - fix: correct user and catalogue meta - format: specify og property names in property field - feat: user profile description
Diffstat (limited to 'internal/templatemanager')
-rw-r--r--internal/templatemanager/templatemanager.go50
1 files changed, 44 insertions, 6 deletions
diff --git a/internal/templatemanager/templatemanager.go b/internal/templatemanager/templatemanager.go
index b23e2e8..9d14e15 100644
--- a/internal/templatemanager/templatemanager.go
+++ b/internal/templatemanager/templatemanager.go
@@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"html/template"
+ "os"
"path/filepath"
"strings"
+ "time"
)
type TemplateManager struct {
@@ -13,8 +15,9 @@ type TemplateManager struct {
}
type templateManagerRender struct {
- Main string
- Tmpl *template.Template
+ Main string
+ Tmpl *template.Template
+ LastModified time.Time
}
type TemplateManagerTemplates struct {
@@ -53,10 +56,15 @@ func NewTemplateManager(templates ...TemplateManagerTemplates) (*TemplateManager
if err != nil {
return nil, err
}
+ modTime, err := setLastModified(tmplStruct.Files...)
+ if err != nil {
+ return nil, err
+ }
templateMap[tmplStruct.Name] = templateManagerRender{
- Main: filepath.Base(tmplStruct.Files[0]),
- Tmpl: tmpl,
+ Main: filepath.Base(tmplStruct.Files[0]),
+ Tmpl: tmpl,
+ LastModified: modTime,
}
}
@@ -114,10 +122,40 @@ func (tm *TemplateManager) Add(name string, files ...string) error {
return fmt.Errorf("failed to add template into manager: %w", err)
}
+ modTime, err := setLastModified(files...)
+ if err != nil {
+ return fmt.Errorf("failed to add template into manager: %w", err)
+ }
+
tm.templates[name] = templateManagerRender{
- Main: filepath.Base(files[0]),
- Tmpl: tmpl,
+ Main: filepath.Base(files[0]),
+ Tmpl: tmpl,
+ LastModified: modTime,
}
return nil
}
+
+func (tm *TemplateManager) GetLastModified(name string) (time.Time, error) {
+ tmpl, exists := tm.templates[name]
+ if !exists {
+ return time.Time{}, fmt.Errorf("template %s not found", name)
+ }
+
+ return tmpl.LastModified, nil
+}
+
+func setLastModified(filenames ...string) (time.Time, error) {
+ lastModified := time.Time{}
+ for _, filename := range filenames {
+ stat, err := os.Stat(filename)
+ if err != nil {
+ return lastModified, fmt.Errorf("failed to stat file: path '%s': %w", filename, err)
+ }
+ modTime := stat.ModTime()
+ if lastModified.Before(modTime) {
+ lastModified = modTime
+ }
+ }
+ return lastModified, nil
+}