add multiple middlewares (#936)

pull/939/head
Tony Chen 4 years ago committed by GitHub
parent 248d30f068
commit ef6e52d1ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      examples/blog/internal/server/grpc.go
  2. 3
      examples/blog/internal/server/http.go
  3. 2
      examples/helloworld/client/main.go
  4. 9
      examples/helloworld/server/main.go
  5. 8
      middleware/middleware.go
  6. 6
      transport/grpc/client.go
  7. 4
      transport/grpc/server.go
  8. 4
      transport/http/client.go
  9. 4
      transport/http/handle.go

@ -5,7 +5,6 @@ import (
"github.com/go-kratos/kratos/examples/blog/internal/conf" "github.com/go-kratos/kratos/examples/blog/internal/conf"
"github.com/go-kratos/kratos/examples/blog/internal/service" "github.com/go-kratos/kratos/examples/blog/internal/service"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/logging" "github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/tracing" "github.com/go-kratos/kratos/v2/middleware/tracing"
@ -17,12 +16,10 @@ import (
func NewGRPCServer(c *conf.Server, tracer trace.TracerProvider, blog *service.BlogService) *grpc.Server { func NewGRPCServer(c *conf.Server, tracer trace.TracerProvider, blog *service.BlogService) *grpc.Server {
var opts = []grpc.ServerOption{ var opts = []grpc.ServerOption{
grpc.Middleware( grpc.Middleware(
middleware.Chain(
tracing.Server(tracing.WithTracerProvider(tracer)), tracing.Server(tracing.WithTracerProvider(tracer)),
logging.Server(log.DefaultLogger), logging.Server(log.DefaultLogger),
recovery.Recovery(), recovery.Recovery(),
), ),
),
} }
if c.Grpc.Network != "" { if c.Grpc.Network != "" {
opts = append(opts, grpc.Network(c.Grpc.Network)) opts = append(opts, grpc.Network(c.Grpc.Network))

@ -5,7 +5,6 @@ import (
"github.com/go-kratos/kratos/examples/blog/internal/conf" "github.com/go-kratos/kratos/examples/blog/internal/conf"
"github.com/go-kratos/kratos/examples/blog/internal/service" "github.com/go-kratos/kratos/examples/blog/internal/service"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/logging" "github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/tracing" "github.com/go-kratos/kratos/v2/middleware/tracing"
@ -26,11 +25,9 @@ func NewHTTPServer(c *conf.Server, tracer trace.TracerProvider, blog *service.Bl
opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration())) opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
} }
m := http.Middleware( m := http.Middleware(
middleware.Chain(
tracing.Server(tracing.WithTracerProvider(tracer)), tracing.Server(tracing.WithTracerProvider(tracer)),
logging.Server(log.DefaultLogger), logging.Server(log.DefaultLogger),
recovery.Recovery(), recovery.Recovery(),
),
) )
srv := http.NewServer(opts...) srv := http.NewServer(opts...)
srv.HandlePrefix("/", v1.NewBlogServiceHandler(blog, m)) srv.HandlePrefix("/", v1.NewBlogServiceHandler(blog, m))

@ -22,10 +22,8 @@ func callHTTP() {
client, err := transhttp.NewClient( client, err := transhttp.NewClient(
context.Background(), context.Background(),
transhttp.WithMiddleware( transhttp.WithMiddleware(
middleware.Chain(
recovery.Recovery(), recovery.Recovery(),
), ),
),
) )
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

@ -9,7 +9,6 @@ import (
"github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/logging" "github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/grpc"
@ -48,10 +47,8 @@ func main() {
grpcSrv := grpc.NewServer( grpcSrv := grpc.NewServer(
grpc.Address(":9000"), grpc.Address(":9000"),
grpc.Middleware( grpc.Middleware(
middleware.Chain(
logging.Server(logger),
recovery.Recovery(), recovery.Recovery(),
), logging.Server(logger),
)) ))
s := &server{} s := &server{}
@ -60,10 +57,8 @@ func main() {
httpSrv := http.NewServer(http.Address(":8000")) httpSrv := http.NewServer(http.Address(":8000"))
httpSrv.HandlePrefix("/", pb.NewGreeterHandler(s, httpSrv.HandlePrefix("/", pb.NewGreeterHandler(s,
http.Middleware( http.Middleware(
middleware.Chain(
logging.Server(logger),
recovery.Recovery(), recovery.Recovery(),
), logging.Server(logger),
)), )),
) )

@ -11,11 +11,11 @@ type Handler func(ctx context.Context, req interface{}) (interface{}, error)
type Middleware func(Handler) Handler type Middleware func(Handler) Handler
// Chain returns a Middleware that specifies the chained handler for endpoint. // Chain returns a Middleware that specifies the chained handler for endpoint.
func Chain(outer Middleware, others ...Middleware) Middleware { func Chain(m ...Middleware) Middleware {
return func(next Handler) Handler { return func(next Handler) Handler {
for i := len(others) - 1; i >= 0; i-- { for i := len(m) - 1; i >= 0; i-- {
next = others[i](next) next = m[i](next)
} }
return outer(next) return next
} }
} }

@ -9,6 +9,8 @@ import (
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/grpc/resolver/discovery" "github.com/go-kratos/kratos/v2/transport/grpc/resolver/discovery"
// init resolver
_ "github.com/go-kratos/kratos/v2/transport/grpc/resolver/direct" _ "github.com/go-kratos/kratos/v2/transport/grpc/resolver/direct"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -33,9 +35,9 @@ func WithTimeout(timeout time.Duration) ClientOption {
} }
// WithMiddleware with client middleware. // WithMiddleware with client middleware.
func WithMiddleware(m middleware.Middleware) ClientOption { func WithMiddleware(m ...middleware.Middleware) ClientOption {
return func(o *clientOptions) { return func(o *clientOptions) {
o.middleware = m o.middleware = middleware.Chain(m...)
} }
} }

@ -52,9 +52,9 @@ func Logger(logger log.Logger) ServerOption {
} }
// Middleware with server middleware. // Middleware with server middleware.
func Middleware(m middleware.Middleware) ServerOption { func Middleware(m ...middleware.Middleware) ServerOption {
return func(s *Server) { return func(s *Server) {
s.middleware = m s.middleware = middleware.Chain(m...)
} }
} }

@ -43,9 +43,9 @@ func WithTransport(trans http.RoundTripper) ClientOption {
} }
// WithMiddleware with client middleware. // WithMiddleware with client middleware.
func WithMiddleware(m middleware.Middleware) ClientOption { func WithMiddleware(m ...middleware.Middleware) ClientOption {
return func(o *clientOptions) { return func(o *clientOptions) {
o.middleware = m o.middleware = middleware.Chain(m...)
} }
} }

@ -67,9 +67,9 @@ func ErrorEncoder(en EncodeErrorFunc) HandleOption {
} }
// Middleware with middleware option. // Middleware with middleware option.
func Middleware(m middleware.Middleware) HandleOption { func Middleware(m ...middleware.Middleware) HandleOption {
return func(o *HandleOptions) { return func(o *HandleOptions) {
o.Middleware = m o.Middleware = middleware.Chain(m...)
} }
} }

Loading…
Cancel
Save