forked from nytimes/gizmo
/
service.go
93 lines (81 loc) · 3.48 KB
/
service.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package kit
import (
"net/http"
"google.golang.org/grpc"
"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
)
// HTTPEndpoint encapsulates everything required to build
// an endpoint hosted on a kit server.
type HTTPEndpoint struct {
Endpoint endpoint.Endpoint
Decoder httptransport.DecodeRequestFunc
Encoder httptransport.EncodeResponseFunc
Options []httptransport.ServerOption
}
// Service is the interface of mixed HTTP/gRPC that can be registered and
// hosted by a gizmo/server/kit server. Services provide hooks for service-wide options
// and middlewares and can be used as a means of dependency injection.
// In general, a Service should just contain the logic for deserializing and authorizing
// requests, passing the request to a business logic interface abstraction,
// handling errors and serializing the apprioriate response.
//
// In other words, each Endpoint is similar to a 'controller' and the Service
// a container for injecting depedencies (business services, repositories, etc.)
// into each request handler.
type Service interface {
// Middleware is for any service-wide go-kit middlewares. This middleware
// is applied to both HTTP and gRPC services.
Middleware(endpoint.Endpoint) endpoint.Endpoint
// HTTPMiddleware is for service-wide http specific middleware
// for easy integration with 3rd party http.Handlers like nytimes/gziphandler.
HTTPMiddleware(http.Handler) http.Handler
// HTTPOptions are service-wide go-kit HTTP server options
HTTPOptions() []httptransport.ServerOption
// HTTPRouterOptions allows users to override the default
// behavior and use of the GorillaRouter.
HTTPRouterOptions() []RouterOption
// HTTPEndpoints default to using a JSON serializer if no encoder is provided.
// For example:
//
// return map[string]map[string]kit.HTTPEndpoint{
// "/cat/{id}": {
// "GET": {
// Endpoint: s.GetCatByID,
// Decoder: decodeGetCatRequest,
// },
// },
// "/cats": {
// "PUT": {
// Endpoint: s.PutCats,
// HTTPDecoder: decodePutCatsProtoRequest,
// },
// "GET": {
// Endpoint: s.GetCats,
// HTTPDecoder: decodeGetCatsRequest,
// },
// },
// }
HTTPEndpoints() map[string]map[string]HTTPEndpoint
// RPCMiddleware is for any service-wide gRPC specific middleware
// for easy integration with 3rd party grpc.UnaryServerInterceptors like
// http://godoc.org/cloud.google.com/go/trace#Client.GRPCServerInterceptor
//
// The underlying kit server already uses the one available grpc.UnaryInterceptor
// grpc.ServerOption so attempting to pass your own in this Service's RPCOptions()
// will cause a panic at startup.
//
// If you want to apply multiple RPC middlewares,
// we recommend using:
// http://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware#ChainUnaryServer
RPCMiddleware() grpc.UnaryServerInterceptor
// RPCServiceDesc allows services to declare an alternate gRPC
// representation of themselves to be hosted on the RPC_PORT (8081 by default).
RPCServiceDesc() *grpc.ServiceDesc
// RPCOptions are for service-wide gRPC server options.
//
// The underlying kit server already uses the one available grpc.UnaryInterceptor
// grpc.ServerOption so attempting to pass your own in this method will cause a panic
// at startup. We recommend using RPCMiddleware() to fill this need.
RPCOptions() []grpc.ServerOption
}