/
grpc.go
51 lines (47 loc) · 1.65 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package server
import (
"time"
appv1alpha1 "github.com/f-rambo/ocean/api/app/v1alpha1"
clusterv1alpha1 "github.com/f-rambo/ocean/api/cluster/v1alpha1"
projectv1alpha1 "github.com/f-rambo/ocean/api/project/v1alpha1"
servicev1alpha1 "github.com/f-rambo/ocean/api/service/v1alpha1"
userv1alpha1 "github.com/f-rambo/ocean/api/user/v1alpha1"
"github.com/f-rambo/ocean/internal/conf"
"github.com/f-rambo/ocean/internal/interfaces"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/go-kratos/kratos/v2/transport/grpc"
)
// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Bootstrap,
cluster *interfaces.ClusterInterface,
app *interfaces.AppInterface,
services *interfaces.ServicesInterface,
user *interfaces.UserInterface,
project *interfaces.ProjectInterface,
logger log.Logger) *grpc.Server {
var opts = []grpc.ServerOption{
grpc.Middleware(
selector.Server(NewAuthServer(user)).Match(NewWhiteListMatcher()).Build(),
recovery.Recovery(),
),
grpc.Timeout(10 * time.Minute),
}
cserver := c.GetOceanServer()
netWork := cserver.GRPC.GetNetwork()
if netWork != "" {
opts = append(opts, grpc.Network(netWork))
}
addr := cserver.GRPC.GetAddr()
if addr != "" {
opts = append(opts, grpc.Address(addr))
}
srv := grpc.NewServer(opts...)
clusterv1alpha1.RegisterClusterInterfaceServer(srv, cluster)
appv1alpha1.RegisterAppInterfaceServer(srv, app)
servicev1alpha1.RegisterServiceInterfaceServer(srv, services)
userv1alpha1.RegisterUserInterfaceServer(srv, user)
projectv1alpha1.RegisterProjectServiceServer(srv, project)
return srv
}