2024-08-02 21:48:26 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-09-01 03:43:32 +00:00
|
|
|
"fmt"
|
2024-08-02 21:48:26 +00:00
|
|
|
"net/http"
|
2024-09-01 03:43:32 +00:00
|
|
|
"strings"
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/admin"
|
2025-01-20 10:34:39 +00:00
|
|
|
"arimelody-web/controller"
|
2025-01-21 14:53:18 +00:00
|
|
|
"arimelody-web/model"
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
func Handler(app *model.AppState) http.Handler {
|
2024-08-02 21:48:26 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2024-09-22 23:57:23 +00:00
|
|
|
// ACCOUNT ENDPOINTS
|
|
|
|
|
2025-01-20 19:02:26 +00:00
|
|
|
/*
|
|
|
|
// temporarily disabling these
|
|
|
|
// accounts should really be handled via the frontend rn, and juggling
|
|
|
|
// two different token bearer methods kinda sucks!!
|
|
|
|
// i'll look into generating API tokens on the frontend in the future
|
|
|
|
// TODO: generate API keys on the frontend
|
|
|
|
|
2024-09-22 23:57:23 +00:00
|
|
|
mux.Handle("/v1/login", handleLogin())
|
|
|
|
mux.Handle("/v1/register", handleAccountRegistration())
|
|
|
|
mux.Handle("/v1/delete-account", handleDeleteAccount())
|
2025-01-20 19:02:26 +00:00
|
|
|
*/
|
2024-09-22 23:57:23 +00:00
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
// ARTIST ENDPOINTS
|
|
|
|
|
|
|
|
mux.Handle("/v1/artist/", http.StripPrefix("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-01 03:43:32 +00:00
|
|
|
var artistID = strings.Split(r.URL.Path[1:], "/")[0]
|
2025-01-21 14:53:18 +00:00
|
|
|
artist, err := controller.GetArtist(app.DB, artistID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2024-09-01 23:15:23 +00:00
|
|
|
if strings.Contains(err.Error(), "no rows") {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Error while retrieving artist %s: %s\n", artistID, err)
|
2024-09-01 23:15:23 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-09-01 03:43:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
// GET /api/v1/artist/{id}
|
2025-01-21 14:53:18 +00:00
|
|
|
ServeArtist(app, artist).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
case http.MethodPut:
|
|
|
|
// PUT /api/v1/artist/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, UpdateArtist(app, artist)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
case http.MethodDelete:
|
|
|
|
// DELETE /api/v1/artist/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, DeleteArtist(app, artist)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})))
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2024-08-03 22:24:15 +00:00
|
|
|
// GET /api/v1/artist
|
2025-01-21 14:53:18 +00:00
|
|
|
ServeAllArtists(app).ServeHTTP(w, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
case http.MethodPost:
|
2024-08-03 22:24:15 +00:00
|
|
|
// POST /api/v1/artist (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, CreateArtist(app)).ServeHTTP(w, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}))
|
2024-08-02 23:27:30 +00:00
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
// RELEASE ENDPOINTS
|
|
|
|
|
2024-08-03 14:02:01 +00:00
|
|
|
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-01 03:43:32 +00:00
|
|
|
var releaseID = strings.Split(r.URL.Path[1:], "/")[0]
|
2025-01-21 14:53:18 +00:00
|
|
|
release, err := controller.GetRelease(app.DB, releaseID, true)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2024-09-01 23:15:23 +00:00
|
|
|
if strings.Contains(err.Error(), "no rows") {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Error while retrieving release %s: %s\n", releaseID, err)
|
2024-09-01 23:15:23 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-09-01 03:43:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-03 14:02:01 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2024-08-03 22:24:15 +00:00
|
|
|
// GET /api/v1/music/{id}
|
2025-01-21 14:53:18 +00:00
|
|
|
ServeRelease(app, release).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
case http.MethodPut:
|
|
|
|
// PUT /api/v1/music/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, UpdateRelease(app, release)).ServeHTTP(w, r)
|
2024-08-03 14:02:01 +00:00
|
|
|
case http.MethodDelete:
|
2024-08-03 22:24:15 +00:00
|
|
|
// DELETE /api/v1/music/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, DeleteRelease(app, release)).ServeHTTP(w, r)
|
2024-08-03 14:02:01 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})))
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2024-08-03 22:24:15 +00:00
|
|
|
// GET /api/v1/music
|
2025-01-21 14:53:18 +00:00
|
|
|
ServeCatalog(app).ServeHTTP(w, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
case http.MethodPost:
|
2024-08-03 22:24:15 +00:00
|
|
|
// POST /api/v1/music (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, CreateRelease(app)).ServeHTTP(w, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
// TRACK ENDPOINTS
|
|
|
|
|
|
|
|
mux.Handle("/v1/track/", http.StripPrefix("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-01 03:43:32 +00:00
|
|
|
var trackID = strings.Split(r.URL.Path[1:], "/")[0]
|
2025-01-21 14:53:18 +00:00
|
|
|
track, err := controller.GetTrack(app.DB, trackID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2024-09-01 23:15:23 +00:00
|
|
|
if strings.Contains(err.Error(), "no rows") {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Error while retrieving track %s: %s\n", trackID, err)
|
2024-09-01 23:15:23 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-09-01 03:43:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
// GET /api/v1/track/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, ServeTrack(app, track)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
case http.MethodPut:
|
|
|
|
// PUT /api/v1/track/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, UpdateTrack(app, track)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
case http.MethodDelete:
|
|
|
|
// DELETE /api/v1/track/{id} (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, DeleteTrack(app, track)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})))
|
|
|
|
mux.Handle("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
// GET /api/v1/track (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, ServeAllTracks(app)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
case http.MethodPost:
|
|
|
|
// POST /api/v1/track (admin)
|
2025-01-21 14:53:18 +00:00
|
|
|
admin.RequireAccount(app, CreateTrack(app)).ServeHTTP(w, r)
|
2024-08-03 22:24:15 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}))
|
2024-08-02 23:27:30 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
return mux
|
|
|
|
}
|