Skip to content

Commit b9cd50f

Browse files
committed
refactor(service_manager): 重构服务管理模块数据库模型和API
- 重构数据库模型,简化部署任务和状态管理结构 - 移除不再使用的部署批次相关代码 - 更新数据库连接配置和SQL查询语句 - 添加CORS中间件支持 - 调整API路由顺序和移除不再使用的端点 - 更新健康状态枚举命名和类型定义 - 添加数据库schema文件定义表结构
1 parent ab28004 commit b9cd50f

File tree

20 files changed

+265
-262
lines changed

20 files changed

+265
-262
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24
44

55
require (
66
github.com/fox-gonic/fox v0.0.6
7+
github.com/lib/pq v1.10.9
78
github.com/rs/zerolog v1.34.0
89
)
910

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
5050
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
5151
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
5252
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
53+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
54+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
5355
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5456
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
5557
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=

internal/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func Load() (*Config, error) {
3939
Database: DatabaseConfig{
4040
Host: getEnv("DB_HOST", "localhost"),
4141
Port: getEnvInt("DB_PORT", 5432),
42-
User: getEnv("DB_USER", "postgres"),
43-
Password: getEnv("DB_PASSWORD", ""),
42+
User: getEnv("DB_USER", "admin"),
43+
Password: getEnv("DB_PASSWORD", "password"),
4444
DBName: getEnv("DB_NAME", "zeroops"),
4545
SSLMode: getEnv("DB_SSLMODE", "disable"),
4646
},

internal/middleware/cors.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/fox-gonic/fox"
7+
)
8+
9+
// CORS 跨域中间件
10+
func CORS() fox.HandlerFunc {
11+
return func(c *fox.Context) {
12+
origin := c.Request.Header.Get("Origin")
13+
14+
// 设置CORS头
15+
c.Header("Access-Control-Allow-Origin", "*")
16+
c.Header("Access-Control-Allow-Credentials", "true")
17+
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
18+
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE, PATCH")
19+
20+
// 处理预检请求
21+
if c.Request.Method == "OPTIONS" {
22+
c.AbortWithStatus(http.StatusNoContent)
23+
return
24+
}
25+
26+
// 设置实际的Origin(如果有的话)
27+
if origin != "" {
28+
c.Header("Access-Control-Allow-Origin", origin)
29+
}
30+
31+
c.Next()
32+
}
33+
}

internal/service_manager/api/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"github.com/fox-gonic/fox"
5+
"github.com/qiniu/zeroops/internal/middleware"
56
"github.com/qiniu/zeroops/internal/service_manager/database"
67
"github.com/qiniu/zeroops/internal/service_manager/service"
78
)
@@ -18,6 +19,10 @@ func NewApi(db *database.Database, service *service.Service, router *fox.Engine)
1819
service: service,
1920
router: router,
2021
}
22+
23+
// 添加CORS中间件
24+
router.Use(middleware.CORS())
25+
2126
api.setupRouters(router)
2227
return api, nil
2328
}

internal/service_manager/api/deploy_api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
func (api *Api) setupDeployRouters(router *fox.Engine) {
1515
// 部署任务基本操作
1616
router.POST("/v1/deployments", api.CreateDeployment)
17+
router.GET("/v1/deployments", api.GetDeployments)
1718
router.GET("/v1/deployments/:deployID", api.GetDeploymentByID)
1819
router.POST("/v1/deployments/:deployID", api.UpdateDeployment)
19-
router.GET("/v1/deployments", api.GetDeployments)
2020
router.DELETE("/v1/deployments/:deployID", api.DeleteDeployment)
2121

2222
// 部署任务控制操作

internal/service_manager/api/info_api.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
func (api *Api) setupInfoRouters(router *fox.Engine) {
1414
// 服务列表和信息查询
1515
router.GET("/v1/services", api.GetServices)
16-
router.GET("/v1/services/:service", api.GetServiceByName)
1716
router.GET("/v1/services/:service/activeVersions", api.GetServiceActiveVersions)
1817
router.GET("/v1/services/:service/availableVersions", api.GetServiceAvailableVersions)
1918
router.GET("/v1/metrics/:service/:name", api.GetServiceMetricTimeSeries)
@@ -185,39 +184,6 @@ func (api *Api) CreateService(c *fox.Context) {
185184
})
186185
}
187186

188-
// GetServiceByName 获取单个服务信息(GET /v1/services/:service)
189-
func (api *Api) GetServiceByName(c *fox.Context) {
190-
ctx := c.Request.Context()
191-
serviceName := c.Param("service")
192-
193-
if serviceName == "" {
194-
c.JSON(http.StatusBadRequest, map[string]any{
195-
"error": "bad request",
196-
"message": "service name is required",
197-
})
198-
return
199-
}
200-
201-
svc, err := api.service.GetServiceByName(ctx, serviceName)
202-
if err != nil {
203-
if err == service.ErrServiceNotFound {
204-
c.JSON(http.StatusNotFound, map[string]any{
205-
"error": "not found",
206-
"message": "service not found",
207-
})
208-
return
209-
}
210-
log.Error().Err(err).Str("service", serviceName).Msg("failed to get service")
211-
c.JSON(http.StatusInternalServerError, map[string]any{
212-
"error": "internal server error",
213-
"message": "failed to get service",
214-
})
215-
return
216-
}
217-
218-
c.JSON(http.StatusOK, svc)
219-
}
220-
221187
// UpdateService 更新服务信息(PUT /v1/services/:service)
222188
func (api *Api) UpdateService(c *fox.Context) {
223189
ctx := c.Request.Context()

internal/service_manager/database/database.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package database
33
import (
44
"context"
55
"database/sql"
6+
"fmt"
67

8+
_ "github.com/lib/pq" // PostgreSQL driver
79
"github.com/qiniu/zeroops/internal/config"
810
)
911

@@ -13,7 +15,27 @@ type Database struct {
1315
}
1416

1517
func NewDatabase(cfg *config.DatabaseConfig) (*Database, error) {
16-
database := &Database{config: cfg}
18+
// 构建PostgreSQL连接字符串
19+
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
20+
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode)
21+
22+
// 连接数据库
23+
db, err := sql.Open("postgres", dsn)
24+
if err != nil {
25+
return nil, fmt.Errorf("failed to open database connection: %w", err)
26+
}
27+
28+
// 测试连接
29+
if err := db.Ping(); err != nil {
30+
db.Close()
31+
return nil, fmt.Errorf("failed to ping database: %w", err)
32+
}
33+
34+
database := &Database{
35+
db: db,
36+
config: cfg,
37+
}
38+
1739
return database, nil
1840
}
1941

0 commit comments

Comments
 (0)