2025-11-15 09:00:06 -06:00
|
|
|
// Package handlers provides HTTP handlers for the web application.
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-24 08:58:47 -06:00
|
|
|
"math"
|
2025-11-15 09:00:06 -06:00
|
|
|
"net/http"
|
|
|
|
|
"text/template"
|
2025-11-24 08:58:47 -06:00
|
|
|
"time"
|
2025-11-15 09:00:06 -06:00
|
|
|
|
|
|
|
|
"github.com/StevanFreeborn/links.stevanfreeborn.com/internal/assets"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-24 08:58:47 -06:00
|
|
|
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 IndexViewModel struct {
|
|
|
|
|
Age float64
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 09:00:06 -06:00
|
|
|
func Index(w http.ResponseWriter, r *http.Request) {
|
2025-11-24 08:58:47 -06:00
|
|
|
// TODO: Links coming in external JSON data
|
|
|
|
|
// this could be static file in the repo
|
|
|
|
|
// or it could be fetched
|
2025-11-20 05:03:51 -06:00
|
|
|
// { link: "link", text: "text", "icon": "icon" }
|
|
|
|
|
|
2025-11-15 09:00:06 -06:00
|
|
|
t, err := template.ParseFS(assets.Templates, "templates/index.gohtml")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "Unable to load template", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 08:58:47 -06:00
|
|
|
age := time.Since(birthday).Hours() / HOURS_IN_DAY / DAYS_IN_YEAR
|
|
|
|
|
|
|
|
|
|
viewModel := IndexViewModel{
|
|
|
|
|
Age: math.Floor(age),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Execute(w, viewModel)
|
2025-11-15 09:00:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CSS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
http.ServeFileFS(w, r, assets.CSS, r.URL.Path)
|
|
|
|
|
}
|
2025-11-15 12:35:51 -06:00
|
|
|
|
|
|
|
|
func Fonts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
http.ServeFileFS(w, r, assets.Fonts, r.URL.Path)
|
|
|
|
|
}
|