Merge branch 'dev' into feature/blog

THAT WAS PAINFUL!
This commit is contained in:
ari melody 2025-11-06 21:24:52 +00:00
commit 3e5ecb9372
Signed by: ari
GPG key ID: CF99829C92678188
99 changed files with 2029 additions and 1010 deletions

View file

@ -4,7 +4,6 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"arimelody-web/admin/account"
"arimelody-web/admin/auth"
@ -14,6 +13,7 @@ import (
"arimelody-web/admin/templates"
"arimelody-web/controller"
"arimelody-web/model"
"arimelody-web/view"
)
func Handler(app *model.AppState) http.Handler {
@ -28,7 +28,20 @@ func Handler(app *model.AppState) http.Handler {
mux.Handle("/logs", core.RequireAccount(logs.Handler(app)))
mux.Handle("/account/", core.RequireAccount(http.StripPrefix("/account", account.Handler(app))))
mux.Handle("/static/", http.StripPrefix("/static", staticHandler()))
mux.Handle("/static/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/static/admin.css" {
http.ServeFile(w, r, "./admin/static/admin.css")
return
}
if r.URL.Path == "/static/admin.js" {
http.ServeFile(w, r, "./admin/static/admin.js")
return
}
core.RequireAccount(
http.StripPrefix("/static",
view.ServeFiles("./admin/static"))).ServeHTTP(w, r)
}))
mux.Handle("/", core.RequireAccount(AdminIndexHandler(app)))
// response wrapper to make sure a session cookie exists
@ -44,21 +57,63 @@ func AdminIndexHandler(app *model.AppState) http.Handler {
session := r.Context().Value("session").(*model.Session)
latestRelease, err := controller.GetLatestRelease(app.DB)
releases, err := controller.GetAllReleases(app.DB, false, 3, true)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to pull latest release: %s\n", err)
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
releaseCount, err := controller.GetReleaseCount(app.DB, false)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases count: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
artists, err := controller.GetAllArtists(app.DB)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
artistCount, err := controller.GetArtistCount(app.DB)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artist count: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
tracks, err := controller.GetOrphanTracks(app.DB)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
trackCount, err := controller.GetTrackCount(app.DB)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to pull track count: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
type IndexData struct {
Session *model.Session
LatestRelease *model.Release
core.AdminPageData
Releases []*model.Release
ReleaseCount int
Artists []*model.Artist
ArtistCount int
Tracks []*model.Track
TrackCount int
}
err = templates.IndexTemplate.Execute(w, IndexData{
Session: session,
LatestRelease: latestRelease,
AdminPageData: core.AdminPageData{ Path: r.URL.Path, Session: session },
Releases: releases,
ReleaseCount: releaseCount,
Artists: artists,
ArtistCount: artistCount,
Tracks: tracks,
TrackCount: trackCount,
})
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to render admin index: %s\n", err)
@ -68,23 +123,23 @@ func AdminIndexHandler(app *model.AppState) http.Handler {
})
}
/*
//go:embed "static"
var staticFS embed.FS
func staticHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
info, err := os.Stat(filepath.Join("admin", "static", filepath.Clean(r.URL.Path)))
// does the file exist?
uri := strings.TrimPrefix(r.URL.Path, "/static")
file, err := staticFS.ReadFile(filepath.Join("static", filepath.Clean(uri)))
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}
// is thjs a directory? (forbidden)
if info.IsDir() {
http.NotFound(w, r)
return
}
http.FileServer(http.Dir(filepath.Join("admin", "static"))).ServeHTTP(w, r)
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path)))
w.WriteHeader(http.StatusOK)
w.Write(file)
})
}
*/