You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
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)
|
|
}
|
|
}
|
|
|