-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support non-JSON encodings by health.Handler (#284)
* Implement MarshalXML() by health.Health * Use goahttp.ResponseEncoder() to health.Handler() * Fix godoc of health.Handler() * Add an example to README --------- Co-authored-by: Raphael Simon <simon.raphael@gmail.com>
- Loading branch information
Showing
4 changed files
with
134 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
package health | ||
|
||
import ( | ||
"encoding/json" | ||
"context" | ||
"net/http" | ||
|
||
goahttp "goa.design/goa/v3/http" | ||
) | ||
|
||
// Handler returns a HTTP handler that serves health check requests. The | ||
// response body is the JSON encoded health status returned by chk.Check(). The | ||
// response status is 200 if chk.Check() returns a nil error, 503 otherwise. | ||
// response body is the health status returned by chk.Check(). By default | ||
// it's encoded as JSON, but you can specify a different encoding in the | ||
// HTTP Accept header. The response status is 200 if chk.Check() returns | ||
// a nil error, 503 otherwise. | ||
func Handler(chk Checker) http.HandlerFunc { | ||
encoder := goahttp.ResponseEncoder | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
h, healthy := chk.Check(r.Context()) | ||
b, _ := json.Marshal(h) | ||
ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept")) | ||
enc := encoder(ctx, w) | ||
h, healthy := chk.Check(ctx) | ||
if healthy { | ||
w.WriteHeader(http.StatusOK) | ||
} else { | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
} | ||
w.Write(b) // nolint: errcheck | ||
enc.Encode(h) // nolint: errcheck | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters