2024-04-16 22:58:39 +01:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2025-07-15 16:40:15 +01:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"arimelody-web/admin/account"
|
|
|
|
"arimelody-web/admin/auth"
|
|
|
|
"arimelody-web/admin/core"
|
|
|
|
"arimelody-web/admin/logs"
|
|
|
|
"arimelody-web/admin/music"
|
|
|
|
"arimelody-web/admin/templates"
|
|
|
|
"arimelody-web/controller"
|
|
|
|
"arimelody-web/model"
|
2024-04-16 22:58:39 +01:00
|
|
|
)
|
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
func Handler(app *model.AppState) http.Handler {
|
2024-07-31 13:45:34 +01:00
|
|
|
mux := http.NewServeMux()
|
2024-07-31 04:09:22 +01:00
|
|
|
|
2025-07-15 16:40:15 +01:00
|
|
|
mux.Handle("/register", auth.RegisterAccountHandler(app))
|
|
|
|
mux.Handle("/login", auth.LoginHandler(app))
|
|
|
|
mux.Handle("/totp", auth.LoginTOTPHandler(app))
|
|
|
|
mux.Handle("/logout", core.RequireAccount(auth.LogoutHandler(app)))
|
2025-01-26 00:48:19 +00:00
|
|
|
|
2025-07-15 16:40:15 +01:00
|
|
|
mux.Handle("/music/", core.RequireAccount(http.StripPrefix("/music", music.Handler(app))))
|
|
|
|
mux.Handle("/logs", core.RequireAccount(logs.Handler(app)))
|
|
|
|
mux.Handle("/account/", core.RequireAccount(http.StripPrefix("/account", account.Handler(app))))
|
2025-01-23 00:37:19 +00:00
|
|
|
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", staticHandler()))
|
2025-07-15 16:40:15 +01:00
|
|
|
mux.Handle("/", core.RequireAccount(AdminIndexHandler(app)))
|
2025-01-23 00:37:19 +00:00
|
|
|
|
|
|
|
// response wrapper to make sure a session cookie exists
|
2025-07-15 16:40:15 +01:00
|
|
|
return core.EnforceSession(app, mux)
|
2025-01-23 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AdminIndexHandler(app *model.AppState) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-02 22:48:26 +01:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-23 00:37:19 +00:00
|
|
|
session := r.Context().Value("session").(*model.Session)
|
2024-08-02 22:48:26 +01:00
|
|
|
|
2025-07-15 16:40:15 +01:00
|
|
|
latestRelease, err := controller.GetLatestRelease(app.DB)
|
2024-09-01 04:43:32 +01:00
|
|
|
if err != nil {
|
2025-07-15 16:40:15 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull latest release: %s\n", err)
|
2025-01-23 00:37:19 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 04:43:32 +01:00
|
|
|
|
2024-09-03 08:07:45 +01:00
|
|
|
type IndexData struct {
|
2025-07-15 16:40:15 +01:00
|
|
|
Session *model.Session
|
|
|
|
LatestRelease *model.Release
|
2024-08-03 23:24:15 +01:00
|
|
|
}
|
|
|
|
|
2025-07-15 16:40:15 +01:00
|
|
|
err = templates.IndexTemplate.Execute(w, IndexData{
|
2025-01-23 00:37:19 +00:00
|
|
|
Session: session,
|
2025-07-15 16:40:15 +01:00
|
|
|
LatestRelease: latestRelease,
|
2024-08-31 15:25:44 +01:00
|
|
|
})
|
2025-01-23 00:37:19 +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)
|
2025-01-23 00:37:19 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-02 00:53:19 +01: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 22:58:39 +01:00
|
|
|
|
2024-08-02 00:53:19 +01: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 22:58:39 +01:00
|
|
|
}
|