Add archive functionality

This commit is contained in:
Senad Uka
2023-08-01 10:07:48 +02:00
parent b1561c4875
commit 517486689e
2 changed files with 138 additions and 6 deletions

View File

@@ -3,13 +3,14 @@ package cetgemini
import (
"context"
"fmt"
"github.com/a-h/gemini"
"github.com/a-h/gemini/mux"
"github.com/senaduka/cetvorke/database"
"log"
"strconv"
"strings"
"sync"
"github.com/a-h/gemini"
"github.com/a-h/gemini/mux"
"github.com/senaduka/cetvorke/database"
)
func StartServer(waitgroup *sync.WaitGroup) {
@@ -18,10 +19,16 @@ func StartServer(waitgroup *sync.WaitGroup) {
// Create the handlers for a domain (a.gemini).
index := gemini.HandlerFunc(indexHandler)
post := gemini.HandlerFunc(postHandler)
archive := gemini.HandlerFunc(archiveHandler)
year := gemini.HandlerFunc(yearHandler)
month := gemini.HandlerFunc(monthHandler)
// Create a router for gemini://a.gemini/require_cert and gemini://a.gemini/public
routerA := mux.NewMux()
routerA.AddRoute("/clanak/{postID}/*", post)
routerA.AddRoute("/a/", archive)
routerA.AddRoute("/y/{year}/", year)
routerA.AddRoute("/y/{year}/m/{month}/", month)
routerA.AddRoute("/p/{postID}/*", post)
routerA.AddRoute("/", index)
// Set up the domain handlers.
@@ -46,13 +53,19 @@ func indexHandler(w gemini.ResponseWriter, r *gemini.Request) {
return
}
response := "# Najnoviji članci\n\n"
response := "# IslamBosna\n\n"
response += "## Najnoviji članci\n\n"
for _, link := range links {
// append links to response in gemini feed format
response += link.GemtextLink()
}
response += "## Ostalo \n\n"
response += "=> /a/ Arhiva\n\n"
fmt.Println(response)
w.Write([]byte(response))
@@ -76,3 +89,78 @@ func postHandler(w gemini.ResponseWriter, r *gemini.Request) {
response := post.GemtextPage()
w.Write([]byte(response))
}
func archiveHandler(w gemini.ResponseWriter, r *gemini.Request) {
links, title, err := database.GetAllYearLinks()
if err != nil {
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}
response := title + "\n\n"
for _, link := range links {
// append links to response in gemini feed format
response += link.GemtextArchiveLink()
}
w.Write([]byte(response))
}
func yearHandler(w gemini.ResponseWriter, r *gemini.Request) {
year_string := strings.Split(r.URL.Path, "/")[2]
year, err := strconv.Atoi(year_string)
if err != nil {
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}
links, title, err := database.GetMonthLinksByYear(year)
if err != nil {
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}
response := title + "\n\n"
for _, link := range links {
// append links to response in gemini feed format
response += link.GemtextArchiveMonthLink()
}
w.Write([]byte(response))
}
func monthHandler(w gemini.ResponseWriter, r *gemini.Request) {
year_string := strings.Split(r.URL.Path, "/")[2]
year, err := strconv.Atoi(year_string)
if err != nil {
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}
month_string := strings.Split(r.URL.Path, "/")[4]
month, err := strconv.Atoi(month_string)
if err != nil {
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}
links, title, err := database.GetLinksByMonth(year, month)
if err != nil {
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}
response := title + "\n\n"
for _, link := range links {
// append links to response in gemini feed format
response += link.GemtextLink()
}
w.Write([]byte(response))
}

View File

@@ -12,7 +12,15 @@ type Link struct {
}
func (l *Link) GemtextLink() string {
return fmt.Sprintf("=> /clanak/%d/%s %d-%d-%d - %s\n", l.ID, l.TitleSlug, l.Year, l.Month, l.Day, l.Title)
return fmt.Sprintf("=> /p/%d/%s %d-%d-%d - %s\n", l.ID, l.TitleSlug, l.Year, l.Month, l.Day, l.Title)
}
func (l *Link) GemtextArchiveLink() string {
return fmt.Sprintf("=> /y/%d/ Godina %d.\n", l.Year, l.Year)
}
func (l *Link) GemtextArchiveMonthLink() string {
return fmt.Sprintf("=> /y/%d/m/%d/ %d-%d\n", l.Year, l.Month, l.Year, l.Month)
}
func GetRecentLinks() ([]Link, error) {
@@ -24,3 +32,39 @@ func GetRecentLinks() ([]Link, error) {
}
return links, nil
}
func GetLinksByMonth(year, month int) ([]Link, string, error) {
db := GetDB()
links := []Link{}
err := db.Select(&links, "SELECT id, title_slug, year, month, day, post_title FROM posts WHERE year = ? and month = ? ORDER BY date desc", year, month)
if err != nil {
return nil, "", err
}
return links, fmt.Sprintf("# Arhiva za %d-%d\n\n", year, month), nil
}
func GetMonthLinksByYear(year int) ([]Link, string, error) {
db := GetDB()
links := []Link{}
err := db.Select(&links, "SELECT distinct month, year FROM posts WHERE year = ? ORDER BY month DESC", year)
if err != nil {
return nil, "", err
}
return links, fmt.Sprintf("# Arhiva za %d\n\n", year), nil
}
func GetAllYearLinks() ([]Link, string, error) {
db := GetDB()
links := []Link{}
err := db.Select(&links, "SELECT distinct year FROM posts ORDER BY year DESC")
if err != nil {
return nil, "", err
}
return links, fmt.Sprintf("# Arhiva: "), nil
}