diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..5a7ab98 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + + "gcy_hpc_server/internal/app" + "gcy_hpc_server/internal/config" + "gcy_hpc_server/internal/logger" + + "go.uber.org/zap" +) + +func main() { + cfgPath := "" + if len(os.Args) > 1 { + cfgPath = os.Args[1] + } + cfg, err := config.Load(cfgPath) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err) + os.Exit(1) + } + + zapLogger, err := logger.NewLogger(cfg.Log) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to init logger: %v\n", err) + os.Exit(1) + } + defer zapLogger.Sync() + + application, err := app.NewApp(cfg, zapLogger) + if err != nil { + zapLogger.Fatal("failed to initialize application", zap.Error(err)) + } + defer application.Close() + + if err := application.Run(); err != nil { + zapLogger.Fatal("application error", zap.Error(err)) + } +} diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go new file mode 100644 index 0000000..cf14690 --- /dev/null +++ b/cmd/server/main_test.go @@ -0,0 +1,117 @@ +package main + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "gcy_hpc_server/internal/handler" + "gcy_hpc_server/internal/model" + "gcy_hpc_server/internal/server" + "gcy_hpc_server/internal/service" + "gcy_hpc_server/internal/slurm" + "gcy_hpc_server/internal/store" + + "go.uber.org/zap" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func newTestDB() *gorm.DB { + db, _ := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) + db.AutoMigrate(&model.JobTemplate{}) + return db +} + +func TestRouterRegistration(t *testing.T) { + slurmSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(map[string]interface{}{"jobs": []interface{}{}}) + })) + defer slurmSrv.Close() + + client, _ := slurm.NewClientWithOpts(slurmSrv.URL, slurm.WithHTTPClient(slurmSrv.Client())) + templateStore := store.NewTemplateStore(newTestDB()) + + router := server.NewRouter( + handler.NewJobHandler(service.NewJobService(client, zap.NewNop()), zap.NewNop()), + handler.NewClusterHandler(service.NewClusterService(client, zap.NewNop()), zap.NewNop()), + handler.NewTemplateHandler(templateStore, zap.NewNop()), + nil, + ) + + routes := router.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 _, r := range routes { + routeMap[r.Method+" "+r.Path] = 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 TestSmokeGetJobsEndpoint(t *testing.T) { + slurmSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{"jobs": []interface{}{}}) + })) + defer slurmSrv.Close() + + client, _ := slurm.NewClientWithOpts(slurmSrv.URL, slurm.WithHTTPClient(slurmSrv.Client())) + templateStore := store.NewTemplateStore(newTestDB()) + + router := server.NewRouter( + handler.NewJobHandler(service.NewJobService(client, zap.NewNop()), zap.NewNop()), + handler.NewClusterHandler(service.NewClusterService(client, zap.NewNop()), zap.NewNop()), + handler.NewTemplateHandler(templateStore, zap.NewNop()), + nil, + ) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/api/v1/jobs", nil) + router.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String()) + } + + var resp map[string]interface{} + if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { + t.Fatalf("failed to parse response: %v", err) + } + if success, ok := resp["success"].(bool); !ok || !success { + t.Fatalf("expected success=true, got %v", resp["success"]) + } +} diff --git a/go.mod b/go.mod index 57dee1c..5a4cd2e 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,53 @@ -module slurm-client +module gcy_hpc_server -go 1.22 +go 1.25.0 + +require ( + github.com/gin-gonic/gin v1.12.0 + go.uber.org/zap v1.27.1 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 + gopkg.in/yaml.v3 v3.0.1 + gorm.io/driver/mysql v1.6.0 + gorm.io/driver/sqlite v1.6.0 + gorm.io/gorm v1.31.1 +) + +require ( + filippo.io/edwards25519 v1.1.0 // indirect + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/go-sql-driver/mysql v1.9.3 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + go.uber.org/multierr v1.10.0 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/crypto v0.48.0 // indirect + golang.org/x/net v0.51.0 // indirect + golang.org/x/sys v0.41.0 // indirect + golang.org/x/text v0.34.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d951e3e --- /dev/null +++ b/go.sum @@ -0,0 +1,123 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= +github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= +go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc= +go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= +golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= +golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= +golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= +golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg= +gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo= +gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= +gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= +gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg= +gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 0000000..fca0b69 --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,159 @@ +package app + +import ( + "context" + "errors" + "fmt" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "gcy_hpc_server/internal/config" + "gcy_hpc_server/internal/handler" + "gcy_hpc_server/internal/server" + "gcy_hpc_server/internal/service" + "gcy_hpc_server/internal/slurm" + "gcy_hpc_server/internal/store" + + "go.uber.org/zap" + "gorm.io/gorm" +) + +// App encapsulates the entire application lifecycle. +type App struct { + cfg *config.Config + logger *zap.Logger + db *gorm.DB + server *http.Server +} + +// NewApp initializes all application dependencies: DB, Slurm client, services, handlers, router. +func NewApp(cfg *config.Config, logger *zap.Logger) (*App, error) { + gormDB, err := initDB(cfg, logger) + if err != nil { + return nil, err + } + + slurmClient, err := initSlurmClient(cfg) + if err != nil { + closeDB(gormDB) + return nil, err + } + + srv := initHTTPServer(cfg, gormDB, slurmClient, logger) + + return &App{ + cfg: cfg, + logger: logger, + db: gormDB, + server: srv, + }, nil +} + +// Run starts the HTTP server and blocks until a shutdown signal or server error. +func (a *App) Run() error { + errCh := make(chan error, 1) + go func() { + a.logger.Info("starting server", zap.String("addr", a.server.Addr)) + if err := a.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + errCh <- fmt.Errorf("server listen: %w", err) + } + }() + + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + + select { + case err := <-errCh: + // Server crashed before receiving a signal — clean up resources before + // returning, because the caller may call os.Exit and skip deferred Close(). + a.logger.Error("server exited unexpectedly", zap.Error(err)) + _ = a.Close() + return err + case sig := <-quit: + a.logger.Info("received shutdown signal", zap.String("signal", sig.String())) + } + + a.logger.Info("shutting down server...") + return a.Close() +} + +// Close cleans up all resources: HTTP server and database connections. +func (a *App) Close() error { + var errs []error + + if a.server != nil { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := a.server.Shutdown(ctx); err != nil && err != http.ErrServerClosed { + errs = append(errs, fmt.Errorf("shutdown http server: %w", err)) + } + } + + if a.db != nil { + sqlDB, err := a.db.DB() + if err != nil { + errs = append(errs, fmt.Errorf("get underlying sql.DB: %w", err)) + } else if err := sqlDB.Close(); err != nil { + errs = append(errs, fmt.Errorf("close database: %w", err)) + } + } + + return errors.Join(errs...) +} + +// --------------------------------------------------------------------------- +// Initialization helpers +// --------------------------------------------------------------------------- + +func initDB(cfg *config.Config, logger *zap.Logger) (*gorm.DB, error) { + gormDB, err := store.NewGormDB(cfg.MySQLDSN, logger, cfg.Log.GormLevel) + if err != nil { + return nil, fmt.Errorf("init database: %w", err) + } + + if err := store.AutoMigrate(gormDB); err != nil { + closeDB(gormDB) + return nil, fmt.Errorf("run migrations: %w", err) + } + + return gormDB, nil +} + +func closeDB(db *gorm.DB) { + if db == nil { + return + } + if sqlDB, err := db.DB(); err == nil { + _ = sqlDB.Close() + } +} + +func initSlurmClient(cfg *config.Config) (*slurm.Client, error) { + client, err := service.NewSlurmClient(cfg.SlurmAPIURL, cfg.SlurmUserName, cfg.SlurmJWTKeyPath) + if err != nil { + return nil, fmt.Errorf("init slurm client: %w", err) + } + return client, nil +} + +func initHTTPServer(cfg *config.Config, db *gorm.DB, slurmClient *slurm.Client, logger *zap.Logger) *http.Server { + jobSvc := service.NewJobService(slurmClient, logger) + clusterSvc := service.NewClusterService(slurmClient, logger) + templateStore := store.NewTemplateStore(db) + + jobH := handler.NewJobHandler(jobSvc, logger) + clusterH := handler.NewClusterHandler(clusterSvc, logger) + templateH := handler.NewTemplateHandler(templateStore, logger) + + router := server.NewRouter(jobH, clusterH, templateH, logger) + + addr := ":" + cfg.ServerPort + + return &http.Server{ + Addr: addr, + Handler: router, + } +} diff --git a/internal/app/app_test.go b/internal/app/app_test.go new file mode 100644 index 0000000..052b9a6 --- /dev/null +++ b/internal/app/app_test.go @@ -0,0 +1,25 @@ +package app + +import ( + "testing" + + "gcy_hpc_server/internal/config" + + "go.uber.org/zap/zaptest" +) + +func TestNewApp_InvalidDB(t *testing.T) { + cfg := &config.Config{ + ServerPort: "8080", + MySQLDSN: "invalid:dsn@tcp(localhost:99999)/nonexistent?parseTime=true", + SlurmAPIURL: "http://localhost:6820", + SlurmUserName: "root", + SlurmJWTKeyPath: "/nonexistent/jwt.key", + } + logger := zaptest.NewLogger(t) + + _, err := NewApp(cfg, logger) + if err == nil { + t.Fatal("expected error for invalid DSN, got nil") + } +} diff --git a/internal/middleware/logger.go b/internal/middleware/logger.go new file mode 100644 index 0000000..ae0df16 --- /dev/null +++ b/internal/middleware/logger.go @@ -0,0 +1,25 @@ +package middleware + +import ( + "time" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +// RequestLogger returns a Gin middleware that logs each request using zap. +func RequestLogger(logger *zap.Logger) gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + + c.Next() + + logger.Info("request", + zap.String("method", c.Request.Method), + zap.String("path", c.Request.URL.Path), + zap.Int("status", c.Writer.Status()), + zap.Duration("latency", time.Since(start)), + zap.String("client_ip", c.ClientIP()), + ) + } +} diff --git a/internal/server/response.go b/internal/server/response.go new file mode 100644 index 0000000..e67eab5 --- /dev/null +++ b/internal/server/response.go @@ -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}) +} diff --git a/internal/server/response_test.go b/internal/server/response_test.go new file mode 100644 index 0000000..dd67fd3 --- /dev/null +++ b/internal/server/response_test.go @@ -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) + } +} diff --git a/internal/server/server.go b/internal/server/server.go new file mode 100644 index 0000000..f6fac7f --- /dev/null +++ b/internal/server/server.go @@ -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", + }) +} diff --git a/internal/server/server_test.go b/internal/server/server_test.go new file mode 100644 index 0000000..8bd8937 --- /dev/null +++ b/internal/server/server_test.go @@ -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") + } +}