2024-04-16 21:58:39 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2024-07-31 03:09:22 +00:00
|
|
|
"context"
|
2024-04-16 21:58:39 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-07-31 03:09:22 +00:00
|
|
|
"os"
|
2024-08-01 23:53:19 +00:00
|
|
|
"path/filepath"
|
2024-07-31 03:09:22 +00:00
|
|
|
|
2025-01-20 10:34:39 +00:00
|
|
|
"arimelody-web/controller"
|
|
|
|
"arimelody-web/model"
|
2025-01-20 11:47:38 +00:00
|
|
|
|
2025-01-20 15:08:01 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2024-04-16 21:58:39 +00:00
|
|
|
)
|
|
|
|
|
2025-01-21 00:20:07 +00:00
|
|
|
func Handler(db *sqlx.DB) http.Handler {
|
2024-07-31 12:45:34 +00:00
|
|
|
mux := http.NewServeMux()
|
2024-07-31 03:09:22 +00:00
|
|
|
|
2025-01-21 00:20:07 +00:00
|
|
|
mux.Handle("/login", LoginHandler(db))
|
|
|
|
mux.Handle("/register", createAccountHandler(db))
|
|
|
|
mux.Handle("/logout", RequireAccount(db, LogoutHandler(db)))
|
|
|
|
mux.Handle("/account", RequireAccount(db, AccountHandler(db)))
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", staticHandler()))
|
2025-01-21 00:20:07 +00:00
|
|
|
mux.Handle("/release/", RequireAccount(db, http.StripPrefix("/release", serveRelease())))
|
|
|
|
mux.Handle("/artist/", RequireAccount(db, http.StripPrefix("/artist", serveArtist())))
|
|
|
|
mux.Handle("/track/", RequireAccount(db, http.StripPrefix("/track", serveTrack())))
|
2024-07-31 12:45:34 +00:00
|
|
|
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-02 21:48:26 +00:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-21 00:20:07 +00:00
|
|
|
account, err := controller.GetAccountByRequest(db, r)
|
2025-01-20 15:08:01 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %s\n", err)
|
|
|
|
}
|
|
|
|
if account == nil {
|
2024-08-02 21:48:26 +00:00
|
|
|
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-21 00:20:07 +00:00
|
|
|
releases, err := controller.GetAllReleases(db, false, 0, true)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2025-01-20 15:08:01 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %s\n", err)
|
2024-09-01 03:43:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-21 00:20:07 +00:00
|
|
|
artists, err := controller.GetAllArtists(db)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2025-01-20 15:08:01 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %s\n", err)
|
2024-09-01 03:43:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-21 00:20:07 +00:00
|
|
|
tracks, err := controller.GetOrphanTracks(db)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2025-01-20 15:08:01 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %s\n", err)
|
2024-09-01 03:43:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
type IndexData struct {
|
2025-01-20 15:08:01 +00:00
|
|
|
Account *model.Account
|
2025-01-20 10:34:39 +00:00
|
|
|
Releases []*model.Release
|
|
|
|
Artists []*model.Artist
|
|
|
|
Tracks []*model.Track
|
2024-08-03 22:24:15 +00:00
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
err = pages["index"].Execute(w, IndexData{
|
2025-01-20 15:08:01 +00:00
|
|
|
Account: account,
|
2024-09-01 03:43:32 +00:00
|
|
|
Releases: releases,
|
|
|
|
Artists: artists,
|
2024-08-03 23:23:55 +00:00
|
|
|
Tracks: tracks,
|
2024-08-31 14:25:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2025-01-20 15:08:01 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to render admin index: %s\n", err)
|
2024-08-31 14:25:44 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-07-31 12:45:34 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
return mux
|
2024-07-31 03:09:22 +00:00
|
|
|
}
|
|
|
|
|
2025-01-20 15:08:01 +00:00
|
|
|
func RequireAccount(db *sqlx.DB, next http.Handler) http.HandlerFunc {
|
2024-07-31 03:09:22 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2025-01-20 15:08:01 +00:00
|
|
|
account, err := controller.GetAccountByRequest(db, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
2025-01-20 15:08:01 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2025-01-20 18:54:03 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
|
2024-07-31 03:09:22 +00:00
|
|
|
return
|
|
|
|
}
|
2025-01-20 15:08:01 +00:00
|
|
|
if account == nil {
|
|
|
|
// TODO: include context in redirect
|
|
|
|
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
|
|
|
return
|
2024-08-02 21:48:26 +00:00
|
|
|
}
|
|
|
|
|
2025-01-20 15:08:01 +00:00
|
|
|
ctx := context.WithValue(r.Context(), "account", account)
|
2024-07-31 03:09:22 +00:00
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2024-04-16 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 23:53:19 +00:00
|
|
|
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?
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 21:58:39 +00:00
|
|
|
|
2024-08-01 23:53:19 +00:00
|
|
|
// is thjs a directory? (forbidden)
|
|
|
|
if info.IsDir() {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.FileServer(http.Dir(filepath.Join("admin", "static"))).ServeHTTP(w, r)
|
|
|
|
})
|
2024-04-16 21:58:39 +00:00
|
|
|
}
|