-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_handler.go
More file actions
40 lines (34 loc) · 1.08 KB
/
Copy pathstate_handler.go
File metadata and controls
40 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package httpstatus
import (
"encoding/json"
"fmt"
"net/http"
)
type stateHandler struct {
statusCode int
body []byte
}
func newStateHandler(statusCode int, application, resource, state, version string) http.Handler {
response := jsonResponse{
Compatibility: fmt.Sprintf("%s:%s", application, state),
Application: application,
Resource: resource,
State: state,
Version: version,
}
body, _ := json.MarshalIndent(response, "", " ")
return &stateHandler{statusCode: statusCode, body: body}
}
func (this *stateHandler) ServeHTTP(response http.ResponseWriter, _ *http.Request) {
response.Header()["Content-Type"] = contentTypeJSON
response.WriteHeader(this.statusCode)
_, _ = response.Write(this.body)
}
type jsonResponse struct {
Compatibility string `json:"compatibility,omitempty"`
Application string `json:"application,omitempty"`
Resource string `json:"resource,omitempty"`
State string `json:"state,omitempty"`
Version string `json:"version,omitempty"`
}
var contentTypeJSON = []string{"application/json; charset=utf-8"}