From dda31a843bc20a58f0551ad99254708d05db5dd2 Mon Sep 17 00:00:00 2001 From: Vince Tse <1501612+vincetse@users.noreply.github.com> Date: Sat, 2 Dec 2017 17:20:42 -0500 Subject: [PATCH] Add version endpoint and upx (#3) * Add ident * add build artifacts to gitignore * Add upx to shrink binary --- .gitattributes | 1 + .gitignore | 4 ++++ .travis.yml | 4 ++++ Dockerfile | 6 +++++- Makefile | 1 + hello_world.go | 10 ++++++++++ 6 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..31884f8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* ident diff --git a/.gitignore b/.gitignore index 447ab76..d5ea5bc 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ Session.vim *~ # auto-generated tag files tags + +# build artifacts +hello_world +docker-hello-world diff --git a/.travis.yml b/.travis.yml index 0ac1e0d..2c5155b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,5 +3,9 @@ go: - 1.8 - 1.9 - master +addons: + apt: + packages: + - upx script: - make all diff --git a/Dockerfile b/Dockerfile index 20eeb01..e47fdd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,12 @@ FROM golang:1.9.2 AS builder WORKDIR /go/src/github.com/infrastructure-as-code/docker-hello-world ENV GIN_MODE debug +ENV DEBIAN_FRONTEND noninteractive COPY Makefile *.go ./ -RUN make all +RUN apt-get update && \ + apt-get upgrade -y && \ + apt-get install -y upx && \ + make all FROM scratch LABEL maintainer "Vince Tse " diff --git a/Makefile b/Makefile index 2edc088..2b8873e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ all: deps test CGO_ENABLED=0 go build -a -o hello_world + upx --brute hello_world test: GIN_MODE=debug go test diff --git a/hello_world.go b/hello_world.go index fc2bd0f..316e984 100644 --- a/hello_world.go +++ b/hello_world.go @@ -19,6 +19,10 @@ func getHostname() string { return name } +func getVersion() string { + return "$Id$" +} + func helloFunc(c *gin.Context) { c.Writer.Header().Set("X-Hostname", hostname) c.String(http.StatusOK, "Hello, World!") @@ -29,6 +33,11 @@ func healthFunc(c *gin.Context) { c.String(http.StatusOK, "") } +func versionFunc(c *gin.Context) { + c.Writer.Header().Set("X-Hostname", hostname) + c.String(http.StatusOK, getVersion()) +} + func setupRouter(routePrefix string) *gin.Engine { router := gin.Default() ginprom := ginprometheus.NewPrometheus("gin") @@ -37,6 +46,7 @@ func setupRouter(routePrefix string) *gin.Engine { rg := router.Group(routePrefix) rg.GET("/", helloFunc) + rg.GET("/version", versionFunc) return router }