examples: add examples/middleware (#1121)
* examples: add examples/middleware Co-authored-by: chenzhihui <zhihui_chen@foxmail.com>pull/1122/head
parent
c7c91edeb6
commit
725d60134c
@ -0,0 +1,69 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"log" |
||||
|
||||
"github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2" |
||||
"github.com/go-kratos/kratos/v2/middleware" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
) |
||||
|
||||
type server struct { |
||||
helloworld.UnimplementedGreeterServer |
||||
} |
||||
|
||||
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) { |
||||
return &helloworld.HelloReply{Message: "hello"}, nil |
||||
} |
||||
|
||||
func authMiddleware(handler middleware.Handler) middleware.Handler { |
||||
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { |
||||
log.Println("auth middleware in", req) |
||||
reply, err = handler(ctx, req) |
||||
fmt.Println("auth middleware out", reply) |
||||
return |
||||
} |
||||
} |
||||
|
||||
func loggingMiddleware(handler middleware.Handler) middleware.Handler { |
||||
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { |
||||
fmt.Println("logging middleware in", req) |
||||
reply, err = handler(ctx, req) |
||||
fmt.Println("logging middleware out", reply) |
||||
return |
||||
} |
||||
} |
||||
|
||||
func main() { |
||||
httpSrv := http.NewServer( |
||||
http.Address(":8000"), |
||||
http.Middleware( |
||||
loggingMiddleware, |
||||
authMiddleware, |
||||
), |
||||
) |
||||
grpcSrv := grpc.NewServer( |
||||
grpc.Address(":9000"), |
||||
grpc.Middleware( |
||||
loggingMiddleware, |
||||
authMiddleware, |
||||
), |
||||
) |
||||
s := &server{} |
||||
helloworld.RegisterGreeterServer(grpcSrv, s) |
||||
helloworld.RegisterGreeterHTTPServer(httpSrv, s) |
||||
app := kratos.New( |
||||
kratos.Name("helloworld"), |
||||
kratos.Server( |
||||
httpSrv, |
||||
grpcSrv, |
||||
), |
||||
) |
||||
if err := app.Run(); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
Loading…
Reference in new issue