From 48b1fa18f68181aa9f0aae2398fe76838f27ac7a Mon Sep 17 00:00:00 2001 From: director-agent Date: Wed, 8 Apr 2026 10:59:37 -0500 Subject: [PATCH] plan/11: Add GET /api/version endpoint Adds handlers/version.go returning hardcoded {"version":"0.1.0","name":"director","commit":"unknown"}, wires GET /api/version into main.go next to /api/health, and adds handlers/version_test.go using httptest (no DB dependency). Co-Authored-By: director-agent --- api/handlers/version.go | 17 +++++++++++++++++ api/handlers/version_test.go | 35 +++++++++++++++++++++++++++++++++++ api/main.go | 3 +++ 3 files changed, 55 insertions(+) create mode 100644 api/handlers/version.go create mode 100644 api/handlers/version_test.go diff --git a/api/handlers/version.go b/api/handlers/version.go new file mode 100644 index 0000000..baa5e47 --- /dev/null +++ b/api/handlers/version.go @@ -0,0 +1,17 @@ +package handlers + +import "github.com/gin-gonic/gin" + +type VersionResponse struct { + Version string `json:"version"` + Name string `json:"name"` + Commit string `json:"commit"` +} + +func (h *Handler) GetVersion(c *gin.Context) { + c.JSON(200, VersionResponse{ + Version: "0.1.0", + Name: "director", + Commit: "unknown", + }) +} diff --git a/api/handlers/version_test.go b/api/handlers/version_test.go new file mode 100644 index 0000000..1405556 --- /dev/null +++ b/api/handlers/version_test.go @@ -0,0 +1,35 @@ +package handlers + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" +) + +func TestGetVersion(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + c.Request = httptest.NewRequest("GET", "/api/version", nil) + + h := &Handler{db: nil, ps: nil} + h.GetVersion(c) + + if w.Code != http.StatusOK { + t.Errorf("expected 200, got %d", w.Code) + } + + var body map[string]any + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { + t.Fatalf("invalid json: %v", err) + } + + for _, field := range []string{"version", "name", "commit"} { + if _, ok := body[field]; !ok { + t.Errorf("missing field: %s", field) + } + } +} diff --git a/api/main.go b/api/main.go index a863240..63620ad 100644 --- a/api/main.go +++ b/api/main.go @@ -98,6 +98,9 @@ func main() { // Stats api.GET("/stats", h.GetStats) + + // Version + api.GET("/version", h.GetVersion) } // WebSocket