feat: define links in separate json file

This commit is contained in:
Stevan Freeborn
2025-11-24 21:34:15 -06:00
parent d423b5401b
commit a34467a67d
6 changed files with 81 additions and 29 deletions
+46 -7
View File
@@ -2,6 +2,8 @@
package handlers
import (
"encoding/json"
"io/fs"
"math"
"net/http"
"text/template"
@@ -10,21 +12,24 @@ import (
"github.com/StevanFreeborn/links.stevanfreeborn.com/internal/assets"
)
const LINKS_JSON_PATH = "json/links.json"
const DAYS_IN_YEAR = 365
const HOURS_IN_DAY = 24
var birthday time.Time = time.Date(1993, time.April, 21, 0, 0, 0, 0, time.UTC)
type Link struct {
Href string `json:"href"`
Icon string `json:"icon"`
Text string `json:"text"`
}
type IndexViewModel struct {
Age float64
Age float64
Links []Link
}
func Index(w http.ResponseWriter, r *http.Request) {
// TODO: Links coming in external JSON data
// this could be static file in the repo
// or it could be fetched
// { link: "link", text: "text", "icon": "icon" }
t, err := template.ParseFS(assets.Templates, "templates/index.gohtml")
if err != nil {
@@ -32,10 +37,25 @@ func Index(w http.ResponseWriter, r *http.Request) {
return
}
linksFile, err := assets.JSON.Open(LINKS_JSON_PATH)
if err != nil {
http.Error(w, "Unable to load links", http.StatusInternalServerError)
return
}
links, err := readLinksFromJSON(linksFile)
if err != nil {
http.Error(w, "Unable to parse links", http.StatusInternalServerError)
return
}
age := time.Since(birthday).Hours() / HOURS_IN_DAY / DAYS_IN_YEAR
viewModel := IndexViewModel{
Age: math.Floor(age),
Age: math.Floor(age),
Links: links,
}
t.Execute(w, viewModel)
@@ -48,3 +68,22 @@ func CSS(w http.ResponseWriter, r *http.Request) {
func Fonts(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, assets.Fonts, r.URL.Path)
}
func Images(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, assets.Images, r.URL.Path)
}
func readLinksFromJSON(file fs.File) ([]Link, error) {
defer file.Close()
var links []Link
decoder := json.NewDecoder(file)
err := decoder.Decode(&links)
if err != nil {
return nil, err
}
return links, nil
}