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 <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-18 14:59:50 +08:00
1 parent 410f36f719
commit 981d754133
5 files changed
+24 -5

No files matched your search

+4
View File
@@ -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) {
+13
View File
@@ -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
+1 -1
View File
@@ -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;