Files
podstalk/main.go
T
jbrechtel c3d1adddc2
Build and Deploy / build-and-deploy (push) Successful in 47s
Fix blank episode form: move all CSS to external static files
- Go html/template rejects <style> blocks and inline style attributes
  when template expressions are present in URL/CSS contexts
- Move all inline CSS to static/auth.css, static/episode.css,
  static/dashboard.css
- Serve static files from embedded FS via /static/ handler
- Fix missing </div> in episode form audio file group
2026-07-08 13:12:43 -04:00

102 lines
2.4 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
//go:embed static/*
var staticFS 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)
// Static files.
http.Handle("/static/", http.FileServer(http.FS(staticFS)))
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
}