Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
// Rpc is the gRPC rpc method, with format of package.service/method
Rpc string
}

// upstream is the configuration for upstream.
upstream struct {
// Grpc is the target of upstream.
Expand Down
23 changes: 11 additions & 12 deletions gateway/requestparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import (
"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) {
vars := pathvar.Vars(r)
if len(vars) == 0 {
return grpcurl.NewJSONRequestParser(r.Body, resolver), nil
}

if r.ContentLength == 0 {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(vars); err != nil {
return nil, err
}

return grpcurl.NewJSONRequestParser(&buf, resolver), nil
return buildJsonRequestParser(vars, resolver)
}

m := make(map[string]interface{})
Expand All @@ -34,10 +38,5 @@ func newRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.Req
m[k] = v
}

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(m); err != nil {
return nil, err
}

return grpcurl.NewJSONRequestParser(&buf, resolver), nil
return buildJsonRequestParser(m, resolver)
}
10 changes: 5 additions & 5 deletions gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (

// Server is a gateway server.
type Server struct {
svr *rest.Server
*rest.Server
upstreams []upstream
timeout time.Duration
}

// MustNewServer creates a new gateway server.
func MustNewServer(c GatewayConf) *Server {
return &Server{
svr: rest.MustNewServer(c.RestConf),
Server: rest.MustNewServer(c.RestConf),
upstreams: c.Upstreams,
timeout: c.Timeout,
}
Expand All @@ -36,12 +36,12 @@ func MustNewServer(c GatewayConf) *Server {
// Start starts the gateway server.
func (s *Server) Start() {
logx.Must(s.build())
s.svr.Start()
s.Server.Start()
}

// Stop stops the gateway server.
func (s *Server) Stop() {
s.svr.Stop()
s.Server.Stop()
}

func (s *Server) build() error {
Expand Down Expand Up @@ -69,7 +69,7 @@ func (s *Server) build() error {
}, func(pipe <-chan interface{}, cancel func(error)) {
for item := range pipe {
route := item.(rest.Route)
s.svr.AddRoute(route)
s.Server.AddRoute(route)
}
})
}
Expand Down