Skip to content

Commit dc15468

Browse files
authored
feat: add version endpoint (#71)
Resolves #56.
1 parent 07392c3 commit dc15468

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ jobs:
5454
push: ${{ steps.push_check.outputs.push }}
5555
tags: ${{ steps.meta.outputs.tags }}
5656
labels: ${{ steps.meta.outputs.labels }}
57+
build-args: |
58+
VERSION=${{ github.ref_name }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
tmp
2-
data/gorm.db
2+
gorm.db
33
coverage.out
4+
/backend

CONTRIBUTING.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ You will need the following tools:
1111

1212
Once those are installed, run `make setup` to perform the repository setup.
1313

14-
## Development server
14+
## Development commands
1515

16-
Run `make devserver` in the repository root, which will build and rebuild the project every time the code changes.
16+
- `make devserver` will start a development server on port 8080 and and rebuild the project every time the code changes.
17+
- `make test` runs all tests
18+
- `make coverage` runs all tests and opens the coverage report in your browser
19+
- `make build` builds the software with production configuration
1720

1821
## Commit messages
1922

@@ -22,6 +25,4 @@ to enable better overview over changes and enables automated tooling based on co
2225

2326
## Tests & test coverage
2427

25-
The test coverage goal is 100%. Please try to add tests for everything you add to the codebase. If in doubt, you’re always welcome to open an issue and ask for help.
26-
27-
To run tests, run `make test`. To show the test coverage graphically in your browser, run `make coverage`.
28+
The test coverage goal is > 95%. Please try to add tests for everything you add to the codebase. If in doubt, you’re always welcome to open an issue and ask for help.

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# syntax=docker/dockerfile:1
21
FROM golang:1.18-alpine as builder
32

4-
# needed for github.com/mattn/go-sqlite3
5-
RUN apk add --no-cache gcc g++
3+
RUN apk add --no-cache make
64

75
WORKDIR /app
86
COPY go.mod go.sum ./
97
RUN go mod download
108

119
COPY internal ./internal
12-
COPY main.go ./
13-
RUN CGO_ENABLED=0 go build -o /backend
10+
COPY main.go Makefile ./
11+
12+
ARG VERSION=0.0.0
13+
RUN CGO_ENABLED=0 make build VERSION=${VERSION}
1414

1515
# Build final image
1616
FROM scratch
1717
WORKDIR /
18-
COPY --from=builder /backend /backend
18+
COPY --from=builder /app/backend /backend
1919
ENTRYPOINT ["/backend"]

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ test:
1515
.PHONY: coverage
1616
coverage: test
1717
go tool cover -html=coverage.out
18+
19+
VERSION ?= $(shell git rev-parse HEAD)
20+
.PHONY: build
21+
build:
22+
go build -ldflags "-X github.com/envelope-zero/backend/internal/controllers.version=${VERSION}"
23+

internal/controllers/main_list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ var getOverviewTests = []struct {
1212
path string
1313
expected string
1414
}{
15-
{"/", `{ "links": { "v1": "http:///v1" }}`},
15+
{"/", `{ "links": { "v1": "http:///v1", "version": "http:///version" }}`},
1616
{"/v1", `{ "links": { "budgets": "http:///v1/budgets" }}`},
17+
{"/version", `{"data": { "version": "0.0.0" }}`},
1718
}
1819

1920
func TestGetOverview(t *testing.T) {

internal/controllers/main_options_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var optionsHeaderTests = []struct {
1313
expected string
1414
}{
1515
{"/", "GET"},
16+
{"/version", "GET"},
1617
{"/v1", "GET"},
1718
{"/v1/budgets", "GET, POST"},
1819
{"/v1/budgets/1", "GET, PATCH, DELETE"},

internal/controllers/routing.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
"github.com/rs/zerolog/log"
1515
)
1616

17+
// This is set at build time, see Makefile.
18+
var version = "0.0.0"
19+
1720
// Router controls the routes for the API.
1821
func Router() (*gin.Engine, error) {
1922
// Set up the router and middlewares
@@ -36,8 +39,6 @@ func Router() (*gin.Engine, error) {
3639
Logger()
3740
})))
3841

39-
// 12:47AM INF Request ip=::1 latency=0.711125 method=GET path=/v1/budgets status=200 user_agent=HTTPie/3.1.0
40-
4142
err := models.ConnectDatabase()
4243
if err != nil {
4344
return nil, fmt.Errorf("Database connection failed with: %s", err.Error())
@@ -47,7 +48,8 @@ func Router() (*gin.Engine, error) {
4748
r.GET("", func(c *gin.Context) {
4849
c.JSON(http.StatusOK, gin.H{
4950
"links": map[string]string{
50-
"v1": requestURL(c) + "v1",
51+
"v1": requestURL(c) + "v1",
52+
"version": requestURL(c) + "version",
5153
},
5254
})
5355
})
@@ -57,6 +59,18 @@ func Router() (*gin.Engine, error) {
5759
c.Header("allow", "GET")
5860
})
5961

62+
r.GET("/version", func(c *gin.Context) {
63+
c.JSON(http.StatusOK, gin.H{
64+
"data": map[string]string{
65+
"version": version,
66+
},
67+
})
68+
})
69+
70+
r.OPTIONS("/version", func(c *gin.Context) {
71+
c.Header("allow", "GET")
72+
})
73+
6074
// API v1 setup
6175
v1 := r.Group("/v1")
6276
{

0 commit comments

Comments
 (0)