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:
dailz
2026-04-10 08:40:16 +08:00
parent e6162063ca
commit 1784331969
11 changed files with 921 additions and 2 deletions

View File

@@ -0,0 +1,44 @@
package server
import (
"net/http"
"github.com/gin-gonic/gin"
)
// APIResponse represents the unified JSON structure for all API responses.
type APIResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
// OK responds with 200 and success data.
func OK(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, APIResponse{Success: true, Data: data})
}
// Created responds with 201 and success data.
func Created(c *gin.Context, data interface{}) {
c.JSON(http.StatusCreated, APIResponse{Success: true, Data: data})
}
// BadRequest responds with 400 and an error message.
func BadRequest(c *gin.Context, msg string) {
c.JSON(http.StatusBadRequest, APIResponse{Success: false, Error: msg})
}
// NotFound responds with 404 and an error message.
func NotFound(c *gin.Context, msg string) {
c.JSON(http.StatusNotFound, APIResponse{Success: false, Error: msg})
}
// InternalError responds with 500 and an error message.
func InternalError(c *gin.Context, msg string) {
c.JSON(http.StatusInternalServerError, APIResponse{Success: false, Error: msg})
}
// ErrorWithStatus responds with a custom status code and an error message.
func ErrorWithStatus(c *gin.Context, code int, msg string) {
c.JSON(code, APIResponse{Success: false, Error: msg})
}

View File

@@ -0,0 +1,116 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func setupTestContext() (*httptest.ResponseRecorder, *gin.Context) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
return w, c
}
func parseResponse(t *testing.T, w *httptest.ResponseRecorder) APIResponse {
t.Helper()
var resp APIResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response body: %v", err)
}
return resp
}
func TestOK(t *testing.T) {
w, c := setupTestContext()
OK(c, map[string]string{"msg": "hello"})
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
resp := parseResponse(t, w)
if !resp.Success {
t.Fatal("expected success=true")
}
}
func TestCreated(t *testing.T) {
w, c := setupTestContext()
Created(c, map[string]int{"id": 1})
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d", w.Code)
}
resp := parseResponse(t, w)
if !resp.Success {
t.Fatal("expected success=true")
}
}
func TestBadRequest(t *testing.T) {
w, c := setupTestContext()
BadRequest(c, "invalid input")
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
resp := parseResponse(t, w)
if resp.Success {
t.Fatal("expected success=false")
}
if resp.Error != "invalid input" {
t.Fatalf("expected error 'invalid input', got '%s'", resp.Error)
}
}
func TestNotFound(t *testing.T) {
w, c := setupTestContext()
NotFound(c, "resource missing")
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", w.Code)
}
resp := parseResponse(t, w)
if resp.Success {
t.Fatal("expected success=false")
}
if resp.Error != "resource missing" {
t.Fatalf("expected error 'resource missing', got '%s'", resp.Error)
}
}
func TestInternalError(t *testing.T) {
w, c := setupTestContext()
InternalError(c, "something broke")
if w.Code != http.StatusInternalServerError {
t.Fatalf("expected 500, got %d", w.Code)
}
resp := parseResponse(t, w)
if resp.Success {
t.Fatal("expected success=false")
}
if resp.Error != "something broke" {
t.Fatalf("expected error 'something broke', got '%s'", resp.Error)
}
}
func TestErrorWithStatus(t *testing.T) {
w, c := setupTestContext()
ErrorWithStatus(c, http.StatusConflict, "already exists")
if w.Code != http.StatusConflict {
t.Fatalf("expected 409, got %d", w.Code)
}
resp := parseResponse(t, w)
if resp.Success {
t.Fatal("expected success=false")
}
if resp.Error != "already exists" {
t.Fatalf("expected error 'already exists', got '%s'", resp.Error)
}
}

111
internal/server/server.go Normal file
View 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",
})
}

View File

@@ -0,0 +1,108 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestAllRoutesRegistered(t *testing.T) {
r := NewTestRouter()
routes := r.Routes()
expected := []struct {
method string
path string
}{
{"POST", "/api/v1/jobs/submit"},
{"GET", "/api/v1/jobs"},
{"GET", "/api/v1/jobs/history"},
{"GET", "/api/v1/jobs/:id"},
{"DELETE", "/api/v1/jobs/:id"},
{"GET", "/api/v1/nodes"},
{"GET", "/api/v1/nodes/:name"},
{"GET", "/api/v1/partitions"},
{"GET", "/api/v1/partitions/:name"},
{"GET", "/api/v1/diag"},
{"GET", "/api/v1/templates"},
{"POST", "/api/v1/templates"},
{"GET", "/api/v1/templates/:id"},
{"PUT", "/api/v1/templates/:id"},
{"DELETE", "/api/v1/templates/:id"},
}
routeMap := map[string]bool{}
for _, route := range routes {
key := route.Method + " " + route.Path
routeMap[key] = true
}
for _, exp := range expected {
key := exp.method + " " + exp.path
if !routeMap[key] {
t.Errorf("missing route: %s", key)
}
}
if len(routes) < len(expected) {
t.Errorf("expected at least %d routes, got %d", len(expected), len(routes))
}
}
func TestUnregisteredPathReturns404(t *testing.T) {
r := NewTestRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/api/v1/nonexistent", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404 for unregistered path, got %d", w.Code)
}
}
func TestRegisteredPathReturns501(t *testing.T) {
r := NewTestRouter()
endpoints := []struct {
method string
path string
}{
{"GET", "/api/v1/jobs"},
{"GET", "/api/v1/nodes"},
{"GET", "/api/v1/partitions"},
{"GET", "/api/v1/diag"},
{"GET", "/api/v1/templates"},
}
for _, ep := range endpoints {
w := httptest.NewRecorder()
req, _ := http.NewRequest(ep.method, ep.path, nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusNotImplemented {
t.Fatalf("%s %s: expected 501, got %d", ep.method, ep.path, w.Code)
}
var resp APIResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if resp.Success {
t.Fatal("expected success=false")
}
if resp.Error != "not implemented" {
t.Fatalf("expected error 'not implemented', got '%s'", resp.Error)
}
}
}
func TestRouterUsesGinMode(t *testing.T) {
gin.SetMode(gin.TestMode)
r := NewTestRouter()
if r == nil {
t.Fatal("NewRouter returned nil")
}
}