-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
138 lines (120 loc) · 3.53 KB
/
gateway.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
package gateway_module
import (
"github.com/go-playground/validator/v10"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
"go.uber.org/fx"
"google.golang.org/protobuf/encoding/protojson"
"pkg.lucas.icu/micro/cfg_module"
)
type Config struct {
UseProtoNames bool `mapstructure:"use-proto-names"`
EmitUnpopulated bool `mapstructure:"emit-unpopulated"`
DiscardUnknown bool `mapstructure:"discard-unknown"`
}
var DefaultConfig = wrappedCfg{
GW: Config{
UseProtoNames: true,
EmitUnpopulated: true,
DiscardUnknown: true,
},
}
type wrappedCfg struct {
GW Config `mapstructure:"gateway"`
}
func ReadConfig(v *viper.Viper) (Config, error) {
cfg := &wrappedCfg{}
if err := v.Unmarshal(&cfg); err != nil {
return Config{}, err
}
return cfg.GW, nil
}
func CheckConfig(cfg Config) error {
return validator.New().Struct(&cfg)
}
func Module() fx.Option {
return fx.Options(
cfg_module.SetDefaultConfig(DefaultConfig),
fx.Provide(
ReadConfig,
NewGatewayMux,
),
fx.Invoke(
RegisterGateway,
),
)
}
func customMatcher(key string) (string, bool) {
switch key {
case "x-request-id":
return key, true
default:
return runtime.DefaultHeaderMatcher(key)
}
}
type runtimeOptionsParams struct {
fx.In
Options []runtime.ServeMuxOption `group:"grpc_gateway_options"`
}
type runtimeOptions struct {
fx.Out
Options []runtime.ServeMuxOption `group:"grpc_gateway_options,flatten"`
}
func WithGWOptions(opts ...runtime.ServeMuxOption) fx.Option {
return fx.Supply(runtimeOptions{Options: opts})
}
func NewGatewayMux(cfg Config, opts runtimeOptionsParams) *runtime.ServeMux {
// f := func(err error) (codes.Code, proto.Message) {
// pb := errorpb.MustFromError(err)
// return errorpb.Code(pb), pb
// }
// statusToErr := func(httpStatus int) error {
// err := errorpb.New(codes.Internal, errorpb.ID_INTERNAL).WithReason("Unexpected routing error")
// switch httpStatus {
// case http.StatusBadRequest:
// err.WithCode(codes.InvalidArgument).WithReason(http.StatusText(httpStatus))
// case http.StatusMethodNotAllowed:
// err.WithCode(codes.Unimplemented).WithReason(http.StatusText(httpStatus))
// case http.StatusNotFound:
// err.WithCode(codes.NotFound).WithReason(http.StatusText(httpStatus))
// }
// return err
// }
gwopts := []runtime.ServeMuxOption{
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: cfg.UseProtoNames,
EmitUnpopulated: cfg.EmitUnpopulated,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: cfg.DiscardUnknown,
},
},
}),
runtime.WithIncomingHeaderMatcher(customMatcher),
}
gwopts = append(gwopts, opts.Options...)
gwmux := runtime.NewServeMux(
gwopts...,
// runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
// Marshaler: &runtime.JSONPb{
// MarshalOptions: protojson.MarshalOptions{
// UseProtoNames: cfg.UseProtoNames,
// EmitUnpopulated: cfg.EmitUnpopulated,
// },
// UnmarshalOptions: protojson.UnmarshalOptions{
// DiscardUnknown: cfg.DiscardUnknown,
// },
// },
// }),
// runtime.WithErrorHandler(gateway_middleware.NewHTTPErrorHandler(f)),
// runtime.WithRoutingErrorHandler(gateway_middleware.NewRoutingErrorHandler(statusToErr, f)),
// runtime.WithIncomingHeaderMatcher(customMatcher),
)
return gwmux
}
func RegisterGateway(e *echo.Echo, gwmux *runtime.ServeMux) {
e.Any("/*", echo.WrapHandler(gwmux))
}