feat: 实现数据库连接和健康检查端点

This commit is contained in:
yangzhaohan
2026-07-09 17:04:56 +08:00
parent 262c8377b9
commit 8ef79eb5f8
4 changed files with 77 additions and 4 deletions
+23 -2
View File
@@ -1,10 +1,31 @@
package database
import "gorm.io/gorm"
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// DatabaseConfig
type DatabaseConfig struct {
Host string
Port int
Username string
Password string
DBName string
SSLMode string
}
func Connect(cfg DatabaseConfig) (*gorm.DB, error) {
return nil, nil
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
cfg.Host, cfg.Port, cfg.Username, cfg.Password, cfg.DBName, cfg.SSLMode,
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("connect database: %w", err)
}
return db, nil
}