-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_rest.go
More file actions
182 lines (146 loc) · 4.15 KB
/
grpc_rest.go
File metadata and controls
182 lines (146 loc) · 4.15 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package servers
import (
"context"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/protobuf/proto"
)
type grpcRestOptions struct {
withDocEndpoint bool
withVersionEndpoint bool
muxOpts []runtime.ServeMuxOption
register []func(mux *runtime.ServeMux) error
}
// WithServerMuxOption sets the options for the mux server.
// Apply to GRPCRest server instances.
func WithServerMuxOption(opts ...runtime.ServeMuxOption) Option {
return func(srv any) {
s, ok := srv.(*GRPCRest)
if !ok {
// skip does not apply to this instance.
return
}
s.options.muxOpts = append(s.options.muxOpts, opts...)
}
}
// WithDocEndpoint sets the options for the mux server to serve API docs.
func WithDocEndpoint(serviceName, basePath, filepath string, json []byte) Option {
return func(srv any) {
s, ok := srv.(*GRPCRest)
if !ok {
// skip does not apply to this instance.
return
}
s.options.withDocEndpoint = true
WithHandlers(NewRestAPIDocsHandlers(
serviceName,
basePath,
filepath,
json,
))(s)
}
}
// WithVersionEndpoint sets the options for the mux server to serve version.
func WithVersionEndpoint() Option {
return func(srv any) {
s, ok := srv.(*GRPCRest)
if !ok {
// skip does not apply to this instance.
return
}
s.options.withVersionEndpoint = true
WithHandlers(map[string]http.Handler{
"/version": NewRestVersionHandler(),
})(s)
}
}
// WithHandlers sets the options for custom path handlers to the mux server.
// Apply to GRPCRest server instances.
func WithHandlers(handlers map[string]http.Handler) Option {
return func(srv any) {
s, ok := srv.(*GRPCRest)
if !ok {
// skip does not apply to this instance.
return
}
for p, handler := range handlers {
s.options.register = append(s.options.register, func(mux *runtime.ServeMux) error {
err := mux.HandlePath(http.MethodGet, p, func(w http.ResponseWriter, r *http.Request, _ map[string]string) {
handler.ServeHTTP(w, r)
})
if err != nil {
return err
}
return nil
})
}
}
}
// WithResponseModifier sets the options for custom response modifier to the mux server.
func WithResponseModifier(modifier ...func(ctx context.Context, w http.ResponseWriter, _ proto.Message) error) Option {
return func(srv any) {
s, ok := srv.(*GRPCRest)
if !ok {
// skip does not apply to this instance.
return
}
opts := make([]runtime.ServeMuxOption, 0, len(modifier))
for _, m := range modifier {
opts = append(opts, runtime.WithForwardResponseOption(m))
}
WithServerMuxOption(opts...)(s)
}
}
// GRPCRestRegisterService is an interface for a grpc rest service that provides registration.
type GRPCRestRegisterService interface {
RegisterServiceHandler(mux *runtime.ServeMux) error
}
// WithRegisterServiceHandler registers a service handler to the mux server.
// Apply to GRPCRes server instances.
func WithRegisterServiceHandler(r GRPCRestRegisterService) Option {
return func(srv any) {
s, ok := srv.(*GRPCRest)
if !ok {
// skip does not apply to this instance.
return
}
s.options.register = append(s.options.register, r.RegisterServiceHandler)
}
}
// GRPCRest is a listening grpc rest server instance.
type GRPCRest struct {
*REST
options grpcRestOptions
}
// NewGRPCRest initiates a new wrapped grpc rest server.
func NewGRPCRest(config Config, opts ...Option) (*GRPCRest, error) {
srv := &GRPCRest{}
// Use custom error handler but can be modified by opts from consumers.
WithServerMuxOption(runtime.WithErrorHandler(customizeErrorHandler()))(srv)
for _, o := range opts {
o(srv)
}
var links []any
if srv.options.withDocEndpoint {
links = append(links, "API Docs", "/docs")
}
if srv.options.withVersionEndpoint {
links = append(links, "Version", "/version")
}
WithHandlers(map[string]http.Handler{
"/": NewRestRootHandler(config.Name, links...),
})(srv)
WithResponseModifier(
xhttpCodeResponseModifier(),
)(srv)
// Init REST Server.
mux := runtime.NewServeMux(srv.options.muxOpts...)
for _, register := range srv.options.register {
err := register(mux)
if err != nil {
return nil, err
}
}
srv.REST = NewREST(config, mux, opts...)
return srv, nil
}