-
Notifications
You must be signed in to change notification settings - Fork 9
/
http.go
executable file
·61 lines (52 loc) · 1.95 KB
/
http.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
51
52
53
54
55
56
57
58
59
60
61
package export
import (
"context"
"encoding/json"
"net/http"
cs "github.com/cloudtrust/common-service"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
"github.com/go-kit/kit/endpoint"
http_transport "github.com/go-kit/kit/transport/http"
"github.com/pkg/errors"
)
// MakeHTTPExportHandler makes a HTTP handler for the export endpoint.
func MakeHTTPExportHandler(e endpoint.Endpoint) *http_transport.Server {
return http_transport.NewServer(e,
decodeHTTPRequest,
encodeHTTPReply,
http_transport.ServerErrorEncoder(errorHandler),
http_transport.ServerBefore(fetchHTTPCorrelationID),
)
}
// fetchHTTPCorrelationID reads the correlation ID from the http header "X-Correlation-ID".
// If the ID is not zero, we put it in the context.
func fetchHTTPCorrelationID(ctx context.Context, req *http.Request) context.Context {
var correlationID = req.Header.Get("X-Correlation-ID")
if correlationID != "" {
ctx = context.WithValue(ctx, cs.CtContextCorrelationID, correlationID)
}
return ctx
}
// decodeHTTPRequest decodes the http event request.
func decodeHTTPRequest(_ context.Context, _ *http.Request) (res interface{}, err error) {
return nil, nil
}
// encodeHTTPReply encodes the http event reply.
func encodeHTTPReply(_ context.Context, w http.ResponseWriter, res interface{}) error {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
var reply = res.(map[string]interface{})
var data, err = json.MarshalIndent(reply, "", " ")
if err != nil {
return errors.Wrap(err, msg.MsgErrCannotMarshal+msg.Response)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
return nil
}
// errorHandler encodes the reply when there is an error.
func errorHandler(_ context.Context, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/octet-stream")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(keycloakb.ComponentName + "." + err.Error()))
}