feat(web): Go W-G4 listen, static, middleware, and JSON routes
Add app.static / configure middleware bags and blocking httpx.Listen (home, routes, _part, static, forms, JSON API, CORS/gzip layers). Pass route/static/middleware/part offline smokes against libweb.so. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
1fc985db7e
commit
1ef71d7492
9 files changed
+1276
-1
No files matched your search
@@ -109,6 +109,49 @@ func MountForm(appBag map[string]any, id string, form any) (map[string]any, erro
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// NormalizeStaticMount mirrors Rust http::normalize_static_mount.
|
||||
func NormalizeStaticMount(raw string) string {
|
||||
s := strings.TrimSpace(raw)
|
||||
if s == "" {
|
||||
return "/static"
|
||||
}
|
||||
m := s
|
||||
if !strings.HasPrefix(m, "/") {
|
||||
m = "/" + m
|
||||
}
|
||||
for len(m) > 1 && strings.HasSuffix(m, "/") {
|
||||
m = strings.TrimSuffix(m, "/")
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Static sets static_dir + static_mount (Rust web_app_static).
|
||||
func Static(appBag map[string]any, dir, mount string) (map[string]any, error) {
|
||||
dir = strings.TrimSpace(dir)
|
||||
if dir == "" {
|
||||
return nil, fmt.Errorf("static `dir` is empty")
|
||||
}
|
||||
if mount == "" {
|
||||
mount = "/static"
|
||||
}
|
||||
mount = NormalizeStaticMount(mount)
|
||||
if mount == "/_form" || strings.HasPrefix(mount, "/_form/") ||
|
||||
mount == "/_part" || strings.HasPrefix(mount, "/_part/") {
|
||||
return nil, fmt.Errorf("static mount `%s` is reserved", mount)
|
||||
}
|
||||
if boolish(appBag["admin"]) {
|
||||
prefix := strOpt(appBag, "admin_prefix", "/admin")
|
||||
prefix = NormalizeStaticMount(prefix)
|
||||
if mount == prefix || strings.HasPrefix(mount, prefix+"/") {
|
||||
return nil, fmt.Errorf("static mount `%s` is reserved", mount)
|
||||
}
|
||||
}
|
||||
out := clone(appBag)
|
||||
out["static_dir"] = dir
|
||||
out["static_mount"] = mount
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func normalizeRoutePath(raw string) (string, error) {
|
||||
s := strings.TrimSpace(raw)
|
||||
if s == "" {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
)
|
||||
|
||||
func TestNormalizeStaticMount(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"", "/static"},
|
||||
{" ", "/static"},
|
||||
{"static", "/static"},
|
||||
{"/static/", "/static"},
|
||||
{"/assets", "/assets"},
|
||||
{"assets/", "/assets"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := app.NormalizeStaticMount(c.in); got != c.want {
|
||||
t.Fatalf("NormalizeStaticMount(%q)=%q want %q", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticSetsDirAndMount(t *testing.T) {
|
||||
a := app.New(map[string]any{"page": map[string]any{"title": "Home"}})
|
||||
out, err := app.Static(a, "web-fixtures/public", "/static")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out["static_dir"] != "web-fixtures/public" {
|
||||
t.Fatalf("static_dir=%v", out["static_dir"])
|
||||
}
|
||||
if out["static_mount"] != "/static" {
|
||||
t.Fatalf("static_mount=%v", out["static_mount"])
|
||||
}
|
||||
out2, err := app.Static(out, "assets", "/assets/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out2["static_mount"] != "/assets" {
|
||||
t.Fatalf("custom mount=%v", out2["static_mount"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticRejectsReserved(t *testing.T) {
|
||||
a := app.New(nil)
|
||||
for _, mount := range []string{"/_form", "/_form/x", "/_part", "/_part/nav"} {
|
||||
_, err := app.Static(a, "pub", mount)
|
||||
if err == nil || !strings.Contains(err.Error(), "reserved") {
|
||||
t.Fatalf("mount %q: want reserved error, got %v", mount, err)
|
||||
}
|
||||
}
|
||||
admin := app.New(map[string]any{"admin": true, "admin_prefix": "/admin"})
|
||||
_, err := app.Static(admin, "pub", "/admin")
|
||||
if err == nil || !strings.Contains(err.Error(), "reserved") {
|
||||
t.Fatalf("admin mount: want reserved, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user