Skip to content

Commit

Permalink
Merge pull request #4 from maruina/root-handler
Browse files Browse the repository at this point in the history
Add root handler
  • Loading branch information
maruina authored Nov 26, 2019
2 parents 6cdc278 + 61840d2 commit afadf8b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
language: go
install: make dep-ci
script: make test-ci
env:
global:
secure: "s1Gacbpu60zQZTdDN2Akke3G3ZrqPoaFtnSfZZqFi3sfLDerZ9GqQuukhnU2KXpjA4DDKN91ZK07SXStcUJR3wImwxSmR8wS99JKeI5751VdZnAs0pfk9i33T6QVhwoeGuPPAGV3LsmQoZfrAc1fCUNyqhP84SkqWGHM1qqldQgaHx8HdepE5ro5Cc4MDhV9mOcdOXc0bOE/RNVHQIWClRn6YyiS7/+S0HhEEaVdWtlB0Zx5YxurSntgCHeluIx5ribqHMcpeVWtyNUB279uBZVpgrAg1xZRXOUhfsksA/iyoWNDevd9ozAnUhVs5tFBFSYVNVg99I3tfDpgMJ+qNL5NURh2bEwtt2T+ze5n3NHcrWz2ZmSbE6qyhwdnDzfAmZXGJVH52ocSnrHOQp3jOi7o4b2DVwi/YSXhr2VoqCKmnt856qCBQWQDtB419hxvhMA2NkEBGyQiM9/qB2xXzA76wIe5ZDCtjuik4KLDF04MEmmnqO76ub67nuwNzf/DlN29bY9EvzzkORRH7FNEdustzZaWpsMc01Xypxas2oyIusGhibDqcDgsYyFllRywdA5P9YA4XpyS/w7eSr+U67eFr5K1vtiV8sON0GMeyUURIGBwgV53VUfOCle8N1nakzrZh++vvPr+IrENQe/qJ6ZWKvjG3fSQVqZG0erDSfQ="
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ all: dep test
test:
go test -v -race ./...

test-ci:
go test -v -covermode=count -coverprofile=coverage.out ./...
${HOME}/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken ${COVERALLS_TOKEN}

build:
go build -o $(BINARY_NAME) cmd/$(BINARY_NAME)/main.go

dep:
go get ./...

dep-ci: dep
go get golang.org/x/tools/cmd/cover
go get github.com/mattn/goveralls

clean:
rm -f $(BINARY_NAME)
rm -f $(BINARY_NAME)
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,21 @@

## Endpoints

* `GET /`
* _returns_: a JSON response

```json
{
"status": "running",
"hostname": <hostname>
}
```

* `GET /healthcheck/liveness`
* _returns_: the JSON `{"status": "liveness probe healthy"}` if healthy or the status code `503` if unhealthy.
* _returns_: a JSON response if healthy or the status code `503` if unhealthy.

```json
{
"status": "liveness probe healthy"
}
```
20 changes: 17 additions & 3 deletions cmd/go-infrabin/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
)

// LivenessHandler handles the liveness check for the application
// RootHandler handles the "/" path
func RootHandler(w http.ResponseWriter, r *http.Request) {
name, err := os.Hostname()
if err != nil {
log.Fatalf("cannot get hostname: %v", err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
resp := fmt.Sprintf(`{"status": "running", "hostname": "%s"}`, name)
io.WriteString(w, resp)
}

// LivenessHandler handles the "/healthcheck/liveness" path
func LivenessHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{"status": "liveness probe healthy"}`)

}

func main() {
r := mux.NewRouter()

r.HandleFunc("/", RootHandler)
r.HandleFunc("/healthcheck/liveness", LivenessHandler)

srv := &http.Server{
Expand All @@ -29,6 +43,6 @@ func main() {
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Println("Starting go-infrabin")
log.Print("starting go-infrabin")
log.Fatal(srv.ListenAndServe())
}
27 changes: 27 additions & 0 deletions cmd/go-infrabin/main_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func TestRootHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(RootHandler)
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
name, err := os.Hostname()
if err != nil {
t.Fatal(err)
}
expected := fmt.Sprintf(`{"status": "running", "hostname": "%s"}`, name)
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}

func TestLivenessHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
Expand Down

0 comments on commit afadf8b

Please sign in to comment.