This commit is contained in:
2023-12-18 16:51:47 +01:00
commit 88741b2303
36 changed files with 1490 additions and 0 deletions

110
internal/server/articles.go Normal file
View File

@@ -0,0 +1,110 @@
package server
import (
"fmt"
"github.com/gorilla/mux"
"gitlab.com/kbr4/svevijesti/internal/database"
"net/http"
"strconv"
"time"
)
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")
data := map[string]interface{}{
"title": title,
"articles": articles,
"previous": dayBefore,
"next": "/",
}
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)
}
data := map[string]interface{}{
"title": title,
"articles": articles,
"previous": dayBefore,
"next": dayAfter,
}
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,
}
err = templates.ExecuteTemplate(wr, "articleHTML", data)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
}

46
internal/server/server.go Normal file
View File

@@ -0,0 +1,46 @@
package server
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"io/ioutil"
"path/filepath"
"strings"
)
var tPath = "./web/tpl/"
var dPath = "./web/data/"
var templateDirs = []string{"./web/tpl", "./web/data"}
var templates *template.Template
func getTemplates() (templates *template.Template, err error) {
var allFiles []string
for _, dir := range templateDirs {
files2, _ := ioutil.ReadDir(dir)
for _, file := range files2 {
filename := file.Name()
if strings.HasSuffix(filename, ".html") {
filePath := filepath.Join(dir, filename)
fmt.Println("Template found: ", filePath)
allFiles = append(allFiles, filePath)
}
}
}
templates, err = template.New("").ParseFiles(allFiles...)
return
}
func init() {
templates, _ = getTemplates()
}
func CreateRoutes() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/dan/{date}", dailyArticlesHandler)
r.HandleFunc("/{id:[0-9]+}/{slug}", articleHandler)
r.HandleFunc("/", rootHandler)
return r
}