forked from nytimes/gizmo
/
service.go
87 lines (73 loc) · 2.12 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
package api
import (
"net/http"
"github.com/NYTimes/gizmo/server/kit"
"github.com/NYTimes/gziphandler"
"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
"google.golang.org/grpc"
"github.com/NYTimes/gizmo/examples/nyt"
)
type (
// service will implement kit.Service.
service struct {
client nyt.Client
}
// Config is a struct to contain all the needed
// configuration for our Service
Config struct {
MostPopularToken string `envconfig:"MOST_POPULAR_TOKEN"`
SemanticToken string `envconfig:"SEMANTIC_TOKEN"`
}
)
var _ ApiServiceServer = service{}
// NewService will instantiate a Service
// with the given configuration.
func New(cfg Config) kit.Service {
return service{
nyt.NewClient(cfg.MostPopularToken, cfg.SemanticToken),
}
}
func (s service) HTTPRouterOptions() []kit.RouterOption {
return nil
}
func (s service) HTTPOptions() []httptransport.ServerOption {
return nil
}
// HTTPMiddleware provides an http.Handler hook wrapped around all requests.
// In this implementation, we're using a GzipHandler middleware to
// compress our responses.
func (s service) HTTPMiddleware(h http.Handler) http.Handler {
return gziphandler.GzipHandler(h)
}
// Middleware provides an kit/endpoint.Middleware hook wrapped around all requests.
func (s service) Middleware(e endpoint.Endpoint) endpoint.Endpoint {
return e
}
// JSONEndpoints is a listing of all endpoints available in the Service.
// If using Cloud Endpoints, this is not needed but handy for local dev.
func (s service) HTTPEndpoints() map[string]map[string]kit.HTTPEndpoint {
return map[string]map[string]kit.HTTPEndpoint{
"/svc/most-popular/{resourceType:[a-z]+}/{section:[a-z]+}/{timeframe:[0-9]+}": {
"GET": {
Endpoint: s.getMostPopular,
Decoder: decodeMostPopularRequest,
},
},
"/svc/cats": {
"GET": {
Endpoint: s.getCats,
},
},
}
}
func (s service) RPCMiddleware() grpc.UnaryServerInterceptor {
return nil
}
func (s service) RPCOptions() []grpc.ServerOption {
return nil
}
func (s service) RPCServiceDesc() *grpc.ServiceDesc {
// snagged from the pb.go file
return &_ApiService_serviceDesc
}