Skip to content

Commit d2520c6

Browse files
committed
feat: add /v1/status endpoint
1 parent 8d46542 commit d2520c6

3 files changed

Lines changed: 16 additions & 19 deletions

File tree

.goreleaser.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ builds:
1111
- CGO_ENABLED=0
1212
ldflags:
1313
- -s -w
14+
- -X main.Version={{.Version}}
15+
- -X main.Commit={{.ShortCommit}}
1416
goos:
1517
- linux
1618
archives:

internal/handlers/status.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import (
44
"github.com/gofiber/fiber/v2"
55
)
66

7-
type Status struct{}
7+
type Status struct {
8+
Version string `json:"v"`
9+
Commit string `json:"commit"`
10+
}
811

9-
func NewStatus() *Status {
10-
return &Status{}
12+
func NewStatus(version string, commit string) *Status {
13+
return &Status{Version: version, Commit: commit}
1114
}
1215

1316
// GetStatus gets status of the API.
1417
func (s *Status) GetStatus(ctx *fiber.Ctx) error {
1518
ctx.Append("Cache-Control", "no-cache")
1619

17-
return ctx.SendStatus(fiber.StatusNoContent)
20+
return ctx.Status(fiber.StatusOK).JSON(s)
1821
}

main.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package main
33
import (
44
"log"
55
"os"
6-
"time"
76

87
"github.com/ansrivas/fiberprometheus/v2"
98
"github.com/caarlos0/env/v6"
109
"github.com/gofiber/fiber/v2"
11-
"github.com/gofiber/fiber/v2/middleware/cache"
1210
"github.com/gofiber/fiber/v2/middleware/recover"
1311
"github.com/italia/publiccode-validator-api/internal/common"
1412
"github.com/italia/publiccode-validator-api/internal/handlers"
1513
"github.com/italia/publiccode-validator-api/internal/jsondecoder"
1614
)
1715

16+
var (
17+
Version = "dev" //nolint:gochecknoglobals // We need this to be set at build
18+
Commit = "<none>" //nolint:gochecknoglobals // We need this to be set at build
19+
)
20+
1821
func main() {
1922
app := Setup()
2023
if err := app.Listen(":3000"); err != nil {
@@ -40,19 +43,6 @@ func Setup() *fiber.App {
4043
// Automatically recover panics in handlers
4144
app.Use(recover.New())
4245

43-
app.Use(cache.New(cache.Config{
44-
Next: func(ctx *fiber.Ctx) bool {
45-
// Don't cache /status
46-
return ctx.Route().Path == "/v1/status"
47-
},
48-
Methods: []string{fiber.MethodGet, fiber.MethodHead},
49-
CacheControl: true,
50-
Expiration: 10 * time.Second, //nolint:gomnd
51-
KeyGenerator: func(ctx *fiber.Ctx) string {
52-
return ctx.Path() + string(ctx.Context().QueryArgs().QueryString())
53-
},
54-
}))
55-
5646
prometheus := fiberprometheus.New(os.Args[0])
5747
prometheus.RegisterAt(app, "/metrics")
5848
app.Use(prometheus.Middleware)
@@ -64,8 +54,10 @@ func Setup() *fiber.App {
6454

6555
func setupHandlers(app *fiber.App) {
6656
validateHandler := handlers.NewPubliccodeymlValidatorHandler()
57+
statusHandler := handlers.NewStatus(Version, Commit)
6758

6859
v1 := app.Group("/v1")
6960

61+
v1.Get("/status", statusHandler.GetStatus)
7062
v1.Add("QUERY", "/validate", validateHandler.Query)
7163
}

0 commit comments

Comments
 (0)