feat: add support for custom cookie name via config to avoid multi-instance conflicts

This commit is contained in:
duorameng
2026-06-16 16:26:52 +08:00
parent 48db87ddae
commit b135763280
5 changed files with 18 additions and 4 deletions
+3
View File
@@ -361,6 +361,8 @@ port = 8052
host = 0.0.0.0
# 可选:配置 URL 前缀用于反向代理,例如 /baihu
url_prefix =
# 全局会话 Cookie 名称
cookie_name = BHToken
[database]
type = sqlite
@@ -676,6 +678,7 @@ table_prefix = baihu_
| `BH_SERVER_PORT` | server.port | 服务端口 | 8052 |
| `BH_SERVER_HOST` | server.host | 监听地址 | 0.0.0.0 |
| `BH_SERVER_URL_PREFIX` | server.url_prefix | URL 前缀,用于反向代理子路径部署 | - |
| `BH_COOKIE_NAME` | server.cookie_name | 全局会话 Cookie 名称 | BHToken |
| `BH_DB_TYPE` | database.type | 数据库类型 (sqlite/mysql) | sqlite |
| `BH_DB_HOST` | database.host | 数据库地址 | localhost |
| `BH_DB_PORT` | database.port | 数据库端口 | 3306 |
+2
View File
@@ -6,6 +6,8 @@ host = 0.0.0.0
# URL前缀,例如 /baihu,留空则无前缀
# 配置后:前端路径为 /baihu/*,后端API路径为 /baihu/api/v1/*
url_prefix =
# 全局会话 Cookie 名称
cookie_name = BHToken
[database]
# 数据库类型: sqlite, mysql, postgres
+3
View File
@@ -13,6 +13,7 @@
| `BH_SERVER_PORT` | server.port | 服务监听端口 | 8052 |
| `BH_SERVER_HOST` | server.host | 监听地址 | 0.0.0.0 |
| `BH_SERVER_URL_PREFIX` | server.url_prefix | URL 前缀,用于反向代理子路径部署 | - |
| `BH_COOKIE_NAME` | server.cookie_name | 全局会话 Cookie 名称 | BHToken |
| `BH_DB_TYPE` | database.type | 数据库类型 (sqlite/mysql) | sqlite |
| `BH_DB_HOST` | database.host | 数据库实例地址 | localhost |
| `BH_DB_PORT` | database.port | 数据库端口 | 3306 |
@@ -44,6 +45,8 @@ port = 8052
host = 0.0.0.0
# 配置 URL 前缀用于反向代理,例如 /baihu/
url_prefix = /baihu
# 全局会话 Cookie 名称
cookie_name = BHToken
[database]
type = sqlite
+3 -3
View File
@@ -10,9 +10,6 @@ const (
// AdminRole 管理员角色
AdminRole = "admin"
// CookieName Cookie 名称
CookieName = "BHToken"
// DefaultTaskTimeout 默认任务超时时间(分钟)
DefaultTaskTimeout = 30
@@ -178,6 +175,9 @@ const (
ScriptsDirPlaceholder = "$SCRIPTS_DIR$"
)
// CookieName Cookie 名称
var CookieName = "BHToken"
// TablePrefix 表前缀,从配置文件读取
var TablePrefix string
+7 -1
View File
@@ -16,6 +16,7 @@ type ServerConfig struct {
Host string `ini:"host"`
URLPrefix string `ini:"url_prefix"`
PprofEnabled bool `ini:"pprof_enabled"`
CookieName string `ini:"cookie_name"`
}
type DatabaseConfig struct {
@@ -87,6 +88,7 @@ func LoadConfig(path string) (*AppConfig, error) {
Port: 8052,
Host: "0.0.0.0",
PprofEnabled: false,
CookieName: "BHToken",
},
Database: DatabaseConfig{
Type: "sqlite",
@@ -125,7 +127,10 @@ func LoadConfig(path string) (*AppConfig, error) {
Config.Database.Path = constant.DefaultDBPath
}
// 设置表前缀到 constant 包
// 设置配置到 constant 包
if Config.Server.CookieName != "" {
constant.CookieName = Config.Server.CookieName
}
constant.TablePrefix = Config.Database.TablePrefix
constant.RuntimeDBType = Config.Database.Type
constant.RuntimeDBHost = Config.Database.Host
@@ -169,6 +174,7 @@ func applyEnvOverrides() {
getEnvStr("BH_SERVER_HOST", &Config.Server.Host)
getEnvStr("BH_SERVER_URL_PREFIX", &Config.Server.URLPrefix)
getEnvBool("BH_SERVER_PPROF", &Config.Server.PprofEnabled)
getEnvStr("BH_COOKIE_NAME", &Config.Server.CookieName)
// Database
getEnvStr("BH_DB_TYPE", &Config.Database.Type)