/
autoconfigure.go
146 lines (131 loc) · 3.63 KB
/
autoconfigure.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grpc
import (
"net"
"google.golang.org/grpc"
"github.com/hidevopsio/hiboot/pkg/utils/str"
"google.golang.org/grpc/reflection"
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/reflector"
"fmt"
"github.com/hidevopsio/hiboot/pkg/utils/mapstruct"
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/factory/instance"
)
type grpcConfiguration struct {
app.Configuration
Properties properties `mapstructure:"grpc"`
instanceFactory *instance.InstanceFactory
}
type grpcService struct {
name string
cb interface{}
svc interface{}
}
var (
grpcServers []*grpcService
grpcClients []*grpcService
)
func RegisterServer(cb interface{}, s interface{}) {
svr := &grpcService{
cb: cb,
svc: s,
}
grpcServers = append(grpcServers, svr)
}
func RegisterClient(name string, cb interface{}, s ...interface{}) {
var svc interface{}
if s != nil && len(s) != 0 {
svc = s[0]
}
svr := &grpcService{
name: name,
cb: cb,
svc: svc,
}
grpcClients = append(grpcClients, svr)
}
func init() {
app.AutoConfiguration(new(grpcConfiguration))
}
// inject instanceFactory
func (c *grpcConfiguration) Init(instanceFactory *instance.InstanceFactory) {
c.instanceFactory = instanceFactory
}
func (c *grpcConfiguration) BuildGrpcClients() {
clientProps := c.Properties.Client
for _, cli := range grpcClients {
prop := new(client)
if err := mapstruct.Decode(prop, clientProps[cli.name]); err != nil {
break
}
host := prop.Host
if host == "" {
host = cli.name
}
address := host + ":" + prop.Port
// connect to grpc server
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
break
}
log.Infof("grpc client connected to: %v", address)
clientInstanceName := str.ToLowerCamel(cli.name)
if cli.cb != nil {
gRpcCli, err := reflector.CallFunc(cli.cb, conn)
if err == nil {
// register grpc client
c.instanceFactory.SetInstance(clientInstanceName, gRpcCli)
}
}
// register clientConn
c.instanceFactory.SetInstance(clientInstanceName + "Conn", conn)
// register client service
if cli.svc != nil {
svcName, err := reflector.GetName(cli.svc)
if err == nil {
c.instanceFactory.SetInstance(svcName, cli.svc)
}
}
}
}
func (c *grpcConfiguration) RunGrpcServers() {
// just return if grpc server is not enabled
if !c.Properties.Server.Enabled {
return
}
address := c.Properties.Server.Host + ":" + c.Properties.Server.Port
lis, err := net.Listen(c.Properties.Server.Network, address)
if err != nil {
log.Fatalf("failed to listen: %v, %v", address, err)
}
grpcServer := grpc.NewServer()
// register server
// Register reflection service on gRPC server.
for _, srv := range grpcServers {
reflector.CallFunc(srv.cb, grpcServer, srv.svc)
reflection.Register(grpcServer)
c := make(chan bool)
go func() {
c <- true
if err := grpcServer.Serve(lis); err != nil {
fmt.Errorf("failed to serve: %v", err)
}
fmt.Printf("grpc server exit\n")
}()
<- c
log.Infof("grpc server listening at: %v", address)
}
}