feat: 添加数据库连接字符串环境变量支持

This commit is contained in:
tbphp
2025-06-30 16:26:10 +08:00
parent 731315144e
commit 5f0990f22b
2 changed files with 6 additions and 3 deletions

View File

@@ -99,4 +99,5 @@ REQUEST_TIMEOUT=30
RESPONSE_TIMEOUT=30
# 空闲连接超时时间(秒)- 控制连接池中空闲连接的生存时间
IDLE_CONN_TIMEOUT=120
IDLE_CONN_TIMEOUT=120
DATABASE_DSN="user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"

View File

@@ -15,8 +15,10 @@ import (
var DB *gorm.DB
func InitDB() (*gorm.DB, error) {
// TODO: 从配置中心读取DSN
dsn := "root:1236@tcp(127.0.0.1:3306)/gpt_load?charset=utf8mb4&parseTime=True&loc=Local"
dsn := os.Getenv("DATABASE_DSN")
if dsn == "" {
return nil, fmt.Errorf("DATABASE_DSN environment variable not set")
}
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer