feat: export gateway.Server to let users add middlewares (#2157)

master
Kevin Wan 2 years ago committed by GitHub
parent 4d7dae9cea
commit 453fa309b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,6 +24,7 @@ type (
// Rpc is the gRPC rpc method, with format of package.service/method // Rpc is the gRPC rpc method, with format of package.service/method
Rpc string Rpc string
} }
// upstream is the configuration for upstream. // upstream is the configuration for upstream.
upstream struct { upstream struct {
// Grpc is the target of upstream. // Grpc is the target of upstream.

@ -10,6 +10,15 @@ import (
"github.com/zeromicro/go-zero/rest/pathvar" "github.com/zeromicro/go-zero/rest/pathvar"
) )
func buildJsonRequestParser(v interface{}, resolver jsonpb.AnyResolver) (grpcurl.RequestParser, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
return grpcurl.NewJSONRequestParser(&buf, resolver), nil
}
func newRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.RequestParser, error) { func newRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.RequestParser, error) {
vars := pathvar.Vars(r) vars := pathvar.Vars(r)
if len(vars) == 0 { if len(vars) == 0 {
@ -17,12 +26,7 @@ func newRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.Req
} }
if r.ContentLength == 0 { if r.ContentLength == 0 {
var buf bytes.Buffer return buildJsonRequestParser(vars, resolver)
if err := json.NewEncoder(&buf).Encode(vars); err != nil {
return nil, err
}
return grpcurl.NewJSONRequestParser(&buf, resolver), nil
} }
m := make(map[string]interface{}) m := make(map[string]interface{})
@ -34,10 +38,5 @@ func newRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.Req
m[k] = v m[k] = v
} }
var buf bytes.Buffer return buildJsonRequestParser(m, resolver)
if err := json.NewEncoder(&buf).Encode(m); err != nil {
return nil, err
}
return grpcurl.NewJSONRequestParser(&buf, resolver), nil
} }

@ -19,7 +19,7 @@ import (
// Server is a gateway server. // Server is a gateway server.
type Server struct { type Server struct {
svr *rest.Server *rest.Server
upstreams []upstream upstreams []upstream
timeout time.Duration timeout time.Duration
} }
@ -27,7 +27,7 @@ type Server struct {
// MustNewServer creates a new gateway server. // MustNewServer creates a new gateway server.
func MustNewServer(c GatewayConf) *Server { func MustNewServer(c GatewayConf) *Server {
return &Server{ return &Server{
svr: rest.MustNewServer(c.RestConf), Server: rest.MustNewServer(c.RestConf),
upstreams: c.Upstreams, upstreams: c.Upstreams,
timeout: c.Timeout, timeout: c.Timeout,
} }
@ -36,12 +36,12 @@ func MustNewServer(c GatewayConf) *Server {
// Start starts the gateway server. // Start starts the gateway server.
func (s *Server) Start() { func (s *Server) Start() {
logx.Must(s.build()) logx.Must(s.build())
s.svr.Start() s.Server.Start()
} }
// Stop stops the gateway server. // Stop stops the gateway server.
func (s *Server) Stop() { func (s *Server) Stop() {
s.svr.Stop() s.Server.Stop()
} }
func (s *Server) build() error { func (s *Server) build() error {
@ -69,7 +69,7 @@ func (s *Server) build() error {
}, func(pipe <-chan interface{}, cancel func(error)) { }, func(pipe <-chan interface{}, cancel func(error)) {
for item := range pipe { for item := range pipe {
route := item.(rest.Route) route := item.(rest.Route)
s.svr.AddRoute(route) s.Server.AddRoute(route)
} }
}) })
} }

Loading…
Cancel
Save