9
"github.com/go-chi/chi/v5"
10
"xelbot.com/reprogl/container"
11
"xelbot.com/reprogl/models/repositories"
12
"xelbot.com/reprogl/views"
15
func CategoriesFragment(app *container.Application) http.HandlerFunc {
16
return func(w http.ResponseWriter, _ *http.Request) {
17
categoryRepo := repositories.CategoryRepository{DB: app.DB}
18
categories, err := categoryRepo.GetCategoryTree()
20
app.ServerError(w, err)
25
templateData := &views.FragmentCategoriesData{Categories: categories}
27
cacheControl(w, container.DefaultEsiTTL)
28
err = views.WriteTemplate(w, "categories.gohtml", templateData)
30
app.ServerError(w, err)
35
func RecentPostsFragment(app *container.Application) http.HandlerFunc {
36
return func(w http.ResponseWriter, r *http.Request) {
37
articleId, err := strconv.Atoi(chi.URLParam(r, "article_id"))
39
app.ServerError(w, err)
44
repo := repositories.ArticleRepository{DB: app.DB}
45
articles, err := repo.GetRecentPostsCollection(articleId)
47
app.ServerError(w, err)
52
templateData := &views.FragmentRecentPostsData{RecentPosts: articles}
54
cacheControl(w, container.DefaultEsiTTL)
55
err = views.WriteTemplate(w, "recent-posts.gohtml", templateData)
57
app.ServerError(w, err)
62
func DaysOfWarCounter(w http.ResponseWriter, r *http.Request) {
63
start, _ := time.ParseInLocation("2006-01-02", "2022-02-24", time.Local)
66
days := 1 + (now.Unix()-start.Unix())/(24*3600)
67
response := fmt.Sprintf("<!-- War in Ukraine: Thu, 24 Feb 2022 05:00:00 +0200, %d days of war -->", days)
69
w.Header().Set("Cache-Control", "public")
71
expires, _ := time.ParseInLocation(time.DateTime, now.Format(time.DateOnly)+" 23:59:59", time.Local)
72
setExpires(w, expires)
73
w.Header().Set("Content-Type", "text/plain")
74
w.Write([]byte(response))