Refactor server routes
This commit is contained in:
parent
ca9b6e05b7
commit
5cbb20dcc4
21 changed files with 429 additions and 390 deletions
66
internal/server/routes/days.go
Normal file
66
internal/server/routes/days.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/config"
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/files"
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/lang"
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/server/util"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// getDays calls getEntries for previous days' entries.
|
||||
func getDays(w http.ResponseWriter, r *http.Request) {
|
||||
description := template.HTML(
|
||||
"<a href=\"#footer\">" + template.HTMLEscapeString(lang.Translate("prompt.days")) + "</a>")
|
||||
getEntries(w, r, lang.Translate("title.days"), description, "day", func(files []string) []Entry {
|
||||
var filesFormatted []Entry
|
||||
for i := range files {
|
||||
v := files[len(files)-1-i] // This is suboptimal, but reverse order is better here
|
||||
dayString := v
|
||||
t, err := time.Parse(time.DateOnly, v)
|
||||
if err == nil {
|
||||
dayString = t.Format("02 Jan 2006")
|
||||
}
|
||||
|
||||
// Fancy text for today and tomorrow
|
||||
// This looks bad, but strings.Title is deprecated, and I'm not importing a golang.org/x package for this...
|
||||
// (chances we ever run into tomorrow are really low)
|
||||
if v == config.Cfg.TodayDate() {
|
||||
dayString = lang.Translate("link.today")
|
||||
dayString = strings.ToTitle(string([]rune(dayString)[0])) + string([]rune(dayString)[1:])
|
||||
} else if v > config.Cfg.TodayDate() {
|
||||
dayString = lang.Translate("link.tomorrow")
|
||||
dayString = strings.ToTitle(string([]rune(dayString)[0])) + string([]rune(dayString)[1:])
|
||||
}
|
||||
filesFormatted = append(filesFormatted, Entry{Title: dayString, Link: "day/" + v})
|
||||
}
|
||||
return filesFormatted
|
||||
})
|
||||
}
|
||||
|
||||
// getDay calls getEntry for a day entry.
|
||||
func getDay(w http.ResponseWriter, r *http.Request) {
|
||||
dayString := chi.URLParam(r, "day")
|
||||
if dayString == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
util.HandleWrite(w.Write([]byte("day not specified")))
|
||||
return
|
||||
}
|
||||
if dayString == config.Cfg.TodayDate() { // Today can still be edited
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
title := dayString
|
||||
t, err := time.Parse(time.DateOnly, dayString)
|
||||
if err == nil { // This is low priority so silently fail
|
||||
title = t.Format("02 Jan 2006")
|
||||
}
|
||||
|
||||
getEntry(w, r, title, files.DataFile("day/"+dayString), false)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue