From 0f47b6d64d17c67bbde83e2bf75b22c4f3f01b53 Mon Sep 17 00:00:00 2001 From: gipsh Date: Wed, 1 Feb 2023 10:46:22 -0300 Subject: [PATCH] add version endpoint (#118) Co-authored-by: gipsh --- api/Makefile | 20 ++++++++++++++- api/internal/build/build.go | 8 ++++++ .../wormscan/infrastructure/controller.go | 25 +++++++++++++++++++ api/routes/wormscan/routes.go | 1 + 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 api/internal/build/build.go diff --git a/api/Makefile b/api/Makefile index dc9d1298..e5cba891 100644 --- a/api/Makefile +++ b/api/Makefile @@ -1,5 +1,23 @@ SHELL := /bin/bash +# These will be provided to the target +GIT := github.com/wormhole-foundation/wormhole-explorer/api/internal/ +VERSION := 1.0.0 +BUILD := `git rev-parse --short HEAD` +AUTHOR := `whoami` +BUILD_DATE := `date +%Y%m%d%H%M%S` +BRANCH := `git branch --show-current` +MACHINE := `uname -n` + + +# Use linker flags to provide version/build settings to the target +LDFLAGS=-ldflags "-X=$(GIT)build.Version=$(VERSION)\ + -X=$(GIT)build.Time=$(BUILD_DATE)\ + -X=$(GIT)build.Build=$(BUILD)\ + -X=$(GIT)build.Branch=$(BRANCH)\ + -X=$(GIT)build.Machine=$(MACHINE)\ + -X=$(GIT)build.User=$(AUTHOR)" + ## help: print this help message .PHONY: help @@ -8,7 +26,7 @@ help: @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' build: - go build -o api main.go + go build -v $(LDFLAGS) -o api main.go doc: swag init -pd diff --git a/api/internal/build/build.go b/api/internal/build/build.go new file mode 100644 index 00000000..2b6f7505 --- /dev/null +++ b/api/internal/build/build.go @@ -0,0 +1,8 @@ +package build + +var Time string +var User string +var Build string +var Version string +var Branch string +var Machine string diff --git a/api/routes/wormscan/infrastructure/controller.go b/api/routes/wormscan/infrastructure/controller.go index f74b310f..8fbfefac 100644 --- a/api/routes/wormscan/infrastructure/controller.go +++ b/api/routes/wormscan/infrastructure/controller.go @@ -3,6 +3,7 @@ package infrastructure import ( "github.com/gofiber/fiber/v2" "github.com/wormhole-foundation/wormhole-explorer/api/handlers/infrastructure" + "github.com/wormhole-foundation/wormhole-explorer/api/internal/build" ) // Controller definition. @@ -49,3 +50,27 @@ func (c *Controller) ReadyCheck(ctx *fiber.Ctx) error { Ready string `json:"ready"` }{Ready: "NO"}) } + +// ReadyCheck handler for the endpoint /ready +// ReadyCheck godoc +// @Description Ready check +// @Tags Wormscan +// @ID ready-check +// @Success 200 {object} object{status=string} +// @Failure 400 +// @Failure 500 +// @Router /api/v1/version [get] +func (c *Controller) Version(ctx *fiber.Ctx) error { + return ctx.JSON(struct { + BuildDate string `json:"build_date"` + Build string `json:"build"` + Branch string `json:"branch"` + Machine string `json:"machine"` + User string `json:"user"` + }{BuildDate: build.Time, + Branch: build.Branch, + Build: build.Build, + Machine: build.Machine, + User: build.User}, + ) +} diff --git a/api/routes/wormscan/routes.go b/api/routes/wormscan/routes.go index d2707fc7..252067a0 100644 --- a/api/routes/wormscan/routes.go +++ b/api/routes/wormscan/routes.go @@ -51,6 +51,7 @@ func RegisterRoutes( // monitoring api.Get("/health", infrastructureCtrl.HealthCheck) api.Get("/ready", infrastructureCtrl.ReadyCheck) + api.Get("/version", infrastructureCtrl.Version) // vaas resource vaas := api.Group("/vaas")