HOLY REFACTOR GOOD GRIEF (also finally started some CRUD work)
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
1c310c9101
commit
442889340c
80 changed files with 1571 additions and 1330 deletions
74
music/view/release.go
Normal file
74
music/view/release.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package view
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody.me/arimelody.me/admin"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
)
|
||||
|
||||
// HTTP HANDLERS
|
||||
|
||||
func ServeRelease() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
releaseID := r.URL.Path[1:]
|
||||
var release = global.GetRelease(releaseID)
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// only allow authorised users to view unreleased releases
|
||||
authorised := admin.GetSession(r) != nil
|
||||
if !release.IsReleased() && !authorised {
|
||||
admin.MustAuthorise(ServeRelease()).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ServeGateway() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.Redirect(w, r, "/music", http.StatusPermanentRedirect)
|
||||
return
|
||||
}
|
||||
|
||||
id := r.URL.Path[1:]
|
||||
release := global.GetRelease(id)
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// only allow authorised users to view unreleased releases
|
||||
authorised := admin.GetSession(r) != nil
|
||||
if !release.IsReleased() && !authorised {
|
||||
admin.MustAuthorise(ServeGateway()).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
lrw := global.LoggingResponseWriter{w, http.StatusOK}
|
||||
|
||||
global.ServeTemplate("music-gateway.html", release).ServeHTTP(&lrw, r)
|
||||
|
||||
if lrw.Code != http.StatusOK {
|
||||
fmt.Printf("Error rendering music gateway for %s\n", id)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue