Skip to content

Commit

Permalink
[bff] Add /ping and /info routes (#69)
Browse files Browse the repository at this point in the history
* [bff] Add ping route

* Include info endpoint

* Add build into via Makefile

* Move info function to a seperate file

* Include build variables in workflows

* Fix ambiguous redirect

* Fix unexpected EOF

* Use git describe always

* Fix checking envs

* Try again

* Now should be working
  • Loading branch information
harryzcy authored Jan 3, 2023
1 parent c0ec5ea commit 672c62d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,25 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Get build variables
run: |
echo "BUILD_COMMIT=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV
echo "BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_ENV
echo "BUILD_VERSION=$(git describe --tags --always)" >> $GITHUB_ENV
- name: Check build variables
run: |
echo $BUILD_COMMIT
echo $BUILD_DATE
echo $BUILD_VERSION
- name: Build docker image
run: docker build -t ${{ github.repository }} .
uses: docker/build-push-action@v3
with:
context: .
build-args: |
BUILD_DATE=${{ env.BUILD_DATE }}
BUILD_COMMIT=${{ env.BUILD_COMMIT }}
BUILD_VERSION=${{ env.BUILD_VERSION }}
platforms: linux/amd64
push: false
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Get build variables
run: |
echo "BUILD_COMMIT=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV
echo "BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_ENV
echo "BUILD_VERSION=$(git describe --tags --always)" >> $GITHUB_ENV
- name: Check build variables
run: |
echo $BUILD_COMMIT
echo $BUILD_DATE
echo $BUILD_VERSION
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
Expand Down Expand Up @@ -72,6 +84,10 @@ jobs:
uses: docker/build-push-action@v3
with:
context: .
build-args: |
BUILD_DATE=${{ env.BUILD_DATE }}
BUILD_COMMIT=${{ env.BUILD_COMMIT }}
BUILD_VERSION=${{ env.BUILD_VERSION }}
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
15 changes: 13 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
FROM golang:1.19.4-alpine3.17 as bff-builder

ARG BUILD_VERSION
ARG BUILD_COMMIT
ARG BUILD_DATE

WORKDIR /go/src/bff
COPY ./bff ./
RUN go mod download

RUN go build -o /bin/bff
RUN set -ex && \
go mod download && \
go build \
-ldflags=" \
-X 'github.com/harryzcy/mailbox-browser/bff/transport/rest/misc.version=${BUILD_VERSION}' \
-X 'github.com/harryzcy/mailbox-browser/bff/transport/rest/misc.commit=${BUILD_COMMIT}' \
-X 'github.com/harryzcy/mailbox-browser/bff/transport/rest/misc.buildDate=${BUILD_DATE}' \
" \
-o /bin/bff

FROM node:18.12.1-alpine3.17 as web-builder

Expand Down
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
DOCKER_IMAGE = "mailbox-browser"

BUILD_COMMIT = $(shell git rev-parse --short HEAD)
BUILD_DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
BUILD_VERSION = $(shell git describe --tags --always)

ARG_BUILD_COMMIT = --build-arg BUILD_COMMIT=$(BUILD_COMMIT)
ARG_BUILD_DATE = --build-arg BUILD_DATE=$(BUILD_DATE)
ARG_BUILD_VERSION = --build-arg BUILD_VERSION=$(BUILD_VERSION)
DOCKER_BUILD_ARGS = $(ARG_BUILD_COMMIT) $(ARG_BUILD_DATE) $(ARG_BUILD_VERSION)

.PHONY: web
web:
@echo "Building web..."
Expand All @@ -8,4 +17,5 @@ web:
.PHONY: docker
docker:
@echo "Building docker..."
@docker build -t $(DOCKER_IMAGE) .
@echo "Build commit: $(DOCKER_BUILD_ARGS)"
@docker build $(DOCKER_BUILD_ARGS) -t $(DOCKER_IMAGE) .
19 changes: 19 additions & 0 deletions bff/transport/rest/misc/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package misc

import (
"github.com/gin-gonic/gin"
)

var (
version = "dev"
commit = "n/a"
buildDate = "n/a"
)

func Info(c *gin.Context) {
c.JSON(200, gin.H{
"version": version,
"commit": commit,
"build": buildDate,
})
}
11 changes: 11 additions & 0 deletions bff/transport/rest/misc/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package misc

import (
"github.com/gin-gonic/gin"
)

func Ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
5 changes: 5 additions & 0 deletions bff/transport/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.uber.org/zap"

"github.com/harryzcy/mailbox-browser/bff/config"
"github.com/harryzcy/mailbox-browser/bff/transport/rest/misc"
"github.com/harryzcy/mailbox-browser/bff/transport/rest/web"
)

Expand Down Expand Up @@ -42,6 +43,10 @@ func Init(logger *zap.Logger, mode string) *gin.Engine {
emails.POST("/:id/send", web.MailboxProxy) // send
}

// misc routes
r.GET("/ping", misc.Ping)
r.GET("/info", misc.Info)

r.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/assets/") {
c.File(filepath.Join(config.STATIC_DIR, c.Request.URL.Path))
Expand Down

0 comments on commit 672c62d

Please sign in to comment.