From d2520c65e8c1d25f117ca1daa5ccc4db9ecc708e Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Thu, 12 Feb 2026 08:59:00 +0100 Subject: [PATCH] feat: add /v1/status endpoint --- .goreleaser.yaml | 2 ++ internal/handlers/status.go | 11 +++++++---- main.go | 22 +++++++--------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 2f4ef0e..06add8c 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -11,6 +11,8 @@ builds: - CGO_ENABLED=0 ldflags: - -s -w + - -X main.Version={{.Version}} + - -X main.Commit={{.ShortCommit}} goos: - linux archives: diff --git a/internal/handlers/status.go b/internal/handlers/status.go index 56d63b6..a796833 100644 --- a/internal/handlers/status.go +++ b/internal/handlers/status.go @@ -4,15 +4,18 @@ import ( "github.com/gofiber/fiber/v2" ) -type Status struct{} +type Status struct { + Version string `json:"v"` + Commit string `json:"commit"` +} -func NewStatus() *Status { - return &Status{} +func NewStatus(version string, commit string) *Status { + return &Status{Version: version, Commit: commit} } // GetStatus gets status of the API. func (s *Status) GetStatus(ctx *fiber.Ctx) error { ctx.Append("Cache-Control", "no-cache") - return ctx.SendStatus(fiber.StatusNoContent) + return ctx.Status(fiber.StatusOK).JSON(s) } diff --git a/main.go b/main.go index ee16e8c..7aa3cd2 100644 --- a/main.go +++ b/main.go @@ -3,18 +3,21 @@ package main import ( "log" "os" - "time" "github.com/ansrivas/fiberprometheus/v2" "github.com/caarlos0/env/v6" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/cache" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/italia/publiccode-validator-api/internal/common" "github.com/italia/publiccode-validator-api/internal/handlers" "github.com/italia/publiccode-validator-api/internal/jsondecoder" ) +var ( + Version = "dev" //nolint:gochecknoglobals // We need this to be set at build + Commit = "" //nolint:gochecknoglobals // We need this to be set at build +) + func main() { app := Setup() if err := app.Listen(":3000"); err != nil { @@ -40,19 +43,6 @@ func Setup() *fiber.App { // Automatically recover panics in handlers app.Use(recover.New()) - app.Use(cache.New(cache.Config{ - Next: func(ctx *fiber.Ctx) bool { - // Don't cache /status - return ctx.Route().Path == "/v1/status" - }, - Methods: []string{fiber.MethodGet, fiber.MethodHead}, - CacheControl: true, - Expiration: 10 * time.Second, //nolint:gomnd - KeyGenerator: func(ctx *fiber.Ctx) string { - return ctx.Path() + string(ctx.Context().QueryArgs().QueryString()) - }, - })) - prometheus := fiberprometheus.New(os.Args[0]) prometheus.RegisterAt(app, "/metrics") app.Use(prometheus.Middleware) @@ -64,8 +54,10 @@ func Setup() *fiber.App { func setupHandlers(app *fiber.App) { validateHandler := handlers.NewPubliccodeymlValidatorHandler() + statusHandler := handlers.NewStatus(Version, Commit) v1 := app.Group("/v1") + v1.Get("/status", statusHandler.GetStatus) v1.Add("QUERY", "/validate", validateHandler.Query) }