feat: support env start

This commit is contained in:
engigu
2025-12-24 08:47:02 +08:00
parent 0f5b7666cc
commit 7193571f1d
4 changed files with 146 additions and 12 deletions
+46 -6
View File
@@ -52,6 +52,14 @@ services:
- ./envs:/app/envs - ./envs:/app/envs
environment: environment:
- TZ=Asia/Shanghai - TZ=Asia/Shanghai
# 以下环境变量可覆盖配置文件(可选)
# - BH_SERVER_PORT=8052
# - BH_DB_TYPE=mysql
# - BH_DB_HOST=localhost
# - BH_DB_PORT=3306
# - BH_DB_USER=root
# - BH_DB_PASSWORD=password
# - BH_DB_NAME=baihu
restart: unless-stopped restart: unless-stopped
``` ```
@@ -135,6 +143,8 @@ docker-compose up -d
## 配置说明 ⚙️ ## 配置说明 ⚙️
### 配置文件
配置文件路径:`configs/config.ini` 配置文件路径:`configs/config.ini`
```ini ```ini
@@ -152,12 +162,42 @@ dbname = ql_panel
table_prefix = baihu_ table_prefix = baihu_
``` ```
| 配置项 | 说明 | 默认值 | ### 环境变量
|--------|------|--------|
| `server.port` | 服务端口 | 8052 | 所有配置项都支持通过环境变量覆盖,环境变量优先级高于配置文件:
| `server.host` | 监听地址 | 0.0.0.0 |
| `database.type` | 数据库类型 | sqlite | | 环境变量 | 对应配置 | 说明 | 默认值 |
| `database.table_prefix` | 表前缀 | baihu_ | |----------|----------|------|--------|
| `BH_SERVER_PORT` | server.port | 服务端口 | 8052 |
| `BH_SERVER_HOST` | server.host | 监听地址 | 0.0.0.0 |
| `BH_DB_TYPE` | database.type | 数据库类型 (sqlite/mysql) | sqlite |
| `BH_DB_HOST` | database.host | 数据库地址 | localhost |
| `BH_DB_PORT` | database.port | 数据库端口 | 3306 |
| `BH_DB_USER` | database.user | 数据库用户 | root |
| `BH_DB_PASSWORD` | database.password | 数据库密码 | - |
| `BH_DB_NAME` | database.dbname | 数据库名称 | ql_panel |
| `BH_DB_PATH` | database.path | SQLite 文件路径 | ./data/ql.db |
| `BH_DB_TABLE_PREFIX` | database.table_prefix | 表前缀 | baihu_ |
| `BH_SECRET` | security.secret | JWT 密钥 | 自动生成 |
**使用 MySQL 示例:**
```bash
docker run -d \
--name baihu \
-p 8052:8052 \
-v $(pwd)/data:/app/data \
-v $(pwd)/envs:/app/envs \
-e TZ=Asia/Shanghai \
-e BH_DB_TYPE=mysql \
-e BH_DB_HOST=mysql-server \
-e BH_DB_PORT=3306 \
-e BH_DB_USER=root \
-e BH_DB_PASSWORD=your_password \
-e BH_DB_NAME=baihu \
--restart unless-stopped \
ghcr.io/engigu/baihu:main
```
### 调度设置 ### 调度设置
+11
View File
@@ -12,4 +12,15 @@ services:
- ./envs:/app/envs - ./envs:/app/envs
environment: environment:
- TZ=Asia/Shanghai - TZ=Asia/Shanghai
# 以下环境变量可覆盖配置文件(可选)
# - BH_SERVER_PORT=8052
# - BH_SERVER_HOST=0.0.0.0
# - BH_DB_TYPE=mysql
# - BH_DB_HOST=localhost
# - BH_DB_PORT=3306
# - BH_DB_USER=root
# - BH_DB_PASSWORD=password
# - BH_DB_NAME=baihu
# - BH_DB_TABLE_PREFIX=baihu_
# - BH_SECRET=your_secret_key
restart: unless-stopped restart: unless-stopped
+11
View File
@@ -11,4 +11,15 @@ services:
- ./envs:/app/envs - ./envs:/app/envs
environment: environment:
- TZ=Asia/Shanghai - TZ=Asia/Shanghai
# 以下环境变量可覆盖配置文件(可选)
# - BH_SERVER_PORT=8052
# - BH_SERVER_HOST=0.0.0.0
# - BH_DB_TYPE=mysql
# - BH_DB_HOST=localhost
# - BH_DB_PORT=3306
# - BH_DB_USER=root
# - BH_DB_PASSWORD=password
# - BH_DB_NAME=baihu
# - BH_DB_TABLE_PREFIX=baihu_
# - BH_SECRET=your_secret_key
restart: unless-stopped restart: unless-stopped
+74 -2
View File
@@ -2,6 +2,9 @@ package services
import ( import (
"baihu/internal/constant" "baihu/internal/constant"
"log"
"os"
"strconv"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
@@ -34,16 +37,60 @@ type AppConfig struct {
var Config *AppConfig var Config *AppConfig
// getEnvStr 获取环境变量字符串
func getEnvStr(key string, target *string) {
if v := os.Getenv(key); v != "" {
*target = v
}
}
// getEnvInt 获取环境变量整数
func getEnvInt(key string, target *int) {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
*target = n
}
}
}
func LoadConfig(path string) (*AppConfig, error) { func LoadConfig(path string) (*AppConfig, error) {
// 初始化默认配置
Config = &AppConfig{
Server: ServerConfig{
Port: 8052,
Host: "0.0.0.0",
},
Database: DatabaseConfig{
Type: "sqlite",
Host: "localhost",
Port: 3306,
User: "root",
Password: "",
DBName: "baihu",
Path: constant.DefaultDBPath,
TablePrefix: "baihu_",
},
Security: SecurityConfig{
Secret: "",
},
}
// 检查配置文件是否存在
if _, err := os.Stat(path); err == nil {
// 配置文件存在,从文件加载
log.Printf("[Config] Loading from file: %s", path)
cfg, err := ini.Load(path) cfg, err := ini.Load(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
Config = &AppConfig{}
if err := cfg.MapTo(Config); err != nil { if err := cfg.MapTo(Config); err != nil {
return nil, err return nil, err
} }
} else {
// 配置文件不存在,使用环境变量
log.Printf("[Config] File not found, loading from environment variables")
applyEnvOverrides()
}
// 设置默认数据库路径 // 设置默认数据库路径
if Config.Database.Path == "" { if Config.Database.Path == "" {
@@ -56,9 +103,34 @@ func LoadConfig(path string) (*AppConfig, error) {
// 设置 Secret 到 constant 包 // 设置 Secret 到 constant 包
constant.Secret = Config.Security.Secret constant.Secret = Config.Security.Secret
// 输出配置信息(隐藏敏感信息)
log.Printf("[Config] Server: %s:%d", Config.Server.Host, Config.Server.Port)
log.Printf("[Config] Database: type=%s, host=%s, port=%d, dbname=%s",
Config.Database.Type, Config.Database.Host, Config.Database.Port, Config.Database.DBName)
return Config, nil return Config, nil
} }
// applyEnvOverrides 从环境变量加载配置
func applyEnvOverrides() {
// Server
getEnvInt("BH_SERVER_PORT", &Config.Server.Port)
getEnvStr("BH_SERVER_HOST", &Config.Server.Host)
// Database
getEnvStr("BH_DB_TYPE", &Config.Database.Type)
getEnvStr("BH_DB_HOST", &Config.Database.Host)
getEnvInt("BH_DB_PORT", &Config.Database.Port)
getEnvStr("BH_DB_USER", &Config.Database.User)
getEnvStr("BH_DB_PASSWORD", &Config.Database.Password)
getEnvStr("BH_DB_NAME", &Config.Database.DBName)
getEnvStr("BH_DB_PATH", &Config.Database.Path)
getEnvStr("BH_DB_TABLE_PREFIX", &Config.Database.TablePrefix)
// Security
getEnvStr("BH_SECRET", &Config.Security.Secret)
}
func GetConfig() *AppConfig { func GetConfig() *AppConfig {
return Config return Config
} }