feat: 实现数据库连接和健康检查端点
This commit is contained in:
+27
-1
@@ -1,10 +1,36 @@
|
||||
package main
|
||||
|
||||
import "synoth.com/janus/internal/config"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"synoth.com/janus/internal/config"
|
||||
"synoth.com/janus/internal/database"
|
||||
"synoth.com/janus/internal/handler"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load("config.yaml")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
db, err := database.Connect(database.DatabaseConfig{
|
||||
Host: cfg.Database.Host,
|
||||
Port: cfg.Database.Port,
|
||||
Username: cfg.Database.Username,
|
||||
Password: cfg.Database.Password,
|
||||
DBName: cfg.Database.DBName,
|
||||
SSLMode: cfg.Database.SSLMode,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
router := gin.Default()
|
||||
|
||||
healthHandler := &handler.HealthHandler{Database: db}
|
||||
router.GET("/api/health", healthHandler.Check)
|
||||
|
||||
router.Run(fmt.Sprintf(":%d", cfg.Server.Port))
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ type ServerConfig struct {
|
||||
type DatabaseConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
User string `mapstructure:"user"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
DBName string `mapstructure:"dbname"`
|
||||
SSLMode string `mapstructure:"sslmode"`
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type HealthHandler struct {
|
||||
Database *gorm.DB
|
||||
}
|
||||
|
||||
// Check 健康检查服务
|
||||
func (handler *HealthHandler) Check(context *gin.Context) {
|
||||
db, err := handler.Database.DB()
|
||||
if err != nil {
|
||||
context.JSON(500, gin.H{"error": "database connect error"})
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
context.JSON(500, gin.H{"error": "database ping error"})
|
||||
return
|
||||
}
|
||||
context.JSON(200, gin.H{"status": "healthy"})
|
||||
}
|
||||
Reference in New Issue
Block a user