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-11-01 19:33:26 +00:00
|
|
|
"arimelody-web/admin"
|
|
|
|
"arimelody-web/controller"
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/global"
|
2024-11-01 19:33:26 +00:00
|
|
|
"arimelody-web/model"
|
2024-09-03 07:07:45 +00:00
|
|
|
"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-11-01 19:33:26 +00:00
|
|
|
func MusicHandler() http.Handler {
|
2024-08-02 21:48:26 +00:00
|
|
|
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-11-01 19:33:26 +00:00
|
|
|
release, err := controller.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-11-01 19:33:26 +00:00
|
|
|
releases, err := controller.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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-11-01 19:33:26 +00:00
|
|
|
|
|
|
|
func ServeGateway(release *model.Release) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// only allow authorised users to view hidden releases
|
|
|
|
authorised := admin.GetSession(r) != nil
|
|
|
|
if !authorised && !release.Visible {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := *release
|
|
|
|
|
|
|
|
if authorised || release.IsReleased() {
|
|
|
|
response.Tracks = release.Tracks
|
|
|
|
response.Credits = release.Credits
|
|
|
|
response.Links = release.Links
|
|
|
|
}
|
|
|
|
|
|
|
|
err := templates.Pages["music-gateway"].Execute(w, response)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering music gateway for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|