Total visitor and page visits statistics page.

This commit is contained in:
Marvin Blum
2020-06-24 17:47:36 +02:00
committed by Marvin Blum
parent e5eee70815
commit f93b85e6f4
8 changed files with 240 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ func NewCache() *Cache {
cache: make(map[string][]byte),
hotReload: os.Getenv("MB_HOT_RELOAD") == "true",
}
logbuch.Debug("Template cache hot reload", logbuch.Fields{"hot_reload": cache.hotReload})
cache.load()
return cache
}
@@ -57,6 +58,12 @@ func (cache *Cache) Render(w http.ResponseWriter, name string, data interface{})
cache.m.Lock()
defer cache.m.Unlock()
logbuch.Debug("Rendering template", logbuch.Fields{"name": name})
if cache.hotReload {
logbuch.Debug("Reloading templates")
cache.load()
}
var buffer bytes.Buffer
if err := cache.tpl.ExecuteTemplate(&buffer, name, data); err != nil {
@@ -75,6 +82,18 @@ func (cache *Cache) Render(w http.ResponseWriter, name string, data interface{})
}
}
func (cache *Cache) RenderWithoutCache(w http.ResponseWriter, name string, data interface{}) {
if cache.hotReload {
logbuch.Debug("Reloading templates")
cache.load()
}
if err := cache.tpl.ExecuteTemplate(w, name, data); err != nil {
logbuch.Error("Error executing template", logbuch.Fields{"err": err, "name": name})
w.WriteHeader(http.StatusInternalServerError)
}
}
func (cache *Cache) Clear() {
cache.m.Lock()
defer cache.m.Unlock()