diff --git a/main.go b/main.go index f30fbd9..d5002ed 100644 --- a/main.go +++ b/main.go @@ -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) { diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..ce546dc --- /dev/null +++ b/views/index.html @@ -0,0 +1,94 @@ + + + + + + podcaster + + + + +

podcaster

+ +
+ + +
+ +
+ {{range .Podcasts}} + + + {{.Name}} + + {{end}} +
+ +