very rough updates to admin pages, reduced reliance on global.DB

This commit is contained in:
ari melody 2025-01-21 00:20:07 +00:00
parent ae254dd731
commit 7044f7344b
Signed by: ari
GPG key ID: CF99829C92678188
15 changed files with 192 additions and 106 deletions

View file

@ -6,37 +6,38 @@ import (
"os"
"arimelody-web/controller"
"arimelody-web/global"
"arimelody-web/model"
"arimelody-web/templates"
"github.com/jmoiron/sqlx"
)
// HTTP HANDLER METHODS
func MusicHandler() http.Handler {
func MusicHandler(db *sqlx.DB) http.Handler {
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
ServeCatalog().ServeHTTP(w, r)
ServeCatalog(db).ServeHTTP(w, r)
return
}
release, err := controller.GetRelease(global.DB, r.URL.Path[1:], true)
release, err := controller.GetRelease(db, r.URL.Path[1:], true)
if err != nil {
http.NotFound(w, r)
return
}
ServeGateway(release).ServeHTTP(w, r)
ServeGateway(db, release).ServeHTTP(w, r)
}))
return mux
}
func ServeCatalog() http.Handler {
func ServeCatalog(db *sqlx.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
releases, err := controller.GetAllReleases(global.DB, true, 0, true)
releases, err := controller.GetAllReleases(db, true, 0, true)
if err != nil {
fmt.Printf("FATAL: Failed to pull releases for catalog: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -56,12 +57,12 @@ func ServeCatalog() http.Handler {
})
}
func ServeGateway(release *model.Release) http.Handler {
func ServeGateway(db *sqlx.DB, release *model.Release) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// only allow authorised users to view hidden releases
privileged := false
if !release.Visible {
account, err := controller.GetAccountByRequest(global.DB, r)
account, err := controller.GetAccountByRequest(db, r)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)