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, BaseTitle: envOrDefault("PODSTALK_TITLE", "Podstalk"), Link: baseURL, Author: envOrDefault("PODSTALK_AUTHOR", "Podstalk"), } // Auth routes — redirect to dashboard if already signed in. 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 feed. http.HandleFunc("/rss", 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 }