Implementierung eines anonymen Counters - Nicht persistent
All checks were successful
release-tag / release-image (push) Successful in 2m9s
All checks were successful
release-tag / release-image (push) Successful in 2m9s
This commit is contained in:
@@ -12,6 +12,35 @@ import (
|
||||
"git.send.nrw/sendnrw/b1tsblog/internal/article"
|
||||
)
|
||||
|
||||
var TickCatalog []TickEntry
|
||||
|
||||
func IncTick(xSlug string) error {
|
||||
for a, b := range TickCatalog {
|
||||
if b.Slug == xSlug {
|
||||
TickCatalog[a].Count = TickCatalog[a].Count + 1
|
||||
return nil
|
||||
}
|
||||
}
|
||||
newEntry := TickEntry{Slug: xSlug, Count: 1}
|
||||
TickCatalog = append(TickCatalog, newEntry)
|
||||
return fmt.Errorf("")
|
||||
}
|
||||
|
||||
func getTick(xSlug string) string {
|
||||
for _, b := range TickCatalog {
|
||||
if b.Slug == xSlug {
|
||||
var n int64 = b.Count
|
||||
return strconv.FormatInt(n, 10)
|
||||
}
|
||||
}
|
||||
return "-1"
|
||||
}
|
||||
|
||||
type TickEntry struct {
|
||||
Slug string
|
||||
Count int64
|
||||
}
|
||||
|
||||
func getenv(k, d string) string {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
@@ -42,35 +71,17 @@ var (
|
||||
func main() {
|
||||
|
||||
// --- Verzeichnisse konfigurierbar machen -------------------------
|
||||
contentDir := os.Getenv("BLOG_CONTENT_DIR")
|
||||
if contentDir == "" {
|
||||
contentDir = "/content" // Fallback für local run
|
||||
}
|
||||
|
||||
staticDir := os.Getenv("BLOG_STATIC_DIR")
|
||||
if staticDir == "" {
|
||||
staticDir = "/app/internal/web/static"
|
||||
}
|
||||
|
||||
pagesDir := os.Getenv("BLOG_PAGES_DIR")
|
||||
if staticDir == "" {
|
||||
staticDir = "/pages"
|
||||
}
|
||||
|
||||
templatesDir := os.Getenv("BLOG_TEMPLATES_DIR")
|
||||
if templatesDir == "" {
|
||||
templatesDir = "/templates"
|
||||
}
|
||||
contentDir := getenv("BLOG_CONTENT_DIR", "/content")
|
||||
staticDir := getenv("BLOG_STATIC_DIR", "/app/internal/web/static")
|
||||
pagesDir := getenv("BLOG_PAGES_DIR", "/pages")
|
||||
templatesDir := getenv("BLOG_TEMPLATES_DIR", "/templates")
|
||||
|
||||
funcs := template.FuncMap{
|
||||
"now": time.Now, // jetzt‑Zeit bereitstellen
|
||||
}
|
||||
|
||||
// Basislayout zuerst parsen
|
||||
layout := template.Must(
|
||||
template.New("base").Funcs(funcs).
|
||||
ParseFiles(templatesDir + "/base.html"),
|
||||
)
|
||||
layout := template.Must(template.New("base").Funcs(funcs).ParseFiles(templatesDir + "/base.html"))
|
||||
|
||||
// LIST‑Seite: base + list.html
|
||||
tplList = template.Must(layout.Clone())
|
||||
@@ -90,6 +101,14 @@ func main() {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
/* */
|
||||
|
||||
for a, b := range articles {
|
||||
articles[a].Counter = getTick(b.Slug)
|
||||
}
|
||||
|
||||
/* */
|
||||
|
||||
staticPages, err := article.LoadStatic(pagesDir)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -116,6 +135,9 @@ func main() {
|
||||
slug := strings.TrimPrefix(r.URL.Path, "/post/")
|
||||
for _, a := range articles {
|
||||
if a.Slug == slug {
|
||||
IncTick(slug)
|
||||
t := getTick(slug)
|
||||
a.Counter = t
|
||||
if err := tplArticle.ExecuteTemplate(w, "layout", a); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
}
|
||||
|
Reference in New Issue
Block a user