Skip to content

Commit

Permalink
Add /info route to return build info (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
harryzcy authored Jan 11, 2023
1 parent dac504b commit 8a50ec9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
.PHONY: build clean deploy remove test

.PHONY: build
build:
./script/build.sh

.PHONY: clean
clean:
rm -rf ./bin ./vendor

.PHONY: deploy
deploy: clean build
sls deploy --verbose

.PHONY: remove
remove: clean
sls remove --verbose

.PHONY: test
test:
go test -race -covermode=atomic ./...
35 changes: 35 additions & 0 deletions api/info/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

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

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
body, err := json.Marshal(map[string]string{
"version": version,
"commit": commit,
"build": buildDate,
})
if err != nil {
fmt.Printf("marshal failed: %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
}
return apiutil.NewSuccessJSONResponse(string(body)), nil
}

func main() {
lambda.Start(handler)
}
19 changes: 17 additions & 2 deletions script/build.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
#!/bin/bash

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

ENVIRONMENT="env GOOS=linux GOARCH=amd64"

apiFuncs=("list" "get" "getRaw" "getContent" "read" "trash" "untrash" "delete" "create" "save" "send")

for i in "${!apiFuncs[@]}";
do
func="${apiFuncs[$i]}"
env GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/api/emails/"${func}" api/emails/"${func}"/*
${ENVIRONMENT} go build -ldflags="-s -w" -o bin/api/emails/"${func}" api/emails/"${func}"/*
cp bin/api/emails/"${func}" bin/bootstrap
zip -j bin/"${func}".zip bin/bootstrap
done

env GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/functions/emailReceive functions/emailReceive/*
${ENVIRONMENT} go build -ldflags="-s -w \
-X 'main.version=${BUILD_VERSION}' \
-X 'main.commit=${BUILD_COMMIT}' \
-X 'main.buildDate=${BUILD_DATE}' \
" \
-o bin/api/info api/info/*
cp bin/api/info bin/bootstrap
zip -j bin/info.zip bin/bootstrap

${ENVIRONMENT} go build -ldflags="-s -w" -o bin/functions/emailReceive functions/emailReceive/*
cp bin/functions/emailReceive bin/bootstrap
zip -j bin/emailReceive.zip bin/bootstrap
rm bin/bootstrap
10 changes: 10 additions & 0 deletions serverless.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ functions:
type: aws_iam
package:
artifact: bin/send.zip
info:
handler: bootstrap
events:
- httpApi:
method: GET
path: /info
authorizer:
type: aws_iam
package:
artifact: bin/info.zip

resources:
Resources:
Expand Down

0 comments on commit 8a50ec9

Please sign in to comment.