Files
podstalk/main.go
T
jbrechtel b179f57628
Build and Deploy / build-and-deploy (push) Successful in 45s
Slug-based RSS URLs and success flash messages
- Add podcast_slug to users with global uniqueness check
- RSS available at /rss/{slug} (path-based) with legacy ?slug= fallback
- Slug field in dashboard settings with validation (lowercase, hyphens)
- JS flash messages for success/error after form submissions
- Remove unused imports
2026-07-08 09:29:00 -04:00

97 lines
2.3 KiB
Go

package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"podstalk/handler"
"podstalk/store"
)
//go:embed template/*.html
var templateFS embed.FS
func main() {
dbPath := os.Getenv("PODSTALK_DB")
if dbPath == "" {
dbPath = "podstalk.db"
}
uploadDir := os.Getenv("PODSTALK_UPLOAD_DIR")
if uploadDir == "" {
uploadDir = "uploads"
}
baseURL := os.Getenv("PODSTALK_LINK")
if baseURL == "" {
baseURL = "http://localhost:8080"
}
st, err := store.New(dbPath)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
defer st.Close()
tmpl, err := template.New("").ParseFS(templateFS, "template/*.html")
if err != nil {
log.Fatalf("failed to parse templates: %v", err)
}
tpl := handler.NewTemplates(tmpl)
sessions := handler.NewSessionStore()
authMid := &handler.AuthMiddleware{Store: st, Sessions: sessions}
authH := &handler.AuthHandler{Store: st, Sessions: sessions, Tpl: tpl}
epH := &handler.EpisodeHandler{
Store: st,
Tpl: tpl,
UploadDir: uploadDir,
BaseURL: baseURL,
}
setH := &handler.SettingsHandler{
Store: st,
UploadDir: uploadDir,
}
rssH := &handler.RSSHandler{
Store: st,
Link: baseURL,
}
// Auth routes.
http.HandleFunc("/signup", authMid.RedirectIfAuthed(authH.ServeSignup))
http.HandleFunc("/signin", authMid.RedirectIfAuthed(authH.ServeSignin))
http.HandleFunc("/signout", authH.ServeSignout)
// Protected routes.
http.HandleFunc("/", authMid.RequireAuth(epH.ServeDashboard))
http.HandleFunc("/episodes/new", authMid.RequireAuth(epH.ServeNew))
http.HandleFunc("/episodes/edit", authMid.RequireAuth(epH.ServeEdit))
http.HandleFunc("/episodes/delete", authMid.RequireAuth(epH.ServeDelete))
http.HandleFunc("/settings", authMid.RequireAuth(setH.ServeSettings))
// Public RSS: /rss/my-slug or /rss?slug=my-slug or /rss?user=N (legacy)
http.HandleFunc("/rss", rssH.ServeRSS)
http.HandleFunc("GET /rss/{slug}", rssH.ServeRSS)
// Public uploaded files.
http.HandleFunc("/uploads/", epH.ServeUpload)
port := envOrDefault("PORT", "8080")
addr := fmt.Sprintf(":%s", port)
log.Printf("Podstalk listening on http://0.0.0.0%s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}