diff --git a/doc/design/ext-web-go-rewrite.md b/doc/design/ext-web-go-rewrite.md index 6a59844..6f1a2d4 100644 --- a/doc/design/ext-web-go-rewrite.md +++ b/doc/design/ext-web-go-rewrite.md @@ -395,7 +395,7 @@ scripts/build-web-plugin.sh # go build -buildmode=c-shared -o …/libweb.so | **W-G7** | SEO RSS sitemap robots redirect error paginate markdown | ✅ `web-content` / `web-w7-*` | | **W-G8** | WS echo/broadcast + access_log | ✅ `web-ws-broadcast-*` | | **W-G9** | Postgres + Redis cache + file/S3 storage | ✅ `web-drivers-smoke` | -| **W-G10** | proxy + invoke + nested plugin | `web-proxy-invoke` / hosting live | +| **W-G10** | proxy + invoke + nested plugin | ✅ `web-proxy-invoke` / hosting live | | **W-G11** | 定制 C2–C4 shell/layout/style/nav | `web-c2`–`c4` | | **W-G12** | G 增强 WS rooms + Redis session/pubsub + API key 原语 | 单测 + anlian chat 切片 | | **W-G13** | 默认切换 Go `libweb`;归档 Rust;更新发版与 ext CLI | 全量 `tests/ext/web-*` 绿 | diff --git a/plugins/web/abi_w10.go b/plugins/web/abi_w10.go new file mode 100644 index 0000000..387a45d --- /dev/null +++ b/plugins/web/abi_w10.go @@ -0,0 +1,43 @@ +package main + +/* +#cgo CFLAGS: -I${SRCDIR}/abi +#include "marqdo_abi_cgo.h" + +extern int web_app_proxy(char *args_json, char **out_json, char **err_msg); +extern int web_app_invoke(char *args_json, char **out_json, char **err_msg); +*/ +import "C" + +import ( + "github.com/marqdo/marqdo/plugins/web/internal/invoke" + "github.com/marqdo/marqdo/plugins/web/internal/proxy" +) + +//export web_app_proxy +func web_app_proxy(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + opts, err := proxy.OptionsFromArgs(args) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + out, err := proxy.Register(asPageMap(args["app"]), opts) + return replyJSON(outJSON, errMsg, out, err) +} + +//export web_app_invoke +func web_app_invoke(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + opts, err := invoke.OptionsFromArgs(args) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + out, err := invoke.Register(asPageMap(args["app"]), opts) + return replyJSON(outJSON, errMsg, out, err) +} diff --git a/plugins/web/internal/invoke/invoke.go b/plugins/web/internal/invoke/invoke.go new file mode 100644 index 0000000..dca88ed --- /dev/null +++ b/plugins/web/internal/invoke/invoke.go @@ -0,0 +1,174 @@ +// Package invoke ports offline invoke route bags (Rust invoke.rs). +package invoke + +import ( + "fmt" + "strings" + + "github.com/marqdo/marqdo/plugins/web/internal/app" + "github.com/marqdo/marqdo/plugins/web/internal/table" +) + +// Options is one invoke mount (web_app_invoke args). +type Options struct { + Path string + Method string + Fn string + Body string + Return string +} + +// RoutesFromTable normalizes `|路径|方法|函数|正文|返回|` into invoke_routes map keys. +func RoutesFromTable(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.Trim(strings.TrimSpace(col(m, "路径", "path", "Path")), "/") + if path == "" { + continue + } + fnPath := col(m, "函数", "fn", "function", "Function") + if fnPath == "" { + continue + } + method := strings.ToUpper(strings.TrimSpace(col(m, "方法", "method", "Method"))) + if method == "" { + method = "POST" + } + body := strings.ToLower(strings.TrimSpace(col(m, "正文", "body", "Body"))) + if body == "" { + body = "json" + } + ret := strings.ToLower(strings.TrimSpace(col(m, "返回", "return", "Return"))) + if ret == "" { + ret = "json" + } + out[path] = routeEntry(method, fnPath, body, ret) + } + return out +} + +// Register adds one invoke route to app.invoke_routes (Rust web_app_invoke). +func Register(appBag map[string]any, opts Options) (map[string]any, error) { + fnPath := strings.TrimSpace(opts.Fn) + if fnPath == "" { + return nil, fmt.Errorf("invoke requires `fn`") + } + if !strings.Contains(fnPath, ".") || strings.Contains(fnPath, "/") || strings.Contains(fnPath, "#") { + return nil, fmt.Errorf("invoke `fn` must be `lib.member` (imported on the entry module)") + } + path, err := app.NormalizeRoutePath(opts.Path, appBag) + if err != nil { + return nil, err + } + key := strings.TrimPrefix(path, "/") + method := strings.ToUpper(strings.TrimSpace(opts.Method)) + if method == "" { + method = "POST" + } + body := strings.ToLower(strings.TrimSpace(opts.Body)) + if body == "" { + body = "json" + } + ret := strings.ToLower(strings.TrimSpace(opts.Return)) + if ret == "" { + ret = "json" + } + out := clone(appBag) + routes := map[string]any{} + if r, ok := out["invoke_routes"].(map[string]any); ok { + routes = clone(r) + } + routes[key] = routeEntry(method, fnPath, body, ret) + out["invoke_routes"] = routes + return out, nil +} + +// OptionsFromArgs parses web_app_invoke ABI args. +func OptionsFromArgs(args map[string]any) (Options, error) { + path, err := argStrReq(args, "path", "路径") + if err != nil { + return Options{}, err + } + fnPath, err := argStrReq(args, "fn", "function", "函数") + if err != nil { + return Options{}, err + } + method, _ := argStr(args, "method", "方法") + body, _ := argStr(args, "body", "正文") + ret, _ := argStr(args, "return", "返回") + return Options{ + Path: path, + Method: method, + Fn: fnPath, + Body: body, + Return: ret, + }, nil +} + +func routeEntry(method, fnPath, body, ret string) map[string]any { + return map[string]any{ + "method": method, + "fn": fnPath, + "body": body, + "return": ret, + } +} + +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 + default: + return fmt.Sprint(t) + } +} + +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 argStr(args map[string]any, keys ...string) (string, bool) { + v, ok := first(args, keys...) + if !ok { + return "", false + } + s, _ := v.(string) + return s, true +} + +func argStrReq(args map[string]any, keys ...string) (string, error) { + s, ok := argStr(args, keys...) + if !ok || strings.TrimSpace(s) == "" { + return "", fmt.Errorf("missing `%s`", keys[0]) + } + return s, nil +} + +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 +} diff --git a/plugins/web/internal/invoke/invoke_test.go b/plugins/web/internal/invoke/invoke_test.go new file mode 100644 index 0000000..3c348fe --- /dev/null +++ b/plugins/web/internal/invoke/invoke_test.go @@ -0,0 +1,84 @@ +package invoke_test + +import ( + "strings" + "testing" + + "github.com/marqdo/marqdo/plugins/web/internal/app" + "github.com/marqdo/marqdo/plugins/web/internal/invoke" + "github.com/marqdo/marqdo/plugins/web/internal/middleware" +) + +func TestRoutesFromTable(t *testing.T) { + tbl := map[string]any{ + "path": []any{"/api/echo"}, + "method": []any{"POST"}, + "fn": []any{"demo.echo"}, + "body": []any{"json"}, + "return": []any{"json"}, + } + routes := invoke.RoutesFromTable(tbl) + echo, ok := routes["api/echo"].(map[string]any) + if !ok { + t.Fatalf("routes=%v", routes) + } + if echo["fn"] != "demo.echo" { + t.Fatalf("fn=%v", echo["fn"]) + } + if echo["method"] != "POST" { + t.Fatalf("method=%v", echo["method"]) + } +} + +func TestRegisterInvokeRoute(t *testing.T) { + a := app.New(nil) + out, err := invoke.Register(a, invoke.Options{ + Path: "/api/ping", + Method: "GET", + Fn: "demo.ping", + Body: "query", + }) + if err != nil { + t.Fatal(err) + } + routes := out["invoke_routes"].(map[string]any) + ping, ok := routes["api/ping"].(map[string]any) + if !ok { + t.Fatalf("routes=%v", routes) + } + if ping["method"] != "GET" { + t.Fatalf("method=%v", ping["method"]) + } + if ping["body"] != "query" { + t.Fatalf("body=%v", ping["body"]) + } +} + +func TestRegisterRejectsInvalidFn(t *testing.T) { + _, err := invoke.Register(app.New(nil), invoke.Options{ + Path: "/api/x", + Fn: "not-valid", + }) + if err == nil || !strings.Contains(err.Error(), "lib.member") { + t.Fatalf("want lib.member error, got %v", err) + } +} + +func TestConfigureMergesInvokeTable(t *testing.T) { + a := app.New(nil) + invokeTbl := map[string]any{ + "path": []any{"/api/echo"}, + "method": []any{"POST"}, + "fn": []any{"demo.echo"}, + "body": []any{"json"}, + "return": []any{"json"}, + } + out, err := middleware.Configure(a, map[string]any{"invoke": invokeTbl}) + if err != nil { + t.Fatal(err) + } + routes := out["invoke_routes"].(map[string]any) + if routes["api/echo"] == nil { + t.Fatalf("invoke_routes=%v", routes) + } +} diff --git a/plugins/web/internal/middleware/middleware.go b/plugins/web/internal/middleware/middleware.go index b0a8ef5..2493810 100644 --- a/plugins/web/internal/middleware/middleware.go +++ b/plugins/web/internal/middleware/middleware.go @@ -7,6 +7,8 @@ import ( "strconv" "strings" + "github.com/marqdo/marqdo/plugins/web/internal/invoke" + "github.com/marqdo/marqdo/plugins/web/internal/proxy" "github.com/marqdo/marqdo/plugins/web/internal/table" ) @@ -76,9 +78,30 @@ func Configure(appBag map[string]any, args map[string]any) (map[string]any, erro mw["json_routes"] = JSONRoutesFromTable(v) } out["middleware"] = mw + + if v, ok := first(args, "proxy", "proxy_routes", "代理"); ok && v != nil { + mergeRouteMap(out, "proxy_routes", proxy.RoutesFromTable(v)) + } + if v, ok := first(args, "invoke", "invoke_routes", "调用"); ok && v != nil { + mergeRouteMap(out, "invoke_routes", invoke.RoutesFromTable(v)) + } return out, nil } +func mergeRouteMap(appBag map[string]any, key string, incoming map[string]any) { + if len(incoming) == 0 { + return + } + existing := map[string]any{} + if r, ok := appBag[key].(map[string]any); ok { + existing = clone(r) + } + for k, v := range incoming { + existing[k] = v + } + appBag[key] = existing +} + // Parse reads app["middleware"] into Config (Rust middleware::parse). func Parse(app map[string]any) Config { var cfg Config diff --git a/plugins/web/internal/proxy/proxy.go b/plugins/web/internal/proxy/proxy.go new file mode 100644 index 0000000..04b8413 --- /dev/null +++ b/plugins/web/internal/proxy/proxy.go @@ -0,0 +1,283 @@ +// Package proxy ports offline proxy route bags (Rust proxy.rs). +package proxy + +import ( + "fmt" + "strconv" + "strings" + + "github.com/marqdo/marqdo/plugins/web/internal/app" + "github.com/marqdo/marqdo/plugins/web/internal/table" +) + +// Options is one proxy mount (web_app_proxy args). +type Options struct { + Path string + Upstream string + Stream bool + StripPrefix string + Methods []string + HeadersFromEnv string + TimeoutMs uint64 +} + +// RoutesFromTable normalizes `|路径|上游|流式|去前缀|方法|环境头|超时|` into proxy_routes map keys. +func RoutesFromTable(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 := normalizeTablePath(col(m, "路径", "path", "Path")) + if path == "" || path == "/" { + continue + } + upstream := col(m, "上游", "upstream", "Upstream") + if upstream == "" { + continue + } + stream := true + if s := col(m, "流式", "stream", "Stream"); s != "" { + stream = boolish(s) + } + stripPrefix := col(m, "去前缀", "strip_prefix", "Strip-Prefix") + methodsRaw := col(m, "方法", "methods", "Methods") + methods := parseMethods(methodsRaw) + headersFromEnv := col(m, "环境头", "headers_from_env", "Headers-From-Env") + timeoutMs := uint64(120_000) + if s := col(m, "超时", "timeout_ms", "Timeout"); s != "" { + if n, err := strconv.ParseUint(strings.TrimSpace(s), 10, 64); err == nil { + timeoutMs = n + } + } + key := strings.TrimPrefix(path, "/") + out[key] = routeEntry(upstream, stream, stripPrefix, methods, headersFromEnv, timeoutMs) + } + return out +} + +// Register adds one proxy route to app.proxy_routes (Rust web_app_proxy). +func Register(appBag map[string]any, opts Options) (map[string]any, error) { + if strings.TrimSpace(opts.Upstream) == "" { + return nil, fmt.Errorf("proxy requires `upstream`") + } + path, err := app.NormalizeRoutePath(opts.Path, appBag) + if err != nil { + return nil, err + } + key := strings.TrimPrefix(path, "/") + out := clone(appBag) + routes := map[string]any{} + if r, ok := out["proxy_routes"].(map[string]any); ok { + routes = clone(r) + } + methods := opts.Methods + if len(methods) == 0 { + methods = []string{"POST"} + } + timeout := opts.TimeoutMs + if timeout == 0 { + timeout = 120_000 + } + routes[key] = routeEntry(opts.Upstream, opts.Stream, opts.StripPrefix, methods, opts.HeadersFromEnv, timeout) + out["proxy_routes"] = routes + return out, nil +} + +// OptionsFromArgs parses web_app_proxy ABI args. +func OptionsFromArgs(args map[string]any) (Options, error) { + path, err := argStrReq(args, "path", "路径") + if err != nil { + return Options{}, err + } + upstream, err := argStrReq(args, "upstream", "上游") + if err != nil { + return Options{}, err + } + stream := true + if v, ok := first(args, "stream", "流式"); ok { + stream = boolish(v) + } + stripPrefix, _ := argStr(args, "strip_prefix", "去前缀") + headersFromEnv, _ := argStr(args, "headers_from_env", "环境头") + timeoutMs := uint64(120_000) + if v, ok := first(args, "timeout_ms", "超时"); ok { + if n, ok := asUint64(v); ok { + timeoutMs = n + } + } + return Options{ + Path: path, + Upstream: upstream, + Stream: stream, + StripPrefix: stripPrefix, + Methods: parseMethodsArg(args["methods"], args["方法"]), + HeadersFromEnv: headersFromEnv, + TimeoutMs: timeoutMs, + }, nil +} + +func routeEntry(upstream string, stream bool, stripPrefix string, methods []string, headersFromEnv string, timeoutMs uint64) map[string]any { + methodAny := make([]any, len(methods)) + for i, m := range methods { + methodAny[i] = m + } + return map[string]any{ + "upstream": upstream, + "stream": stream, + "strip_prefix": stripPrefix, + "methods": methodAny, + "headers_from_env": headersFromEnv, + "timeout_ms": float64(timeoutMs), + } +} + +func normalizeTablePath(raw string) string { + s := strings.TrimSpace(raw) + if s == "" { + return "" + } + if !strings.HasPrefix(s, "/") { + return "/" + s + } + return s +} + +func parseMethods(raw string) []string { + if strings.TrimSpace(raw) == "" { + return []string{"POST"} + } + var out []string + for _, part := range strings.Split(raw, ",") { + part = strings.TrimSpace(part) + if part != "" { + out = append(out, strings.ToUpper(part)) + } + } + if len(out) == 0 { + return []string{"POST"} + } + return out +} + +func parseMethodsArg(v any, alt any) []string { + if v == nil { + v = alt + } + if v == nil { + return nil + } + switch t := v.(type) { + case []any: + var out []string + for _, x := range t { + if s, ok := x.(string); ok && strings.TrimSpace(s) != "" { + out = append(out, strings.ToUpper(strings.TrimSpace(s))) + } + } + return out + case string: + return parseMethods(t) + } + return nil +} + +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) + 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 + } + 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 + } + return 0, false +} + +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 argStr(args map[string]any, keys ...string) (string, bool) { + v, ok := first(args, keys...) + if !ok { + return "", false + } + s, _ := v.(string) + return s, true +} + +func argStrReq(args map[string]any, keys ...string) (string, error) { + s, ok := argStr(args, keys...) + if !ok || strings.TrimSpace(s) == "" { + return "", fmt.Errorf("missing `%s`", keys[0]) + } + return s, nil +} + +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 +} diff --git a/plugins/web/internal/proxy/proxy_test.go b/plugins/web/internal/proxy/proxy_test.go new file mode 100644 index 0000000..9086d24 --- /dev/null +++ b/plugins/web/internal/proxy/proxy_test.go @@ -0,0 +1,93 @@ +package proxy_test + +import ( + "testing" + + "github.com/marqdo/marqdo/plugins/web/internal/app" + "github.com/marqdo/marqdo/plugins/web/internal/middleware" + "github.com/marqdo/marqdo/plugins/web/internal/proxy" +) + +func TestRoutesFromTable(t *testing.T) { + tbl := map[string]any{ + "path": []any{"/proxy/chat"}, + "upstream": []any{"$OPENAI_BASE_URL"}, + "stream": []any{true}, + "strip_prefix": []any{"/proxy"}, + "methods": []any{"POST"}, + "headers_from_env": []any{"OPENAI_API_KEY=Authorization"}, + "timeout_ms": []any{60000}, + } + routes := proxy.RoutesFromTable(tbl) + chat, ok := routes["proxy/chat"].(map[string]any) + if !ok { + t.Fatalf("routes=%v", routes) + } + if chat["upstream"] != "$OPENAI_BASE_URL" { + t.Fatalf("upstream=%v", chat["upstream"]) + } + if chat["stream"] != true { + t.Fatalf("stream=%v", chat["stream"]) + } + if chat["strip_prefix"] != "/proxy" { + t.Fatalf("strip_prefix=%v", chat["strip_prefix"]) + } + if chat["headers_from_env"] != "OPENAI_API_KEY=Authorization" { + t.Fatalf("headers_from_env=%v", chat["headers_from_env"]) + } + methods, _ := chat["methods"].([]any) + if len(methods) != 1 || methods[0] != "POST" { + t.Fatalf("methods=%v", methods) + } + if chat["timeout_ms"] != float64(60000) { + t.Fatalf("timeout_ms=%v", chat["timeout_ms"]) + } +} + +func TestRegisterProxyRoute(t *testing.T) { + a := app.New(map[string]any{"page": map[string]any{"title": "p"}}) + out, err := proxy.Register(a, proxy.Options{ + Path: "/v1/stream", + Upstream: "https://example.com/v1/chat", + Stream: true, + HeadersFromEnv: "TOKEN=X-Token", + }) + if err != nil { + t.Fatal(err) + } + routes := out["proxy_routes"].(map[string]any) + entry, ok := routes["v1/stream"].(map[string]any) + if !ok { + t.Fatalf("routes=%v", routes) + } + if entry["upstream"] != "https://example.com/v1/chat" { + t.Fatalf("upstream=%v", entry["upstream"]) + } + if entry["headers_from_env"] != "TOKEN=X-Token" { + t.Fatalf("headers=%v", entry["headers_from_env"]) + } +} + +func TestConfigureMergesProxyTable(t *testing.T) { + a := app.New(nil) + proxyTbl := map[string]any{ + "path": []any{"/proxy/chat"}, + "upstream": []any{"$OPENAI_BASE_URL"}, + "stream": []any{true}, + } + out, err := middleware.Configure(a, map[string]any{"proxy": proxyTbl}) + if err != nil { + t.Fatal(err) + } + routes := out["proxy_routes"].(map[string]any) + if routes["proxy/chat"] == nil { + t.Fatalf("proxy_routes=%v", routes) + } +} + +func TestRegisterRejectsEmptyUpstream(t *testing.T) { + _, err := proxy.Register(app.New(nil), proxy.Options{Path: "/x", Upstream: " "}) + if err == nil { + t.Fatal("want error for empty upstream") + } +} diff --git a/plugins/web/plugin.go b/plugins/web/plugin.go index ad13f8e..aedff2c 100644 --- a/plugins/web/plugin.go +++ b/plugins/web/plugin.go @@ -130,6 +130,8 @@ extern int web_cache_set(char *args_json, char **out_json, char **err_msg); extern int web_cache_del(char *args_json, char **out_json, char **err_msg); extern int web_cache_exists(char *args_json, char **out_json, char **err_msg); extern int web_cache_ttl(char *args_json, char **out_json, char **err_msg); +extern int web_app_proxy(char *args_json, char **out_json, char **err_msg); +extern int web_app_invoke(char *args_json, char **out_json, char **err_msg); static int register_core(void) { if (host_register((char *)"web_go_ready", (char *)"", web_go_ready) != 0) return 1; @@ -213,6 +215,8 @@ static int register_core(void) { if (host_register((char *)"web_cache_del", (char *)"url,key", web_cache_del) != 0) return 1; if (host_register((char *)"web_cache_exists", (char *)"url,key", web_cache_exists) != 0) return 1; if (host_register((char *)"web_cache_ttl", (char *)"url,key", web_cache_ttl) != 0) return 1; + if (host_register((char *)"web_app_proxy", (char *)"app,path,upstream,stream,strip_prefix,methods,headers_from_env,timeout_ms", web_app_proxy) != 0) return 1; + if (host_register((char *)"web_app_invoke", (char *)"app,path,method,fn,body,return", web_app_invoke) != 0) return 1; return 0; } */