refactor mux path routes
legend has it that if you refactor your code enough times, one day you will finally be happy
This commit is contained in:
parent
82fd17c836
commit
21912d4ec2
17 changed files with 102 additions and 469 deletions
18
api/api.go
18
api/api.go
|
|
@ -18,8 +18,8 @@ func Handler(app *model.AppState) http.Handler {
|
|||
|
||||
// ARTIST ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/artist/", http.StripPrefix("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
mux.Handle("/v1/artist/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
|
|
@ -44,7 +44,7 @@ func Handler(app *model.AppState) http.Handler {
|
|||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})))
|
||||
}))
|
||||
mux.Handle("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
|
@ -60,8 +60,8 @@ func Handler(app *model.AppState) http.Handler {
|
|||
|
||||
// RELEASE ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
mux.Handle("/v1/music/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
|
|
@ -86,7 +86,7 @@ func Handler(app *model.AppState) http.Handler {
|
|||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})))
|
||||
}))
|
||||
mux.Handle("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
|
@ -102,8 +102,8 @@ func Handler(app *model.AppState) http.Handler {
|
|||
|
||||
// TRACK ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/track/", http.StripPrefix("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
mux.Handle("/v1/track/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
|
|
@ -128,7 +128,7 @@ func Handler(app *model.AppState) http.Handler {
|
|||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})))
|
||||
}))
|
||||
mux.Handle("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue