feat: 添加应用骨架,配置化 zap 日志贯穿全链路
- cmd/server/main.go: 使用 logger.NewLogger(cfg.Log) 替代 zap.NewProduction() - internal/app: 依赖注入组装 DB/Slurm/Service/Handler,传递 logger - internal/middleware: RequestLogger 请求日志中间件 - internal/server: 统一响应格式和路由注册 - go.mod: module 更名为 gcy_hpc_server,添加 gin/zap/lumberjack/gorm 依赖 - 日志初始化失败时 fail fast (os.Exit(1)) - GormLevel 从配置传递到 NewGormDB,支持 YAML 独立配置 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
111
internal/server/server.go
Normal file
111
internal/server/server.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gcy_hpc_server/internal/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type JobHandler interface {
|
||||
SubmitJob(c *gin.Context)
|
||||
GetJobs(c *gin.Context)
|
||||
GetJobHistory(c *gin.Context)
|
||||
GetJob(c *gin.Context)
|
||||
CancelJob(c *gin.Context)
|
||||
}
|
||||
|
||||
type ClusterHandler interface {
|
||||
GetNodes(c *gin.Context)
|
||||
GetNode(c *gin.Context)
|
||||
GetPartitions(c *gin.Context)
|
||||
GetPartition(c *gin.Context)
|
||||
GetDiag(c *gin.Context)
|
||||
}
|
||||
|
||||
type TemplateHandler interface {
|
||||
ListTemplates(c *gin.Context)
|
||||
CreateTemplate(c *gin.Context)
|
||||
GetTemplate(c *gin.Context)
|
||||
UpdateTemplate(c *gin.Context)
|
||||
DeleteTemplate(c *gin.Context)
|
||||
}
|
||||
|
||||
// NewRouter creates a Gin engine with all API v1 routes registered with real handlers.
|
||||
func NewRouter(jobH JobHandler, clusterH ClusterHandler, templateH TemplateHandler, logger *zap.Logger) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
if logger != nil {
|
||||
r.Use(middleware.RequestLogger(logger))
|
||||
}
|
||||
|
||||
v1 := r.Group("/api/v1")
|
||||
|
||||
jobs := v1.Group("/jobs")
|
||||
jobs.POST("/submit", jobH.SubmitJob)
|
||||
jobs.GET("", jobH.GetJobs)
|
||||
jobs.GET("/history", jobH.GetJobHistory)
|
||||
jobs.GET("/:id", jobH.GetJob)
|
||||
jobs.DELETE("/:id", jobH.CancelJob)
|
||||
|
||||
v1.GET("/nodes", clusterH.GetNodes)
|
||||
v1.GET("/nodes/:name", clusterH.GetNode)
|
||||
|
||||
v1.GET("/partitions", clusterH.GetPartitions)
|
||||
v1.GET("/partitions/:name", clusterH.GetPartition)
|
||||
|
||||
v1.GET("/diag", clusterH.GetDiag)
|
||||
|
||||
templates := v1.Group("/templates")
|
||||
templates.GET("", templateH.ListTemplates)
|
||||
templates.POST("", templateH.CreateTemplate)
|
||||
templates.GET("/:id", templateH.GetTemplate)
|
||||
templates.PUT("/:id", templateH.UpdateTemplate)
|
||||
templates.DELETE("/:id", templateH.DeleteTemplate)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// NewTestRouter creates a router for testing without real handlers.
|
||||
func NewTestRouter() *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
v1 := r.Group("/api/v1")
|
||||
registerPlaceholderRoutes(v1)
|
||||
return r
|
||||
}
|
||||
|
||||
func registerPlaceholderRoutes(v1 *gin.RouterGroup) {
|
||||
jobs := v1.Group("/jobs")
|
||||
jobs.POST("/submit", notImplemented)
|
||||
jobs.GET("", notImplemented)
|
||||
jobs.GET("/history", notImplemented)
|
||||
jobs.GET("/:id", notImplemented)
|
||||
jobs.DELETE("/:id", notImplemented)
|
||||
|
||||
v1.GET("/nodes", notImplemented)
|
||||
v1.GET("/nodes/:name", notImplemented)
|
||||
|
||||
v1.GET("/partitions", notImplemented)
|
||||
v1.GET("/partitions/:name", notImplemented)
|
||||
|
||||
v1.GET("/diag", notImplemented)
|
||||
|
||||
templates := v1.Group("/templates")
|
||||
templates.GET("", notImplemented)
|
||||
templates.POST("", notImplemented)
|
||||
templates.GET("/:id", notImplemented)
|
||||
templates.PUT("/:id", notImplemented)
|
||||
templates.DELETE("/:id", notImplemented)
|
||||
}
|
||||
|
||||
func notImplemented(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, APIResponse{
|
||||
Success: false,
|
||||
Error: "not implemented",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user