-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (72 loc) · 2.22 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package main GRPC服务
package main
import (
"context"
"flag"
"net"
"net/http"
"github.com/axiaoxin-com/goutils"
"github.com/axiaoxin-com/grpc-tpl/pb"
"github.com/axiaoxin-com/logging"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/spf13/viper"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
grpcAddr = flag.String("grpc_addr", ":1888", "The grpc server address")
gatewayAddr = flag.String("gateway_addr", ":1999", "The grpc gateway address")
conf = flag.String("conf", "./config.toml", "The service config file")
)
func init() {
flag.Parse()
// init viper
goutils.InitViper(*conf, nil)
logging.Info(nil, "init viper config", zap.Any("config", viper.AllSettings()))
// init logging
InitLogging()
// init db
InitDB()
// init grpc
InitGrpc()
// init redis
InitRedis()
}
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// gateway
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
if err := pb.RegisterGrpcTplServiceHandlerFromEndpoint(ctx, mux, *grpcAddr, opts); err != nil {
logging.Fatal(ctx, err.Error())
}
logging.Infof(ctx, "grpc gateway listening at %v", *gatewayAddr)
go http.ListenAndServe(*gatewayAddr, mux)
// grpc
lis, err := net.Listen("tcp", *grpcAddr)
if err != nil {
logging.Fatal(ctx, err.Error())
}
logger := logging.CloneLogger("grpcserver").WithOptions(zap.AddCallerSkip(2))
server := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_zap.StreamServerInterceptor(logger),
grpc_recovery.StreamServerInterceptor(),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_zap.UnaryServerInterceptor(logger),
grpc_recovery.UnaryServerInterceptor(),
)),
)
pb.RegisterGrpcTplServiceServer(server, &implement{})
logging.Infof(ctx, "grpc server listening at %v", *grpcAddr)
if err := server.Serve(lis); err != nil {
logging.Fatal(ctx, err.Error())
}
}