From 981d75413315bd807704c5aae160a2ce25fcd12c Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 18 Sep 2026 14:59:50 +0800 Subject: [PATCH] feat(web): allow Redis session URL separate from app DB Wire session_url through app.auth/listen so sites can store sessions in Redis while keeping Postgres/SQLite for application data. Co-authored-by: Cursor --- ext/web/web.mq.md | 5 +++-- ext/web/网页.mq.md | 5 +++-- plugins/web/internal/app/app.go | 4 ++++ plugins/web/internal/httpx/listen.go | 13 +++++++++++++ plugins/web/plugin.go | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ext/web/web.mq.md b/ext/web/web.mq.md index fdc8f3e..7640b64 100644 --- a/ext/web/web.mq.md +++ b/ext/web/web.mq.md @@ -815,10 +815,11 @@ Register a WebSocket endpoint at `path` (e.g. `/live` or `/chat/{id}`). `mode` i + `register`=False + `register_path`=/register + `default_role`="member" + + `session_url`=None -Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings. With `register=True` and `enable_rbac`, exposes `register_path` (writes `web_users` + default role). +Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings. With `register=True` and `enable_rbac`, exposes `register_path` (writes `web_users` + default role). Optional `session_url` (`redis://…` / `rediss://…`) stores sessions in Redis separately from the Postgres/SQLite app DB; omit to keep `_marqdo_sessions` (or in-memory). -*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path` register=`register` register_path=`register_path` default_role=`default_role`* +*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path` register=`register` register_path=`register_path` default_role=`default_role` session_url=`session_url`* ## gate + `path` diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index 8299777..106ce79 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -783,10 +783,11 @@ SSR 顶栏刊名 Logo、主题切换钮与目录抽屉钮。交互写在 `client + `注册`=假 + `注册路径`=/register + `默认角色`="member" + + `会话地址`=None -在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。`注册=真` 且已 `启用权限` 时开放 `注册路径`(写入 `web_users` 并赋默认角色)。 +在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。`注册=真` 且已 `启用权限` 时开放 `注册路径`(写入 `web_users` 并赋默认角色)。`会话地址` 可选:`redis://…` / `rediss://…` 将会话存 Redis(与 Postgres/SQLite 业务库分离);缺省仍落在业务库的 `_marqdo_sessions` 或内存。 -*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径` register=`注册` register_path=`注册路径` default_role=`默认角色`* +*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径` register=`注册` register_path=`注册路径` default_role=`默认角色` session_url=`会话地址`* ## 门禁 + `路径` diff --git a/plugins/web/internal/app/app.go b/plugins/web/internal/app/app.go index 83dce8b..0ee0b8e 100644 --- a/plugins/web/internal/app/app.go +++ b/plugins/web/internal/app/app.go @@ -213,6 +213,10 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any if v := firstStr(opts, "default_role", "默认角色"); v != "" { authBag["default_role"] = v } + if v := firstStr(opts, "session_url", "会话地址", "redis_url", "Redis"); v != "" { + out["session_url"] = v + authBag["session_url"] = v + } out["auth"] = authBag gates := gatesOf(out) if !hasAdminGate(gates, prefix) { diff --git a/plugins/web/internal/httpx/listen.go b/plugins/web/internal/httpx/listen.go index 3a7ab69..17588d1 100644 --- a/plugins/web/internal/httpx/listen.go +++ b/plugins/web/internal/httpx/listen.go @@ -45,6 +45,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) { } session.Configure(session.Config{ DBURL: dbURL, + RedisURL: sessionURLOf(appBag), TTLSec: authCfg.sessionTTL, CookieSecure: authCfg.cookieSecure, }) @@ -846,6 +847,18 @@ func dbURLOf(appBag map[string]any) string { return "" } +func sessionURLOf(appBag map[string]any) string { + if s, ok := appBag["session_url"].(string); ok && s != "" { + return s + } + if auth, ok := appBag["auth"].(map[string]any); ok { + if s, ok := auth["session_url"].(string); ok && s != "" { + return s + } + } + return "" +} + func resolvePath(p, entryDir string) string { if filepath.IsAbs(p) { return p diff --git a/plugins/web/plugin.go b/plugins/web/plugin.go index 3442357..6470289 100644 --- a/plugins/web/plugin.go +++ b/plugins/web/plugin.go @@ -220,7 +220,7 @@ static int register_core(void) { if (host_register((char *)"web_auth_check", (char *)"session_id", web_auth_check) != 0) return 1; if (host_register((char *)"web_auth_logout", (char *)"session_id", web_auth_logout) != 0) return 1; if (host_register((char *)"web_auth_new", (char *)"users,session_ttl", web_auth_new) != 0) return 1; - if (host_register((char *)"web_app_auth", (char *)"app,users,session_ttl,admin_prefix,login_redirect,logout_redirect,login_path,register,register_path,default_role", web_app_auth) != 0) return 1; + if (host_register((char *)"web_app_auth", (char *)"app,users,session_ttl,admin_prefix,login_redirect,logout_redirect,login_path,register,register_path,default_role,session_url", web_app_auth) != 0) return 1; if (host_register((char *)"web_app_gate", (char *)"app,path,roles,permissions,match,on_deny,exclude", web_app_gate) != 0) return 1; if (host_register((char *)"web_app_rbac", (char *)"app,catalog", web_app_rbac) != 0) return 1; if (host_register((char *)"web_app_tenant", (char *)"app,mode,param,column,default_scope", web_app_tenant) != 0) return 1;