Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 395c1cd

Browse files
authored
Merge pull request #8 from randlabs/health_redesign
Health callback change.
2 parents 896c6c5 + a7ddb67 commit 395c1cd

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ func main() {
5151
mc.Destroy()
5252
}
5353

54-
// Health output is in JSON format. Don't forget to add json tags.
54+
// Health output will be in JSON format.
5555
type exampleHealthOutput struct {
5656
Status string `json:"status"`
5757
}
5858

5959
// Our health callback routine.
60-
func healthCallback() interface{} {
61-
return exampleHealthOutput{
60+
func healthCallback() string {
61+
state := exampleHealthOutput{
6262
Status: "ok",
6363
}
64+
65+
j, _ := json.Marshal(state)
66+
return string(j)
6467
}
6568
```
6669

handlers.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010

1111
func (mws *Controller) getHealthHandler() webserver.HandlerFunc {
1212
return func(req *request.RequestContext) error {
13-
// Get current state from callback
14-
state := mws.healthCallback()
13+
// Get current health status from callback
14+
status := mws.healthCallback()
1515

16-
// Encode and send output
17-
req.WriteJSON(state)
16+
// Send output
17+
if len(status) > 0 {
18+
req.WriteString(status)
19+
}
1820
req.Success()
1921

2022
// Done

metrics.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ type Options struct {
6060
Middlewares []webserver.MiddlewareFunc
6161
}
6262

63-
// HealthCallback indicates a function that returns an object that will be returned as a JSON output.
64-
type HealthCallback func() interface{}
63+
// HealthCallback indicates a function that returns a string that will be returned as the output.
64+
type HealthCallback func() string
6565

6666
// -----------------------------------------------------------------------------
6767

metrics_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package metrics_test
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"math/rand"
67
"os"
@@ -129,10 +130,13 @@ func TestWebServer(t *testing.T) {
129130

130131
// -----------------------------------------------------------------------------
131132

132-
func healthCallback() interface{} {
133-
return State{
133+
func healthCallback() string {
134+
state := State{
134135
System: "all services running",
135136
}
137+
138+
j, _ := json.Marshal(state)
139+
return string(j)
136140
}
137141

138142
func openBrowser(url string) {

0 commit comments

Comments
 (0)