forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
50 lines (42 loc) · 1.18 KB
/
version.go
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
41
42
43
44
45
46
47
48
49
50
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package operations
import (
"encoding/json"
"fmt"
"net/http"
"github.com/hyperledger/fabric/common/flogging"
)
type VersionInfoHandler struct {
CommitSHA string `json:"CommitSHA,omitempty"`
Version string `json:"Version,omitempty"`
}
func (m *VersionInfoHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
m.sendResponse(resp, http.StatusOK, m)
default:
err := fmt.Errorf("invalid request method: %s", req.Method)
m.sendResponse(resp, http.StatusBadRequest, err)
}
}
type errorResponse struct {
Error string `json:"Error"`
}
func (m *VersionInfoHandler) sendResponse(resp http.ResponseWriter, code int, payload interface{}) {
if err, ok := payload.(error); ok {
payload = &errorResponse{Error: err.Error()}
}
js, err := json.Marshal(payload)
if err != nil {
logger := flogging.MustGetLogger("operations.runner")
logger.Errorw("failed to encode payload", "error", err)
resp.WriteHeader(http.StatusInternalServerError)
} else {
resp.WriteHeader(code)
resp.Header().Set("Content-Type", "application/json")
resp.Write(js)
}
}