Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ builds:
- CGO_ENABLED=0
ldflags:
- -s -w
- -X main.Version={{.Version}}
- -X main.Commit={{.ShortCommit}}
goos:
- linux
archives:
Expand Down
11 changes: 7 additions & 4 deletions internal/handlers/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 7 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<none>" //nolint:gochecknoglobals // We need this to be set at build
)

func main() {
app := Setup()
if err := app.Listen(":3000"); err != nil {
Expand All @@ -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)
Expand All @@ -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)
}