-
Notifications
You must be signed in to change notification settings - Fork 46
/
run.go
66 lines (55 loc) · 1.59 KB
/
run.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
package main
import (
"context"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/wrfly/ecp"
"github.com/wrfly/container-web-tty/config"
"github.com/wrfly/container-web-tty/container"
"github.com/wrfly/container-web-tty/proxy"
"github.com/wrfly/container-web-tty/route"
"github.com/wrfly/container-web-tty/util"
)
func run(c *cli.Context, conf config.Config) {
srvOptions := conf.Server
if len(conf.Backend.GRPC.Servers) > 0 {
srvOptions.ShowLocation = true
}
if err := ecp.Parse(&srvOptions); err != nil {
logrus.Fatal(err)
}
if srvOptions.GrpcPort <= 0 && srvOptions.Port <= 0 {
logrus.Fatal("bad config, no port listenning")
}
containerCli, err := container.NewCliBackend(conf.Backend)
if err != nil {
logrus.Fatalf("Create backend client error: %s", err)
}
defer containerCli.Close()
ctx, cancel := context.WithCancel(context.Background())
gCtx, gCancel := context.WithCancel(ctx)
errs := make(chan error, 2)
// run HTTP server if port > 0
if srvOptions.Port > 0 {
go func() {
srv, err := route.New(containerCli, srvOptions)
if err != nil {
logrus.Fatalf("Create server error: %s", err)
}
errs <- srv.Run(ctx, route.WithGracefullContext(gCtx))
}()
}
// run grpc server if grpc-port > 0
if srvOptions.GrpcPort > 0 {
go func() {
grpcServer := proxy.New(conf.Backend.GRPC.Auth,
srvOptions.GrpcPort, containerCli)
errs <- grpcServer.Run(ctx, gCtx)
}()
}
err = util.WaitSignals(errs, cancel, gCancel)
if err != nil && err != context.Canceled {
logrus.Fatalf("Server closed with error: %s", err)
}
logrus.Info("Server closed")
}