forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_go17.go
30 lines (24 loc) · 858 Bytes
/
service_go17.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
// +build go1.7
package service
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
"github.com/NYTimes/gizmo/server"
)
// JSONMiddleware provides a JSONContextEndpoint hook wrapped around all requests.
// In this implementation, we're using it to provide application logging and to check errors
// and provide generic responses.
func (s *RPCService) JSONMiddleware(j server.JSONContextEndpoint) server.JSONContextEndpoint {
return func(ctx context.Context, r *http.Request) (int, interface{}, error) {
status, res, err := j(ctx, r)
if err != nil {
server.LogWithFields(r).WithFields(logrus.Fields{
"error": err,
}).Error("problems with serving request")
return http.StatusServiceUnavailable, nil, &jsonErr{"sorry, this service is unavailable"}
}
server.LogWithFields(r).Info("success!")
return status, res, nil
}
}