Skip to content

Commit bee9630

Browse files
authored
Remade in Golang (#1)
* Update to Golang with Prometheus instrumentation * Add logging * Use gin instead of net/http * Remove unnecessary library * Instrument gin app * Add CI/CD * Remove docker * Add latest master go * Add build status * Cleanup
1 parent 51bfbbf commit bee9630

File tree

7 files changed

+123
-44
lines changed

7 files changed

+123
-44
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
language: python
2-
services:
3-
- docker
1+
language: go
2+
go:
3+
- 1.8
4+
- 1.9
5+
- master
46
script:
5-
- docker build -t infrastructureascode/hello-world .
7+
- make all

Dockerfile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
FROM alpine
1+
# Copied from https://github.com/prometheus/client_golang/blob/master/examples/simple/Dockerfile
22

3-
RUN apk update && \
4-
apk upgrade && \
5-
apk add nginx && \
6-
mkdir -p /run/nginx
3+
FROM golang:1.9.2 AS builder
4+
WORKDIR /go/src/github.com/infrastructure-as-code/docker-hello-world
5+
ENV GIN_MODE debug
6+
COPY Makefile *.go ./
7+
RUN make all
78

8-
COPY default.conf /etc/nginx/conf.d/default.conf
9-
EXPOSE 80
10-
CMD nginx -g "daemon off;"
9+
FROM scratch
10+
LABEL maintainer "Vince Tse <[email protected]>"
11+
COPY --from=builder /go/src/github.com/infrastructure-as-code/docker-hello-world/hello_world .
12+
ENV GIN_MODE release
13+
EXPOSE 8080
14+
ENTRYPOINT ["/hello_world"]

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all: deps test
2+
CGO_ENABLED=0 go build -a -o hello_world
3+
4+
test:
5+
GIN_MODE=debug go test
6+
7+
deps:
8+
go get -d
9+
go get github.com/stretchr/testify

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
[![Build Status](https://travis-ci.org/infrastructure-as-code/docker-hello-world.svg?branch=master)](https://travis-ci.org/infrastructure-as-code/docker-hello-world)
2+
13
# infrastructureascode/hello-world
24

3-
A Docker "Hello World" web server inspired by the [tutum/hello-world](https://hub.docker.com/r/tutum/hello-world/) image which seems to have become unmaintained, and returns HTTP/400 more often than it should.
5+
A [Prometheus](https://prometheus.io/)-instrumented Docker "Hello World" web server.
46

57
## Features
68

7-
1. Always returns a HTTP 200 status code and a "Hello, world!" message.
8-
1. Has a health check endpoint, `/health`, that returns an empty response.
9+
1. Always returns a HTTP 200 status code and a "Hello, World!" message at the `/` path.
10+
1. Has a metrics endpoint at `/metrics` that returns Prometheus metrics.
11+
1. Has a health check endpoint, `/health`, that returns an empty response and a HTTP 200 response.
912

1013
## Building
1114

1215
```
13-
docker build --rm infrastructureascode/hello-world .
16+
docker build --rm -t infrastructureascode/hello-world .
1417
```
1518

1619
## Usage
@@ -20,12 +23,15 @@ docker build --rm infrastructureascode/hello-world .
2023
docker run \
2124
--detach \
2225
--name hello-world \
23-
--publish 8000:80 \
26+
--publish 8000:8080 \
2427
infrastructureascode/hello-world
2528
2629
# curl the container
2730
curl http://0.0.0.0:8000/
2831
2932
# curl the health check endpoint which returns an empty response
3033
curl http://0.0.0.0:8000/health
34+
35+
# curl Prometheus metrics
36+
curl http://0.0.0.0:8000/metrics
3137
```

default.conf

Lines changed: 0 additions & 27 deletions
This file was deleted.

hello_world.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"os"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/zsais/go-gin-prometheus"
9+
)
10+
11+
var hostname = getHostname()
12+
13+
func getHostname() string {
14+
var name, err = os.Hostname()
15+
if err != nil {
16+
panic(err)
17+
}
18+
return name
19+
}
20+
21+
func helloFunc(c *gin.Context) {
22+
c.Writer.Header().Set("X-Hostname", hostname)
23+
c.String(http.StatusOK, "Hello, World!")
24+
}
25+
26+
func healthFunc(c *gin.Context) {
27+
c.Writer.Header().Set("X-Hostname", hostname)
28+
c.String(http.StatusOK, "")
29+
}
30+
31+
func setupRouter() *gin.Engine {
32+
router := gin.Default()
33+
ginprom := ginprometheus.NewPrometheus("gin")
34+
ginprom.Use(router)
35+
router.GET("/", helloFunc)
36+
router.GET("/health", healthFunc)
37+
return router
38+
}
39+
40+
func main() {
41+
router := setupRouter()
42+
router.Run()
43+
}

hello_world_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
package main
3+
4+
import (
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestDefaultRoute(t *testing.T) {
13+
router := setupRouter()
14+
15+
w := httptest.NewRecorder()
16+
req, _ := http.NewRequest("GET", "/", nil)
17+
router.ServeHTTP(w, req)
18+
19+
assert.Equal(t, 200, w.Code)
20+
assert.Equal(t, "Hello, World!", w.Body.String())
21+
}
22+
23+
func TestHealthRoute(t *testing.T) {
24+
router := setupRouter()
25+
26+
w := httptest.NewRecorder()
27+
req, _ := http.NewRequest("GET", "/health", nil)
28+
router.ServeHTTP(w, req)
29+
30+
assert.Equal(t, 200, w.Code)
31+
assert.Equal(t, "", w.Body.String())
32+
}
33+
34+
func TestMetricsRoute(t *testing.T) {
35+
router := setupRouter()
36+
37+
w := httptest.NewRecorder()
38+
req, _ := http.NewRequest("GET", "/metrics", nil)
39+
router.ServeHTTP(w, req)
40+
41+
assert.Equal(t, 200, w.Code)
42+
}

0 commit comments

Comments
 (0)