grpc.go 885 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7
package server

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/transport/grpc"
kzkzzzz's avatar
kzkzzzz committed
8 9 10
	"hilo/api/service/demo"
	"hilo/app/service/demo/internal/conf"
	"hilo/app/service/demo/internal/handler"
kzkzzzz's avatar
kzkzzzz committed
11 12 13 14
	"time"
)

// NewGRPCServer new a gRPC server.
kzkzzzz's avatar
kzkzzzz committed
15
func NewGRPCServer(c *conf.Server, svc *handler.DemoHandler) *grpc.Server {
kzkzzzz's avatar
kzkzzzz committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	var opts = []grpc.ServerOption{
		grpc.Middleware(
			recovery.Recovery(),
			tracing.Server(),
			ValidateParams(),
		),
	}
	if c.Grpc.Addr != "" {
		opts = append(opts, grpc.Address(c.Grpc.Addr))
	}
	if c.Grpc.Timeout != 0 {
		opts = append(opts, grpc.Timeout(time.Duration(c.Grpc.Timeout)*time.Second))
	}

	srv := grpc.NewServer(opts...)
	demo.RegisterDemoServer(srv, svc)

	logHelper = log.NewHelper(log.GetLogger())
	return srv
}