Implementierung eines anonymen Counters - Nicht persistent
All checks were successful
release-tag / release-image (push) Successful in 2m9s

This commit is contained in:
2025-05-11 10:57:36 +02:00
parent 847d860144
commit 08ef9c7132
5 changed files with 50 additions and 24 deletions

View File

@@ -48,7 +48,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
COPY --from=build /blog /usr/local/bin/blog
# ─── Content + Assets ───
RUN mkdir -p /content /static /pages /app
RUN mkdir -p /content /static /pages /app /templates /ticks
COPY . /app
COPY --from=content /out/content /content
COPY --from=content /out/static /static
@@ -59,6 +59,7 @@ ENV BLOG_CONTENT_DIR=/content
ENV BLOG_STATIC_DIR=/static
ENV BLOG_PAGES_DIR=/pages
ENV BLOG_TEMPLATES_DIR=/templates
ENV BLOG_TICKS_DIR=/ticks
EXPOSE 8080
CMD ["blog"]

View File

@@ -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, // jetztZeit 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"))
// LISTSeite: 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)
}

View File

@@ -13,6 +13,7 @@ type Article struct {
Cover string
Body template.HTML
Description string
Counter string
}
type ListPage struct {

View File

@@ -127,6 +127,7 @@ footer {
.card-content { padding: 1rem 1.25rem 1.5rem; }
.card h2 { margin: .25rem 0 .5rem; font-size: 1.25rem; line-height: 1.3; }
.card time { color: var(--text-muted); font-size: .85rem; }
.card-count { color: var(--text-muted); font-size: .85rem; }
/* ---------- Artikel ---------- */
.hero {

View File

@@ -11,6 +11,7 @@
<div class="card-content">
<h2>{{ .Title }}</h2>
<time datetime="{{ .Date.Format "2006-01-02" }}">{{ .Date.Format "02.01.2006" }}</time>
<p class="card-count">{{ .Counter }}</p>
</div>
</a>
</li>