github.com/kaydxh/golang@v0.0.131/pkg/grpc-gateway/grpc_gateway_http.option.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package grpcgateway 23 24 import ( 25 "net/http" 26 "time" 27 28 "github.com/gin-gonic/gin/binding" 29 "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" 30 http_ "github.com/kaydxh/golang/go/net/http" 31 marshaler_ "github.com/kaydxh/golang/go/runtime/marshaler" 32 interceptortcloud3_ "github.com/kaydxh/golang/pkg/middleware/api/tcloud/v3.0" 33 interceptortrivialv1_ "github.com/kaydxh/golang/pkg/middleware/api/trivial/v1" 34 interceptortrivialv2_ "github.com/kaydxh/golang/pkg/middleware/api/trivial/v2" 35 httpinterceptordebug_ "github.com/kaydxh/golang/pkg/middleware/http-middleware/debug" 36 httpinterceptorhttp_ "github.com/kaydxh/golang/pkg/middleware/http-middleware/http" 37 httpinterceptoropentelemetr_ "github.com/kaydxh/golang/pkg/middleware/http-middleware/monitor/opentelemetry" 38 httpinterceptorlimiter_ "github.com/kaydxh/golang/pkg/middleware/http-middleware/ratelimiter" 39 httpinterceptortimer_ "github.com/kaydxh/golang/pkg/middleware/http-middleware/timer" 40 ) 41 42 func WithGatewayMuxOptions(opts ...runtime.ServeMuxOption) GRPCGatewayOption { 43 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 44 c.opts.gatewayMuxOptions = append(c.opts.gatewayMuxOptions, opts...) 45 }) 46 } 47 48 //now unused, only called by successed response, only append message to response 49 func WithServerInterceptorsHTTPForwardResponseOptions() GRPCGatewayOption { 50 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 51 WithGatewayMuxOptions(runtime.WithForwardResponseOption(interceptortcloud3_.HTTPForwardResponse)).apply(c) 52 }) 53 } 54 55 //now unused, only called by successed response 56 func WithServerInterceptorsTrivialV1HTTPForwardResponseOptions() GRPCGatewayOption { 57 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 58 WithGatewayMuxOptions(runtime.WithForwardResponseOption(interceptortrivialv1_.HTTPForwardResponse)).apply(c) 59 }) 60 } 61 62 //tcloud api3.0 http response formatter 63 func WithServerInterceptorsTCloud30HTTPResponseOptions() GRPCGatewayOption { 64 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 65 WithGatewayMuxOptions( 66 runtime.WithMarshalerOption(runtime.MIMEWildcard, interceptortcloud3_.NewDefaultJSONPb()), 67 ).apply( 68 c, 69 ) 70 71 WithGatewayMuxOptions( 72 runtime.WithMarshalerOption(binding.MIMEJSON, interceptortcloud3_.NewDefaultJSONPb()), 73 ).apply( 74 c, 75 ) 76 }) 77 } 78 79 //trivial api1.0 http response formatter 80 func WithServerInterceptorsTrivialV1HTTPResponseOptions() GRPCGatewayOption { 81 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 82 WithGatewayMuxOptions( 83 runtime.WithMarshalerOption(runtime.MIMEWildcard, marshaler_.NewDefaultJSONPb()), 84 ).apply( 85 c, 86 ) 87 88 WithGatewayMuxOptions( 89 runtime.WithMarshalerOption(binding.MIMEJSON, marshaler_.NewDefaultJSONPb()), 90 ).apply( 91 c, 92 ) 93 }) 94 } 95 96 //trivial api2.0 http response formatter 97 func WithServerInterceptorsTrivialV2HTTPResponseOptions() GRPCGatewayOption { 98 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 99 WithGatewayMuxOptions( 100 runtime.WithMarshalerOption(runtime.MIMEWildcard, marshaler_.NewDefaultJSONPb()), 101 ).apply( 102 c, 103 ) 104 105 WithGatewayMuxOptions( 106 runtime.WithMarshalerOption(binding.MIMEJSON, marshaler_.NewDefaultJSONPb()), 107 ).apply( 108 c, 109 ) 110 }) 111 } 112 113 // http body proto Marshal 114 func WithServerInterceptorsHttpBodyProtoOptions() GRPCGatewayOption { 115 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 116 WithGatewayMuxOptions( 117 runtime.WithMarshalerOption( 118 binding.MIMEPROTOBUF, 119 &marshaler_.ProtoMarshaller{}, 120 ), 121 ).apply( 122 c, 123 ) 124 }) 125 } 126 127 //HTTP, only called by failed response 128 func WithServerInterceptorsTCloud30HttpErrorOptions() GRPCGatewayOption { 129 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 130 WithGatewayMuxOptions(runtime.WithErrorHandler(interceptortcloud3_.HTTPError)).apply(c) 131 }) 132 } 133 134 //HTTP, only called by failed response 135 func WithServerInterceptorsTrivialV1HttpErrorOptions() GRPCGatewayOption { 136 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 137 WithGatewayMuxOptions(runtime.WithErrorHandler(interceptortrivialv1_.HTTPError)).apply(c) 138 }) 139 } 140 141 //HTTP, only called by failed response 142 func WithServerInterceptorsTrivialV2HttpErrorOptions() GRPCGatewayOption { 143 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 144 WithGatewayMuxOptions(runtime.WithErrorHandler(interceptortrivialv2_.HTTPError)).apply(c) 145 }) 146 } 147 148 // WithHttpPreHandlerInterceptorOptions 149 func WithHttpPreHandlerInterceptorOptions( 150 handlers ...func(w http.ResponseWriter, r *http.Request) error, 151 ) GRPCGatewayOption { 152 153 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 154 c.opts.interceptionOptions.httpServerOpts.handlerChain.PreHandlers = append( 155 c.opts.interceptionOptions.httpServerOpts.handlerChain.PreHandlers, 156 handlers...) 157 }) 158 } 159 160 // WithHttpHandlerInterceptorOptions 161 func WithHttpHandlerInterceptorOptions(handlers ...http_.HandlerInterceptor) GRPCGatewayOption { 162 163 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 164 if handlers != nil { 165 c.opts.interceptionOptions.httpServerOpts.handlerChain.Handlers = append( 166 c.opts.interceptionOptions.httpServerOpts.handlerChain.Handlers, 167 handlers...) 168 } 169 }) 170 } 171 172 // WithHttpPostHandlerInterceptorOptions 173 func WithHttpPostHandlerInterceptorOptions( 174 handlers ...func(w http.ResponseWriter, r *http.Request), 175 ) GRPCGatewayOption { 176 177 return GRPCGatewayOptionFunc(func(c *GRPCGateway) { 178 c.opts.interceptionOptions.httpServerOpts.handlerChain.PostHandlers = append( 179 c.opts.interceptionOptions.httpServerOpts.handlerChain.PostHandlers, 180 handlers...) 181 }) 182 } 183 184 // WithHttpHandlerInterceptorRequestIDOptions 185 func WithHttpHandlerInterceptorRequestIDOptions() GRPCGatewayOption { 186 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 187 Interceptor: httpinterceptordebug_.RequestID, 188 }) 189 } 190 191 func WithHttpHandlerInterceptorsTimerOptions() GRPCGatewayOption { 192 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 193 Interceptor: httpinterceptortimer_.ServerInterceptorOfTimer, 194 }) 195 } 196 197 func WithHttpHandlerInterceptorsMetricOptions() GRPCGatewayOption { 198 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 199 Interceptor: httpinterceptoropentelemetr_.Metric, 200 }) 201 } 202 203 func WithHttpHandlerInterceptorInOutputPrinterOptions(enabled bool) GRPCGatewayOption { 204 if enabled { 205 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 206 Interceptor: httpinterceptordebug_.InOutputPrinter, 207 }) 208 } 209 210 return WithHttpHandlerInterceptorOptions() 211 } 212 213 func WithHttpHandlerInterceptorInOutputHeaderPrinterOptions(enabled bool) GRPCGatewayOption { 214 if enabled { 215 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 216 Interceptor: httpinterceptordebug_.InOutputHeaderPrinter, 217 }) 218 } 219 return WithHttpHandlerInterceptorOptions() 220 } 221 222 // timeout 223 func WithHttpHandlerInterceptorTimeoutOptions(timeout time.Duration) GRPCGatewayOption { 224 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 225 Interceptor: httpinterceptorhttp_.Timeout(timeout), 226 }) 227 } 228 229 func WithHttpHandlerInterceptorsLimitAllOptions(burst int) GRPCGatewayOption { 230 handler := http_.HandlerInterceptor{} 231 if burst > 0 { 232 handler.Interceptor = httpinterceptorlimiter_.LimitAll(burst).Handler 233 } 234 235 return WithHttpHandlerInterceptorOptions(handler) 236 } 237 238 // CleanPath 239 func WithHttpHandlerInterceptorCleanPathOptions() GRPCGatewayOption { 240 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 241 Interceptor: httpinterceptorhttp_.CleanPath, 242 }) 243 } 244 245 // recovery 246 func WithHttpHandlerInterceptorRecoveryOptions() GRPCGatewayOption { 247 return WithHttpHandlerInterceptorOptions(http_.HandlerInterceptor{ 248 Interceptor: httpinterceptordebug_.Recovery, 249 }) 250 }