-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
65 lines (57 loc) · 1.56 KB
/
main.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
package main
import (
"context"
"fmt"
"github.com/spf13/pflag"
"github.com/webws/go-moda/config"
"github.com/webws/go-moda/logger"
modagrpc "github.com/webws/go-moda/transport/grpc"
app "github.com/webws/go-moda"
pbexample "github.com/webws/go-moda/example/pb/example"
)
var (
ServerName string
AppVersion string
ConfFilePath string
)
type Config struct {
HttpAddr string `json:"http_addr" toml:"http_addr"`
GrpcAddr string `json:"grpc_addr" toml:"grpc_addr"`
}
var csFlag = pflag.StringP("cs", "s", "client", "client or server")
func main() {
// load config
conf := &Config{}
if err := config.NewConfigWithFile("./conf.toml").Load(conf); err != nil {
logger.Fatalw("NewConfigWithFile fail", "err", err)
}
// 通过csFlag判断是启动服务端还是客户端
if *csFlag == "server" {
gs := modagrpc.NewServer(
modagrpc.WithServerAddress(conf.GrpcAddr),
)
pbexample.RegisterExampleServiceServer(gs, &ExampleServer{})
a := app.New(app.Server(gs))
if err := a.Run(); err != nil {
panic(err)
}
} else {
ClientGrpcSend(conf.GrpcAddr)
}
}
func ClientGrpcSend(addr string) {
// 连接服务器
conn, err := modagrpc.Dial(context.Background(), addr, modagrpc.WithDialWithInsecure(true), modagrpc.WithDialTracing(true))
if err != nil {
panic(err)
}
defer conn.Close()
// 创建一个grpc客户端
client := pbexample.NewExampleServiceClient(conn)
// 调用服务端的 SayHello 方法
resp, err := client.SayHello(context.Background(), &pbexample.HelloRequest{Name: "gRPC"})
if err != nil {
panic(err)
}
fmt.Println(resp.Message)
}