refactoring everything teehee (i'm so glad this isn't a team project)
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
c684f0c7ae
commit
10f5f51e76
17 changed files with 970 additions and 547 deletions
|
@ -1,37 +1,62 @@
|
|||
package music
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody.me/arimelody.me/api/v1/admin"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
)
|
||||
|
||||
var Releases []*MusicRelease;
|
||||
var Artists []*Artist;
|
||||
// func make_date_work(date string) time.Time {
|
||||
// res, err := time.Parse("2-Jan-2006", date)
|
||||
// if err != nil {
|
||||
// fmt.Printf("somehow we failed to parse %s! falling back to epoch :]\n", date)
|
||||
// return time.Unix(0, 0)
|
||||
// }
|
||||
// return res
|
||||
// }
|
||||
|
||||
func make_date_work(date string) time.Time {
|
||||
res, err := time.Parse("2-Jan-2006", date)
|
||||
if err != nil {
|
||||
fmt.Printf("somehow we failed to parse %s! falling back to epoch :]\n", date)
|
||||
return time.Unix(0, 0)
|
||||
}
|
||||
return res
|
||||
}
|
||||
// HTTP HANDLER METHODS
|
||||
|
||||
func GetRelease(id string) (*MusicRelease, error) {
|
||||
for _, release := range Releases {
|
||||
if release.Id == id {
|
||||
return release, nil
|
||||
func ServeCatalog() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
releases := []Release{}
|
||||
authorised := r.Context().Value("role") != nil && r.Context().Value("role") == "admin"
|
||||
for _, release := range Releases {
|
||||
if !release.IsReleased() && !authorised {
|
||||
continue
|
||||
}
|
||||
releases = append(releases, release)
|
||||
}
|
||||
}
|
||||
return nil, errors.New(fmt.Sprintf("Release %s not found", id))
|
||||
|
||||
global.ServeTemplate("music.html", releases).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func GetArtist(id string) (*Artist, error) {
|
||||
for _, artist := range Artists {
|
||||
if artist.Id == id {
|
||||
return artist, nil
|
||||
func ServeGateway() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.URL.Path[1:]
|
||||
release := GetRelease(id)
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
return nil, errors.New(fmt.Sprintf("Artist %s not found", id))
|
||||
|
||||
// only allow authorised users to view unreleased releases
|
||||
authorised := r.Context().Value("role") != nil && r.Context().Value("role") == "admin"
|
||||
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 loading music gateway for %s\n", id)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue