141 lines
3.5 KiB
Go
141 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"gitlab.com/kbr4/svevijesti/internal/database"
|
|
)
|
|
|
|
func rootHandler(wr http.ResponseWriter, req *http.Request) {
|
|
title := "Pocetna"
|
|
store, err := database.Connect()
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
defer store.Close()
|
|
|
|
articles, err := database.ArticlesForDay(store, time.Now())
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
dayBefore := "/dan/" + time.Now().Add(-24*time.Hour).Format("2006-01-02")
|
|
|
|
cities := []string{"Sarajevo", "Banja Luka", "Zenica", "Tuzla", "Mostar"}
|
|
|
|
var weatherInfo []WeatherData
|
|
for _, city := range cities {
|
|
data, err := getWeather(city)
|
|
if err != nil {
|
|
fmt.Printf("Error fetching weather for %s: %v\n", city, err)
|
|
continue
|
|
}
|
|
weatherInfo = append(weatherInfo, data)
|
|
}
|
|
|
|
data := map[string]interface{}{
|
|
"title": title,
|
|
"articles": articles,
|
|
"previous": dayBefore,
|
|
"next": "/",
|
|
"weatherInfo": weatherInfo,
|
|
"categories": CategoryMenu,
|
|
}
|
|
|
|
err = templates.ExecuteTemplate(wr, "homeHTML", data)
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func dailyArticlesHandler(wr http.ResponseWriter, req *http.Request) {
|
|
vars := mux.Vars(req)
|
|
day, err := time.Parse("2006-01-02", vars["date"])
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusNotFound)
|
|
}
|
|
dayBefore := "/dan/" + day.Add(-24*time.Hour).Format("2006-01-02")
|
|
dayAfter := "/dan/" + day.Add(24*time.Hour).Format("2006-01-02")
|
|
|
|
if day.Add(24*time.Hour).Format("2006-01-02") == time.Now().Format("2006-01-02") {
|
|
dayAfter = "/"
|
|
}
|
|
|
|
title := fmt.Sprintf("Stare novine na dan %s", day.Format("2006-01-02"))
|
|
store, err := database.Connect()
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
defer store.Close()
|
|
|
|
articles, err := database.ArticlesForDay(store, day)
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
cities := []string{"Sarajevo", "Banja Luka", "Zenica", "Tuzla", "Mostar"}
|
|
|
|
var weatherInfo []WeatherData
|
|
for _, city := range cities {
|
|
data, err := getWeather(city)
|
|
if err != nil {
|
|
fmt.Printf("Error fetching weather for %s: %v\n", city, err)
|
|
continue
|
|
}
|
|
weatherInfo = append(weatherInfo, data)
|
|
}
|
|
|
|
data := map[string]interface{}{
|
|
"title": title,
|
|
"articles": articles,
|
|
"previous": dayBefore,
|
|
"next": dayAfter,
|
|
"weatherInfo": weatherInfo,
|
|
"categories": CategoryMenu,
|
|
}
|
|
|
|
err = templates.ExecuteTemplate(wr, "homeHTML", data)
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func articleHandler(wr http.ResponseWriter, req *http.Request) {
|
|
store, err := database.Connect()
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
defer store.Close()
|
|
|
|
vars := mux.Vars(req)
|
|
articleID, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
articleID = -1
|
|
}
|
|
articleSlug := vars["slug"]
|
|
article, err := database.ArticleByID(store, articleID, articleSlug)
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusNotFound)
|
|
}
|
|
|
|
next, previous, _ := database.PreviousAndNextArticleUrlByID(store, articleID)
|
|
|
|
title := article.Title
|
|
data := map[string]interface{}{
|
|
"title": title,
|
|
"article": article,
|
|
"previous": previous,
|
|
"next": next,
|
|
"categories": CategoryMenu,
|
|
}
|
|
|
|
err = templates.ExecuteTemplate(wr, "articleHTML", data)
|
|
if err != nil {
|
|
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|