plan/11: Add GET /api/version endpoint #3

Closed
director-agent wants to merge 1 commits from plan/11-api-version into main
3 changed files with 55 additions and 0 deletions

17
api/handlers/version.go Normal file
View File

@@ -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",
})
}

View File

@@ -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)
}
}
}

View File

@@ -98,6 +98,9 @@ func main() {
// Stats // Stats
api.GET("/stats", h.GetStats) api.GET("/stats", h.GetStats)
// Version
api.GET("/version", h.GetVersion)
} }
// WebSocket // WebSocket