forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
196 lines (173 loc) · 5.48 KB
/
options.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package restful
import (
"context"
"net/http"
"time"
"github.com/valyala/fasthttp"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/filter"
)
// Options are restful router options.
type Options struct {
namespace string // global namespace
environment string // global environment
container string // global container name
set string // global set name
ServiceName string // tRPC service name
ServiceImpl interface{} // tRPC service impl
FilterFunc ExtractFilterFunc // extract tRPC service filter chain
ErrorHandler ErrorHandler // error handler
HeaderMatcher HeaderMatcher // header matcher
ResponseHandler CustomResponseHandler // custom response handler
FastHTTPErrHandler FastHTTPErrorHandler // fasthttp error handler
FastHTTPHeaderMatcher FastHTTPHeaderMatcher // fasthttp header matcher
FastHTTPRespHandler FastHTTPRespHandler // fasthttp custom response handler
DiscardUnknownParams bool // ignore unknown query params
Timeout time.Duration // timeout
}
// Option sets restful router options.
type Option func(*Options)
func (o *Options) rebuildHeaderMatcher() {
headerMatcher := o.HeaderMatcher
o.HeaderMatcher = func(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
serviceName, methodName string,
) (context.Context, error) {
ctx, err := headerMatcher(ctx, w, r, serviceName, methodName)
if err != nil {
return nil, err
}
return withGlobalMsg(ctx, o), nil
}
fastHTTPHeaderMatcher := o.FastHTTPHeaderMatcher
o.FastHTTPHeaderMatcher = func(
ctx context.Context,
requestCtx *fasthttp.RequestCtx,
serviceName, methodName string,
) (context.Context, error) {
ctx, err := fastHTTPHeaderMatcher(ctx, requestCtx, serviceName, methodName)
if err != nil {
return nil, err
}
return withGlobalMsg(ctx, o), nil
}
}
// WithNamespace returns an Option that set namespace.
func WithNamespace(namespace string) Option {
return func(o *Options) {
o.namespace = namespace
}
}
// WithEnvironment returns an Option that sets environment name.
func WithEnvironment(env string) Option {
return func(o *Options) {
o.environment = env
}
}
// WithContainer returns an Option that sets container name.
func WithContainer(container string) Option {
return func(o *Options) {
o.container = container
}
}
// WithSet returns an Option that sets set name.
func WithSet(set string) Option {
return func(o *Options) {
o.set = set
}
}
// WithServiceName returns an Option that sets tRPC service name for the restful router.
func WithServiceName(name string) Option {
return func(o *Options) {
o.ServiceName = name
}
}
// WithFilterFunc returns an Option that sets tRPC service filter chain extracting function
// for the restful router.
func WithFilterFunc(f ExtractFilterFunc) Option {
return func(o *Options) {
if getFilters := o.FilterFunc; getFilters != nil {
o.FilterFunc = func() filter.ServerChain {
return append(getFilters(), f()...)
}
} else {
o.FilterFunc = f
}
}
}
// WithErrorHandler returns an Option that sets error handler for the restful router.
func WithErrorHandler(errorHandler ErrorHandler) Option {
return func(o *Options) {
o.ErrorHandler = errorHandler
}
}
// WithHeaderMatcher returns an Option that sets header matcher for the restful router.
func WithHeaderMatcher(m HeaderMatcher) Option {
return func(o *Options) {
o.HeaderMatcher = m
}
}
// WithResponseHandler returns an Option that sets custom response handler for
// the restful router.
func WithResponseHandler(h CustomResponseHandler) Option {
return func(o *Options) {
o.ResponseHandler = h
}
}
// WithFastHTTPErrorHandler returns an Option that sets fasthttp error handler
// for the restful router.
func WithFastHTTPErrorHandler(errHandler FastHTTPErrorHandler) Option {
return func(o *Options) {
o.FastHTTPErrHandler = errHandler
}
}
// WithFastHTTPHeaderMatcher returns an Option that sets fasthttp header matcher
// for the restful router.
func WithFastHTTPHeaderMatcher(m FastHTTPHeaderMatcher) Option {
return func(o *Options) {
o.FastHTTPHeaderMatcher = m
}
}
// WithFastHTTPRespHandler returns an Option that sets fasthttp custom response
// handler for the restful router.
func WithFastHTTPRespHandler(h FastHTTPRespHandler) Option {
return func(o *Options) {
o.FastHTTPRespHandler = h
}
}
// WithDiscardUnknownParams returns an Option that sets whether to ignore unknown query params
// for the restful router.
func WithDiscardUnknownParams(i bool) Option {
return func(o *Options) {
o.DiscardUnknownParams = i
}
}
// WithTimeout returns an Option that sets timeout for the restful router.
func WithTimeout(t time.Duration) Option {
return func(o *Options) {
o.Timeout = t
}
}
// withGlobalMsg sets tRPC yaml global fields to ctx message.
func withGlobalMsg(ctx context.Context, o *Options) context.Context {
ctx, msg := codec.EnsureMessage(ctx)
msg.WithNamespace(o.namespace)
msg.WithEnvName(o.environment)
msg.WithCalleeContainerName(o.container)
msg.WithSetName(o.set)
return ctx
}