diff --git a/Makefile b/Makefile index 4dc2804e..cb9a9c23 100644 --- a/Makefile +++ b/Makefile @@ -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 ./... diff --git a/api/info/main.go b/api/info/main.go new file mode 100644 index 00000000..8145841b --- /dev/null +++ b/api/info/main.go @@ -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) +} diff --git a/script/build.sh b/script/build.sh index 2439fa0b..01d76495 100755 --- a/script/build.sh +++ b/script/build.sh @@ -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 diff --git a/serverless.yml.example b/serverless.yml.example index 94f99a19..bc6e22de 100644 --- a/serverless.yml.example +++ b/serverless.yml.example @@ -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: