2026-03-14 21:31:03 +01:00
|
|
|
// Package admin provides the embedded admin panel static file server and the
|
|
|
|
|
// admin REST API for the OwnCord server.
|
2026-03-14 20:34:37 +01:00
|
|
|
package admin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"embed"
|
2026-03-14 21:31:03 +01:00
|
|
|
"io/fs"
|
2026-03-14 20:34:37 +01:00
|
|
|
"net/http"
|
2026-03-14 21:31:03 +01:00
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/db"
|
|
|
|
|
"github.com/J3vb/OwnCord/Server/service"
|
|
|
|
|
"github.com/J3vb/OwnCord/Server/updater"
|
2026-03-14 21:31:03 +01:00
|
|
|
"github.com/go-chi/chi/v5"
|
2026-03-14 20:34:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed static
|
|
|
|
|
var staticFiles embed.FS
|
|
|
|
|
|
2026-03-14 21:31:03 +01:00
|
|
|
// NewHandler returns an http.Handler that serves both the admin REST API and
|
|
|
|
|
// the embedded admin panel static files.
|
|
|
|
|
//
|
|
|
|
|
// Routes:
|
|
|
|
|
//
|
2026-08-01 22:06:14 +02:00
|
|
|
// /api/* — admin REST API (all require a moderation permission; see NewAdminAPI)
|
2026-03-14 21:31:03 +01:00
|
|
|
// /* — embedded static files (SPA; index.html for unknown paths)
|
2026-08-01 22:06:14 +02:00
|
|
|
func NewHandler(database *db.DB, version string, hub HubBroadcaster, u *updater.Updater, logBuf *RingBuffer, allowedOrigins []string, permInvalidator PermissionInvalidator, mod *service.ModerationService, roles *service.RoleService, opts ...SetupOptions) http.Handler {
|
2026-03-14 21:31:03 +01:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
|
|
// Admin REST API mounted at /api
|
2026-08-01 22:06:14 +02:00
|
|
|
r.Mount("/api", NewAdminAPI(database, version, hub, u, logBuf, allowedOrigins, permInvalidator, mod, roles, opts...))
|
2026-03-14 21:31:03 +01:00
|
|
|
|
2026-03-14 21:37:47 +01:00
|
|
|
// Static files — serve from the "static" sub-tree of the embedded FS.
|
|
|
|
|
// The //go:embed static directive in this package embeds as "static/…",
|
|
|
|
|
// not "admin/static/…", so we strip just "static".
|
|
|
|
|
staticFS, err := fs.Sub(staticFiles, "static")
|
2026-03-14 21:31:03 +01:00
|
|
|
if err != nil {
|
|
|
|
|
// This is a programming error (wrong embed path) and should never
|
|
|
|
|
// happen in production. Panic so it surfaces immediately in tests.
|
|
|
|
|
panic("admin: failed to create static sub-FS: " + err.Error())
|
|
|
|
|
}
|
2026-03-14 22:36:35 +01:00
|
|
|
|
|
|
|
|
// Serve index.html directly for the root path. We read it once at
|
|
|
|
|
// startup instead of using http.FileServer, which has redirect
|
|
|
|
|
// behaviour that conflicts with chi's Mount prefix stripping.
|
|
|
|
|
indexHTML, err := fs.ReadFile(staticFS, "index.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("admin: failed to read index.html: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
r.Get("/", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2026-08-01 22:06:14 +02:00
|
|
|
// img-src adds blob: for the Emoji section: /api/v1/emoji/{id}/image
|
|
|
|
|
// requires an Authorization header, which <img src> cannot send, so
|
|
|
|
|
// each thumbnail is fetched with the session token and swapped in as a
|
|
|
|
|
// blob: URL. blob: is same-origin, opaque and unreadable across
|
|
|
|
|
// documents — it widens nothing an attacker could aim at.
|
2026-03-15 07:07:59 +01:00
|
|
|
w.Header().Set("Content-Security-Policy",
|
2026-08-01 22:06:14 +02:00
|
|
|
"default-src 'self'; img-src 'self' blob:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'")
|
2026-03-30 21:48:14 +02:00
|
|
|
_, _ = w.Write(indexHTML)
|
2026-03-14 22:36:35 +01:00
|
|
|
})
|
2026-03-14 21:31:03 +01:00
|
|
|
r.Handle("/*", http.FileServer(http.FS(staticFS)))
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
}
|