-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
164 lines (124 loc) · 3.21 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
// Services
service "github.com/edobtc/cloudkit/controlplane/grpc"
"github.com/edobtc/cloudkit/controlplane/ws"
pb "github.com/edobtc/cloudkit/rpc/controlplane/resources/v1"
api "github.com/edobtc/cloudkit/rpc/gateway/controlplane/resources/v1"
"github.com/edobtc/cloudkit/config"
"github.com/edobtc/cloudkit/controlplane"
"github.com/edobtc/cloudkit/http/middleware/auth/key"
"github.com/edobtc/cloudkit/http/middleware/namespace"
"github.com/edobtc/cloudkit/logging"
cloudkitRuntime "github.com/edobtc/cloudkit/runtime"
"github.com/edobtc/cloudkit/server"
)
func init() {
logging.Setup()
}
var rootCmd = &cobra.Command{
Use: "control-plane",
Short: "bck is the main command to launch the notification server",
Run: func(cmd *cobra.Command, args []string) {
go func() {
serveGRPC()
}()
<-serveHTTP()
},
}
func serveHTTP() chan bool {
srv := server.NewServer()
r := srv.Router()
// middleware attachments
r.Use(namespace.DecorateNamespace) // namespace middleware
r.Use(ws.InjectConnectionPool) // maintenance of websocket connections
// route bindings
controlplane.BindRoutes(r)
// optional API key enabled
if config.Read().EnableApiKey {
log.Info("enabling api key")
r.Use(key.ApiKey)
}
log.Info("starting control plane")
signal.Notify(srv.Close, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
s := <-srv.Close
log.Infof("received signal %v", s)
srv.GracefulShutdown()
}()
return srv.Start()
}
func serveGRPCGateway() chan error {
await := make(chan error)
listen := config.Read().GRPCGatewayListen
mux := runtime.NewServeMux()
proxyOpts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
err := api.RegisterResourcesHandlerFromEndpoint(
context.Background(),
mux,
config.Read().GRPCListen,
proxyOpts,
)
if err != nil {
panic(err)
}
go func() {
log.Infof("grpc gateway starting and listening on %s", listen)
await <- http.ListenAndServe(listen, mux)
}()
return await
}
func serveGRPC() chan bool {
listen := config.Read().GRPCListen
lis, err := net.Listen("tcp", listen)
if err != nil {
panic("failed to listen")
}
r := cloudkitRuntime.New(context.TODO())
go func(rt *cloudkitRuntime.Runtime) {
log.Debug("starting runtime")
r.Run()
}(r)
srv := service.NewResourcesServiceServer()
signal.Notify(srv.Close, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
grpcServer := grpc.NewServer()
pb.RegisterResourcesServer(grpcServer, srv)
if config.Read().GRPCGatewayEnabled {
go func() {
log.Infof("grpc starting and listening on %s", listen)
<-serveGRPCGateway()
}()
}
go func() {
s := <-srv.Close
logrus.Infof("got signal %v, attempting graceful shutdown", s)
grpcServer.GracefulStop()
}()
go func() {
err = grpcServer.Serve(lis)
if err != nil {
panic(err)
}
}()
return make(chan bool)
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Error(err)
os.Exit(1)
}
}