新着一覧の画面を追加(ジャンル切替、予告の除外、10分キャッシュ)

This commit is contained in:
2026-10-03 17:20:44 +09:00
parent ffe7451d04
commit f0d73a14e1
9 changed files with 518 additions and 13 deletions
+23 -3
View File
@@ -4,7 +4,10 @@ import (
"embed"
"html/template"
"io/fs"
"log"
"net/http"
"sync"
"time"
)
//go:embed templates/*.html static/*
@@ -12,29 +15,46 @@ var assets embed.FS
type page struct {
Title string
Newer *newerView
}
func NewHandler() http.Handler {
type server struct {
fetcher NewerFetcher
now func() time.Time
logger *log.Logger
zone *time.Location
tmpl *template.Template
cacheMu sync.Mutex
cache map[string]cacheEntry
}
func NewHandler(fetcher NewerFetcher, now func() time.Time, logger *log.Logger) http.Handler {
tmpl := template.Must(template.ParseFS(assets, "templates/*.html"))
staticFiles, err := fs.Sub(assets, "static")
if err != nil {
panic(err)
}
zone, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
panic(err)
}
s := &server{fetcher: fetcher, now: now, logger: logger, zone: zone, tmpl: tmpl, cache: make(map[string]cacheEntry)}
mux := http.NewServeMux()
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(staticFiles)))
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = w.Write([]byte("ok\n"))
})
mux.HandleFunc("GET /{$}", s.home)
mux.HandleFunc("GET /newer", s.newerFragment)
for path, title := range map[string]string{
"GET /{$}": "新着",
"GET /queue": "待ち行列",
"GET /subscriptions": "登録番組",
"GET /library": "ライブラリ",
} {
mux.HandleFunc(path, func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := tmpl.ExecuteTemplate(w, "layout", page{Title: title}); err != nil {
if err := s.tmpl.ExecuteTemplate(w, "layout", page{Title: title}); err != nil {
http.Error(w, "template error", http.StatusInternalServerError)
}
})