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. 9
      examples/blog/internal/server/grpc.go
  2. 9
      examples/blog/internal/server/http.go
  3. 4
      examples/helloworld/client/main.go
  4. 13
      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/service"
"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/recovery"
"github.com/go-kratos/kratos/v2/middleware/tracing"
@ -17,11 +16,9 @@ import (
func NewGRPCServer(c *conf.Server, tracer trace.TracerProvider, blog *service.BlogService) *grpc.Server {
var opts = []grpc.ServerOption{
grpc.Middleware(
middleware.Chain(
tracing.Server(tracing.WithTracerProvider(tracer)),
logging.Server(log.DefaultLogger),
recovery.Recovery(),
),
tracing.Server(tracing.WithTracerProvider(tracer)),
logging.Server(log.DefaultLogger),
recovery.Recovery(),
),
}
if 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/service"
"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/recovery"
"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()))
}
m := http.Middleware(
middleware.Chain(
tracing.Server(tracing.WithTracerProvider(tracer)),
logging.Server(log.DefaultLogger),
recovery.Recovery(),
),
tracing.Server(tracing.WithTracerProvider(tracer)),
logging.Server(log.DefaultLogger),
recovery.Recovery(),
)
srv := http.NewServer(opts...)
srv.HandlePrefix("/", v1.NewBlogServiceHandler(blog, m))

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

@ -9,7 +9,6 @@ import (
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/errors"
"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/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
@ -48,10 +47,8 @@ func main() {
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
middleware.Chain(
logging.Server(logger),
recovery.Recovery(),
),
recovery.Recovery(),
logging.Server(logger),
))
s := &server{}
@ -60,10 +57,8 @@ func main() {
httpSrv := http.NewServer(http.Address(":8000"))
httpSrv.HandlePrefix("/", pb.NewGreeterHandler(s,
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
// 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 {
for i := len(others) - 1; i >= 0; i-- {
next = others[i](next)
for i := len(m) - 1; i >= 0; i-- {
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/transport"
"github.com/go-kratos/kratos/v2/transport/grpc/resolver/discovery"
// init resolver
_ "github.com/go-kratos/kratos/v2/transport/grpc/resolver/direct"
"google.golang.org/grpc"
@ -33,9 +35,9 @@ func WithTimeout(timeout time.Duration) ClientOption {
}
// WithMiddleware with client middleware.
func WithMiddleware(m middleware.Middleware) ClientOption {
func WithMiddleware(m ...middleware.Middleware) ClientOption {
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.
func Middleware(m middleware.Middleware) ServerOption {
func Middleware(m ...middleware.Middleware) ServerOption {
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.
func WithMiddleware(m middleware.Middleware) ClientOption {
func WithMiddleware(m ...middleware.Middleware) ClientOption {
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.
func Middleware(m middleware.Middleware) HandleOption {
func Middleware(m ...middleware.Middleware) HandleOption {
return func(o *HandleOptions) {
o.Middleware = m
o.Middleware = middleware.Chain(m...)
}
}

Loading…
Cancel
Save