0.1.38 gateway
This commit is contained in:
@@ -5,24 +5,24 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A local preview of the admin console with no gateway behind it.
|
||||
// A canned API for a local React console preview with no gateway behind it.
|
||||
//
|
||||
// In one terminal:
|
||||
//
|
||||
// cd server && ADMIN_PREVIEW=1 go test ./internal/api -run TestAdminPreview -timeout 0
|
||||
// → http://127.0.0.1:7777/admin/overview
|
||||
//
|
||||
// It serves the same finished bytes the real console serves — adminRendered, composed from
|
||||
// the embedded shell, stylesheet and fragments — so what appears here is what a deployment
|
||||
// would show. What it fakes is only the data: /admin/api/* answers from the canned status
|
||||
// below, which is what makes it runnable without Postgres, Redis or an Emby to talk to.
|
||||
// In another:
|
||||
//
|
||||
// It is a test so that it can reach adminRendered, which is unexported for the good reason
|
||||
// that composing the console is nobody else's business. It skips unless ADMIN_PREVIEW is
|
||||
// set, so an ordinary `go test ./...` never blocks on a server that runs until interrupted.
|
||||
// cd admin-ui && npm run dev
|
||||
//
|
||||
// Vite owns the React shell at http://127.0.0.1:5180/admin/ and proxies its /admin/api/*
|
||||
// requests here. The preview therefore fakes only data, leaving the same built console
|
||||
// responsible for every route and asset it will own in deployment. It skips unless
|
||||
// ADMIN_PREVIEW is set, so an ordinary `go test ./...` never blocks on this server.
|
||||
func TestAdminPreview(t *testing.T) {
|
||||
if os.Getenv("ADMIN_PREVIEW") == "" {
|
||||
t.Skip("set ADMIN_PREVIEW=1 to serve the console locally")
|
||||
@@ -34,35 +34,14 @@ func TestAdminPreview(t *testing.T) {
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/admin/api/", func(w http.ResponseWriter, r *http.Request) {
|
||||
body, ok := adminPreviewData()[strings.TrimSuffix(r.URL.Path, "/")]
|
||||
body, ok := adminPreviewData()[r.URL.Path]
|
||||
if !ok {
|
||||
body = map[string]any{}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
})
|
||||
mux.HandleFunc("/admin/", func(w http.ResponseWriter, r *http.Request) {
|
||||
id := strings.TrimPrefix(r.URL.Path, "/admin/")
|
||||
if id == "" {
|
||||
id = "overview"
|
||||
}
|
||||
// A hidden page is addressed by a route carrying something else in the path, so the
|
||||
// preview takes the first segment and lets /admin/accounts/42 render the account page.
|
||||
id, _, _ = strings.Cut(id, "/")
|
||||
page, ok := adminRendered[id]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_, _ = w.Write(page)
|
||||
})
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/admin/overview", http.StatusFound)
|
||||
})
|
||||
|
||||
fmt.Printf("\nadmin console preview → http://%s/admin/overview (ctrl-c to stop)\n\n", address)
|
||||
fmt.Printf("\ncanned admin API → http://%s; open Vite at http://127.0.0.1:5180/admin/ (ctrl-c to stop)\n\n", address)
|
||||
if err := http.ListenAndServe(address, mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user