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:
cflmyandCursor committed 2026-09-10 16:41:59 +08:00
1 parent 1fc985db7e
commit 1ef71d7492
9 files changed
+1276 -1

No files matched your search

@@ -0,0 +1,402 @@
// Package middleware ports declarative CORS/security/compress/JSON configure
// from the Rust web plugin (配置即数据、装配即函数).
package middleware
import (
"fmt"
"strconv"
"strings"
"github.com/marqdo/marqdo/plugins/web/internal/table"
)
// Config is the runtime view of app["middleware"].
type Config struct {
CORS *CORSConfig
Security [][2]string
Compress bool
BodyLimit *uint64
AccessLog bool
CacheControl string
JSONRoutes []JSONRouteMount
}
// CORSConfig matches middleware.cors map shape.
type CORSConfig struct {
AllowOrigins []string
Methods []string
Headers []string
ExposeHeaders []string
Credentials bool
}
// JSONRouteMount is one JSON API route with leading-slash path.
type JSONRouteMount struct {
Path string
Method string
Table string
Where any
Order string
Limit int64
}
// Configure merges configure tables into app.middleware (Rust web_app_middleware).
func Configure(appBag map[string]any, args map[string]any) (map[string]any, error) {
out := clone(appBag)
mw, _ := out["middleware"].(map[string]any)
if mw == nil {
mw = map[string]any{}
} else {
mw = clone(mw)
}
if v, ok := args["cors"]; ok && v != nil {
mw["cors"] = CorsFromTable(v)
}
if v, ok := args["security"]; ok && v != nil {
mw["security"] = SecurityFromTable(v)
}
if v, ok := args["compress"]; ok && v != nil {
mw["compress"] = boolish(v)
}
if v, ok := first(args, "access_log", "访问日志"); ok {
mw["access_log"] = boolish(v)
}
if v, ok := first(args, "cache_control", "缓存控制"); ok {
if s := strings.TrimSpace(cellText(v)); s != "" {
mw["cache_control"] = s
}
}
if v, ok := args["body_limit"]; ok && v != nil {
if n, ok := asUint64(v); ok && n > 0 {
mw["body_limit"] = float64(n)
}
}
if v, ok := first(args, "json_routes", "json"); ok && v != nil {
mw["json_routes"] = JSONRoutesFromTable(v)
}
out["middleware"] = mw
return out, nil
}
// Parse reads app["middleware"] into Config (Rust middleware::parse).
func Parse(app map[string]any) Config {
var cfg Config
obj, _ := app["middleware"].(map[string]any)
if obj == nil {
return cfg
}
if c, ok := obj["cors"].(map[string]any); ok {
cfg.CORS = parseCORS(c)
}
if sec, ok := obj["security"].(map[string]any); ok {
for k, v := range sec {
s := cellText(v)
if s != "" {
cfg.Security = append(cfg.Security, [2]string{k, s})
}
}
}
cfg.Compress = boolish(obj["compress"])
cfg.AccessLog = boolish(obj["access_log"])
if s := strings.TrimSpace(cellText(obj["cache_control"])); s != "" {
cfg.CacheControl = s
}
if n, ok := asUint64(obj["body_limit"]); ok && n > 0 {
cfg.BodyLimit = &n
}
if routes, ok := obj["json_routes"].(map[string]any); ok {
for path, spec := range routes {
if r, ok := parseJSONRoute(path, spec); ok {
cfg.JSONRoutes = append(cfg.JSONRoutes, r)
}
}
sortJSONRoutes(cfg.JSONRoutes)
}
return cfg
}
func parseCORS(obj map[string]any) *CORSConfig {
return &CORSConfig{
AllowOrigins: strList(obj["allow_origins"]),
Methods: strList(obj["methods"]),
Headers: strList(obj["headers"]),
ExposeHeaders: strList(obj["expose_headers"]),
Credentials: boolish(obj["credentials"]),
}
}
func parseJSONRoute(path string, spec any) (JSONRouteMount, bool) {
m, ok := spec.(map[string]any)
if !ok {
return JSONRouteMount{}, false
}
tableName := strings.TrimSpace(cellText(m["table"]))
if tableName == "" {
return JSONRouteMount{}, false
}
method := strings.ToUpper(strings.TrimSpace(cellText(m["method"])))
if method == "" {
method = "GET"
}
limit := int64(200)
if n, ok := asInt64(m["limit"]); ok {
limit = n
}
order := strings.TrimSpace(cellText(m["order"]))
p := "/" + strings.TrimPrefix(strings.TrimSpace(path), "/")
r := JSONRouteMount{
Path: p,
Method: method,
Table: tableName,
Order: order,
Limit: limit,
}
if w, ok := m["where"]; ok && w != nil {
r.Where = w
}
return r, true
}
func sortJSONRoutes(rs []JSONRouteMount) {
for i := 0; i < len(rs); i++ {
for j := i + 1; j < len(rs); j++ {
if rs[j].Path < rs[i].Path {
rs[i], rs[j] = rs[j], rs[i]
}
}
}
}
// CorsFromTable normalizes `|允许来源|方法|头|暴露头|凭证|` into middleware.cors.
func CorsFromTable(tableV any) map[string]any {
rows, _ := table.AsRows(tableV).([]any)
var allowOrigins, methods, headers, expose []string
credentials := false
for _, row := range rows {
m, ok := row.(map[string]any)
if !ok {
continue
}
origin := col(m, "允许来源", "origin", "Origin")
if origin != "" && origin != "*" {
allowOrigins = append(allowOrigins, origin)
}
for _, part := range strings.Split(col(m, "方法", "methods", "Methods"), ",") {
part = strings.TrimSpace(part)
if part != "" {
methods = append(methods, part)
}
}
for _, part := range strings.Split(col(m, "头", "headers", "Headers"), ",") {
part = strings.TrimSpace(part)
if part != "" {
headers = append(headers, part)
}
}
for _, part := range strings.Split(col(m, "暴露头", "expose_headers", "Expose-Headers"), ",") {
part = strings.TrimSpace(part)
if part != "" {
expose = append(expose, part)
}
}
cred := col(m, "凭证", "credentials", "Credentials")
if strings.EqualFold(cred, "true") || cred == "1" || strings.EqualFold(cred, "yes") {
credentials = true
}
}
return map[string]any{
"allow_origins": toAnyList(allowOrigins),
"methods": toAnyList(methods),
"headers": toAnyList(headers),
"expose_headers": toAnyList(expose),
"credentials": credentials,
}
}
// SecurityFromTable normalizes `|头|值|` into middleware.security map.
func SecurityFromTable(tableV any) map[string]any {
rows, _ := table.AsRows(tableV).([]any)
out := map[string]any{}
for _, row := range rows {
m, ok := row.(map[string]any)
if !ok {
continue
}
k := col(m, "头", "header", "Header", "name")
v := col(m, "值", "value", "Value")
if k != "" {
out[k] = v
}
}
return out
}
// JSONRoutesFromTable normalizes `|路径|方法|表|条件|排序|上限|` into json_routes.
func JSONRoutesFromTable(tableV any) map[string]any {
rows, _ := table.AsRows(tableV).([]any)
out := map[string]any{}
for _, row := range rows {
m, ok := row.(map[string]any)
if !ok {
continue
}
path := strings.TrimPrefix(strings.TrimSpace(col(m, "路径", "path", "Path")), "/")
if path == "" {
continue
}
method := strings.ToUpper(strings.TrimSpace(col(m, "方法", "method", "Method")))
if method == "" {
method = "GET"
}
tbl := col(m, "表", "table", "Table")
if tbl == "" {
continue
}
order := col(m, "排序", "order", "Order")
limit := int64(200)
if s := col(m, "上限", "limit", "Limit"); s != "" {
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
limit = n
}
}
spec := map[string]any{
"method": method,
"table": tbl,
"limit": float64(limit),
}
if order != "" {
spec["order"] = order
}
out[path] = spec
}
return out
}
func col(m map[string]any, keys ...string) string {
for _, k := range keys {
if v, ok := m[k]; ok {
return cellText(v)
}
}
return ""
}
func cellText(v any) string {
switch t := v.(type) {
case nil:
return ""
case string:
return t
case bool:
return strconv.FormatBool(t)
case float64:
if t == float64(int64(t)) {
return strconv.FormatInt(int64(t), 10)
}
return strconv.FormatFloat(t, 'f', -1, 64)
case int:
return strconv.Itoa(t)
case int64:
return strconv.FormatInt(t, 10)
default:
return fmt.Sprint(t)
}
}
func boolish(v any) bool {
switch t := v.(type) {
case bool:
return t
case string:
switch strings.TrimSpace(t) {
case "true", "True", "1", "yes", "on", "真":
return true
}
return false
case float64:
return t != 0
case int:
return t != 0
case int64:
return t != 0
}
return false
}
func asUint64(v any) (uint64, bool) {
switch t := v.(type) {
case float64:
if t > 0 {
return uint64(t), true
}
case int64:
if t > 0 {
return uint64(t), true
}
case int:
if t > 0 {
return uint64(t), true
}
case string:
n, err := strconv.ParseUint(strings.TrimSpace(t), 10, 64)
return n, err == nil && n > 0
}
return 0, false
}
func asInt64(v any) (int64, bool) {
switch t := v.(type) {
case float64:
return int64(t), true
case int64:
return t, true
case int:
return int64(t), true
case string:
n, err := strconv.ParseInt(strings.TrimSpace(t), 10, 64)
return n, err == nil
}
return 0, false
}
func strList(v any) []string {
switch t := v.(type) {
case []any:
out := make([]string, 0, len(t))
for _, x := range t {
if s, ok := x.(string); ok {
out = append(out, s)
}
}
return out
case []string:
return append([]string{}, t...)
}
return nil
}
func toAnyList(ss []string) []any {
out := make([]any, len(ss))
for i, s := range ss {
out[i] = s
}
return out
}
func first(args map[string]any, keys ...string) (any, bool) {
for _, k := range keys {
if v, ok := args[k]; ok && v != nil {
return v, true
}
}
return nil, false
}
func clone(m map[string]any) map[string]any {
out := make(map[string]any, len(m))
for k, v := range m {
out[k] = v
}
return out
}
@@ -0,0 +1,112 @@
package middleware_test
import (
"testing"
"github.com/marqdo/marqdo/plugins/web/internal/app"
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
)
func TestConfigureCORSOrigins(t *testing.T) {
cors := map[string]any{
"允许来源": []any{"https://a.example", "https://b.example"},
"方法": []any{"GET,POST", "GET"},
"头": []any{"Content-Type,Authorization", "Content-Type"},
"暴露头": []any{"X-Total", ""},
"凭证": []any{"true", ""},
}
sec := map[string]any{
"头": []any{"X-Frame-Options", "X-Content-Type-Options", "Content-Security-Policy"},
"值": []any{"DENY", "nosniff", "default-src 'self'"},
}
api := map[string]any{
"路径": []any{"/api/posts", "/api/publish"},
"方法": []any{"GET", "POST"},
"表": []any{"articles", "articles"},
"条件": []any{"", ""},
"排序": []any{"-created_at", ""},
"上限": []any{"20", "10"},
}
a := app.New(map[string]any{"page": map[string]any{"title": "mw"}})
out, err := middleware.Configure(a, map[string]any{
"cors": cors,
"security": sec,
"compress": true,
"body_limit": float64(1048576),
"json": api,
})
if err != nil {
t.Fatal(err)
}
mw, _ := out["middleware"].(map[string]any)
if mw == nil {
t.Fatal("missing middleware")
}
cobj, _ := mw["cors"].(map[string]any)
origins, _ := cobj["allow_origins"].([]any)
if len(origins) < 1 || origins[0] != "https://a.example" {
t.Fatalf("allow_origins=%v", origins)
}
methods, _ := cobj["methods"].([]any)
if len(methods) < 2 || methods[1] != "POST" {
t.Fatalf("methods=%v", methods)
}
expose, _ := cobj["expose_headers"].([]any)
if len(expose) < 1 || expose[0] != "X-Total" {
t.Fatalf("expose=%v", expose)
}
if cred, _ := cobj["credentials"].(bool); !cred {
t.Fatal("credentials want true")
}
secmap, _ := mw["security"].(map[string]any)
if secmap["X-Frame-Options"] != "DENY" {
t.Fatalf("security frame=%v", secmap["X-Frame-Options"])
}
if mw["compress"] != true {
t.Fatalf("compress=%v", mw["compress"])
}
if mw["body_limit"] != float64(1048576) {
t.Fatalf("body_limit=%v", mw["body_limit"])
}
jmap, _ := mw["json_routes"].(map[string]any)
posts, _ := jmap["api/posts"].(map[string]any)
if posts["method"] != "GET" || posts["table"] != "articles" {
t.Fatalf("posts=%v", posts)
}
if posts["order"] != "-created_at" || posts["limit"] != float64(20) {
t.Fatalf("posts order/limit=%v", posts)
}
pub, _ := jmap["api/publish"].(map[string]any)
if pub["method"] != "POST" {
t.Fatalf("publish=%v", pub)
}
}
func TestParseReadsBag(t *testing.T) {
a := map[string]any{
"middleware": map[string]any{
"cors": map[string]any{
"allow_origins": []any{"https://a.example"},
"methods": []any{"GET"},
"credentials": true,
},
"compress": true,
"body_limit": float64(1000),
"json_routes": map[string]any{
"api/posts": map[string]any{"method": "GET", "table": "articles", "limit": float64(10)},
},
},
}
cfg := middleware.Parse(a)
if cfg.CORS == nil || len(cfg.CORS.AllowOrigins) != 1 {
t.Fatalf("cors=%v", cfg.CORS)
}
if !cfg.Compress || cfg.BodyLimit == nil || *cfg.BodyLimit != 1000 {
t.Fatalf("cfg=%+v", cfg)
}
if len(cfg.JSONRoutes) != 1 || cfg.JSONRoutes[0].Path != "/api/posts" {
t.Fatalf("json routes=%v", cfg.JSONRoutes)
}
}