2024-08-02 21:48:26 +00:00
|
|
|
package view
|
2024-03-18 10:34:43 +00:00
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
import (
|
2024-09-01 03:43:32 +00:00
|
|
|
"fmt"
|
2024-08-01 00:39:18 +00:00
|
|
|
"net/http"
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/global"
|
|
|
|
music "arimelody-web/music/controller"
|
|
|
|
"arimelody-web/music/model"
|
|
|
|
"arimelody-web/templates"
|
2024-03-18 20:19:04 +00:00
|
|
|
)
|
|
|
|
|
2024-08-01 00:39:18 +00:00
|
|
|
// HTTP HANDLER METHODS
|
2024-03-18 20:19:04 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
func Handler() http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
2024-08-01 00:39:18 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-01 02:54:15 +00:00
|
|
|
if r.URL.Path == "/" {
|
2024-08-02 21:48:26 +00:00
|
|
|
ServeCatalog().ServeHTTP(w, r)
|
2024-08-01 02:54:15 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-01 00:39:18 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
release, err := music.GetRelease(global.DB, r.URL.Path[1:], true)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2024-08-01 02:54:15 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
ServeGateway(release).ServeHTTP(w, r)
|
|
|
|
}))
|
2024-08-01 02:54:15 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
return mux
|
|
|
|
}
|
2024-08-01 02:54:15 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func ServeCatalog() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-03 07:07:45 +00:00
|
|
|
releases, err := music.GetAllReleases(global.DB, true, 0, true)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to pull releases for catalog: %s\n", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-08-01 02:54:15 +00:00
|
|
|
return
|
|
|
|
}
|
2024-09-03 07:07:45 +00:00
|
|
|
|
|
|
|
for _, release := range releases {
|
|
|
|
if !release.IsReleased() {
|
|
|
|
release.ReleaseType = model.Upcoming
|
2024-08-01 02:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
err = templates.Pages["music"].Execute(w, releases)
|
2024-08-01 02:54:15 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|