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
|
|
@ -15,20 +15,10 @@ import (
|
|||
func MusicHandler(app *model.AppState) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
ServeCatalog(app).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
release, err := controller.GetRelease(app.DB, r.URL.Path[1:], true)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
ServeGateway(app, release).ServeHTTP(w, r)
|
||||
}))
|
||||
mux.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
ServeGateway(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
mux.Handle("/", ServeCatalog(app))
|
||||
|
||||
return mux
|
||||
}
|
||||
|
|
@ -55,8 +45,14 @@ func ServeCatalog(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeGateway(app *model.AppState, release *model.Release) http.Handler {
|
||||
func ServeGateway(app *model.AppState, releaseID string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
release, err := controller.GetRelease(app.DB, r.PathValue("id"), true)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// only allow authorised users to view hidden releases
|
||||
privileged := false
|
||||
if !release.Visible {
|
||||
|
|
@ -86,8 +82,7 @@ func ServeGateway(app *model.AppState, release *model.Release) http.Handler {
|
|||
|
||||
for i, track := range release.Tracks { track.Number = i + 1 }
|
||||
|
||||
err := templates.MusicGatewayTemplate.Execute(w, release)
|
||||
|
||||
err = templates.MusicGatewayTemplate.Execute(w, release)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error rendering music gateway for %s: %v\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue