-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (79 loc) · 1.83 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"net"
"os"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/vinyl-linux/vin/server"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
var (
logger *zap.Logger
sugar *zap.SugaredLogger
)
func init() {
var err error
if logger == nil {
logger, err = zap.NewProduction()
if err != nil {
panic(err)
}
}
sugar = logger.Sugar()
}
func main() {
defer os.Remove(sockAddr)
os.Remove(sockAddr) //#nosec: G104
lis, err := net.Listen("unix", sockAddr)
if err != nil {
sugar.Panic(err)
}
sugar.Panic(Setup().Serve(lis))
}
// Setup configures grpc servers, handles startup conditions, and returns
// a grpc server.
//
// This function cynically exists as a way to avoid dropping code coverage
func Setup() *grpc.Server {
sugar.Info("starting")
sugar.Info("loading config")
err := loadConfig()
if err != nil {
sugar.Panic(err)
}
sugar.Info("loaded")
sugar.Info("loading manifests")
mdb, err := LoadDB()
if err != nil {
sugar.Panic(err)
}
sugar.Info("loaded")
sugar.Info("loading state")
sdb, err := LoadStateDB()
if err != nil {
sugar.Panic(err)
}
sugar.Info("loaded")
sugar.Info("starting server")
s, err := NewServer(mdb, sdb)
if err != nil {
sugar.Panic(err)
}
grpcServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.StreamServerInterceptor(logger),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_zap.UnaryServerInterceptor(logger),
)),
)
server.RegisterVinServer(grpcServer, s)
reflection.Register(grpcServer)
sugar.Info("serving vin server :-)")
return grpcServer
}