add html page w/ list of podcasts

This commit is contained in:
2026-02-13 17:04:32 +03:00
parent 76b550d628
commit e4c22dbe47
2 changed files with 134 additions and 6 deletions

46
main.go
View File

@@ -1,9 +1,11 @@
package main
import (
"embed"
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"io"
"log"
"net/http"
@@ -223,7 +225,14 @@ func deletePodcastById(db *sqlx.DB, podcastId int64) error {
return nil
}
var (
//go:embed views
viewsFS embed.FS
)
func main() {
tmpl := template.Must(template.ParseFS(viewsFS, "**/*.html"))
podcastsDirPath := os.Getenv("PODCASTS_DIRPATH")
dbPath := os.Getenv("DB_PATH")
@@ -324,6 +333,20 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
podcasts, err := getPodcasts(db)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
tmpl.ExecuteTemplate(w, "index.html", struct {
Podcasts []*Podcast
}{
Podcasts: podcasts,
})
})
mux.HandleFunc("GET /podcasts", func(w http.ResponseWriter, r *http.Request) {
podcasts, err := getPodcasts(db)
if err != nil {
@@ -338,9 +361,16 @@ func main() {
var body struct {
Feed string `json:"feed"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), 500)
return
isFormData := r.Header.Get("Content-Type") == "application/x-www-form-urlencoded"
if isFormData {
body.Feed = r.FormValue("feed")
} else {
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
if body.Feed == "" {
@@ -363,9 +393,13 @@ func main() {
log.Printf("failed to create directory for podcast %s: %v\n", feed.Channel.Title, err)
}
sendJSON(w, struct {
Ok bool `json:"ok"`
}{true}, 201)
if isFormData {
http.Redirect(w, r, "/", http.StatusFound)
} else {
sendJSON(w, struct {
Ok bool `json:"ok"`
}{true}, 201)
}
})
mux.HandleFunc("GET /podcasts/{id}/episodes", func(w http.ResponseWriter, r *http.Request) {