rewrite ui with vite+react
This commit is contained in:
94
main.go
94
main.go
@@ -5,13 +5,11 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -229,13 +227,11 @@ func deletePodcastById(db *sqlx.DB, podcastId int64) error {
|
||||
}
|
||||
|
||||
var (
|
||||
//go:embed views
|
||||
viewsFS embed.FS
|
||||
//go:embed web/dist/*
|
||||
webFS embed.FS
|
||||
)
|
||||
|
||||
func main() {
|
||||
tmpl := template.Must(template.ParseFS(viewsFS, "**/*.html"))
|
||||
|
||||
podcastsDirPath := os.Getenv("PODCASTS_DIRPATH")
|
||||
|
||||
dbPath := os.Getenv("DB_PATH")
|
||||
@@ -337,20 +333,21 @@ func main() {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||
podcasts, err := getPodcasts(db)
|
||||
path := r.URL.Path
|
||||
if path == "/" {
|
||||
path = "/index.html"
|
||||
}
|
||||
path = "web/dist" + path
|
||||
|
||||
_, err := webFS.Open(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
path = "web/dist/index.html"
|
||||
}
|
||||
|
||||
tmpl.ExecuteTemplate(w, "index.html", struct {
|
||||
Podcasts []*Podcast
|
||||
}{
|
||||
Podcasts: podcasts,
|
||||
})
|
||||
http.ServeFileFS(w, r, webFS, path)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /podcasts", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("GET /api/podcasts", func(w http.ResponseWriter, r *http.Request) {
|
||||
podcasts, err := getPodcasts(db)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
@@ -360,52 +357,7 @@ func main() {
|
||||
sendJSON(w, podcasts, 200)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
return
|
||||
}
|
||||
|
||||
podcast, err := getPodcastById(db, int64(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
return
|
||||
}
|
||||
|
||||
episodes, err := getPodcastEpisodes(db, podcast.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
return
|
||||
}
|
||||
|
||||
loc, err := time.LoadLocation("Europe/Moscow")
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to load time location: %v", err), 500)
|
||||
return
|
||||
}
|
||||
for _, episode := range episodes {
|
||||
episode.CreatedAt = episode.CreatedAt.In(loc)
|
||||
}
|
||||
|
||||
slices.SortFunc(episodes, func(a, b *Episode) int {
|
||||
if a.CreatedAt.Before(b.CreatedAt) {
|
||||
return 1
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
})
|
||||
|
||||
tmpl.ExecuteTemplate(w, "podcast.html", struct {
|
||||
Podcast *Podcast
|
||||
Episodes []*Episode
|
||||
}{
|
||||
Podcast: podcast,
|
||||
Episodes: episodes,
|
||||
})
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /podcasts", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("POST /api/podcasts", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Feed string `json:"feed"`
|
||||
}
|
||||
@@ -450,7 +402,23 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /podcasts/{id}/episodes", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("GET /api/podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
podcast, err := getPodcastById(db, int64(id))
|
||||
if err != nil {
|
||||
http.Error(w, "podcast not found", 404)
|
||||
return
|
||||
}
|
||||
|
||||
sendJSON(w, podcast, 200)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/podcasts/{id}/episodes", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
@@ -466,7 +434,7 @@ func main() {
|
||||
sendJSON(w, episodes, 200)
|
||||
})
|
||||
|
||||
mux.HandleFunc("DELETE /podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("DELETE /api/podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
|
||||
Reference in New Issue
Block a user