增加mysql配置

This commit is contained in:
2026-06-21 01:54:43 +08:00
parent c82d4482d4
commit 126e1d9234
2 changed files with 11 additions and 12 deletions
+3 -4
View File
@@ -28,11 +28,10 @@ func InitDB(cfg *config.Config) *gorm.DB {
switch cfg.Database.Type {
case "mysql":
dsn := cfg.Database.DSN
if dsn == "" {
dsn = "root:password@tcp(127.0.0.1:3306)/blog_go?charset=utf8mb4&parseTime=True&loc=Local"
if cfg.Database.DSN == "" {
log.Fatalf("Database DSN is required when type is 'mysql'. Please set it in your config file.")
}
dialector = mysql.Open(dsn)
dialector = mysql.Open(cfg.Database.DSN)
default:
dbPath := filepath.Join(cfg.Path, "blog.db")
dialector = sqlite.Open(dbPath)