-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (69 loc) · 1.87 KB
/
Copy pathmain.go
File metadata and controls
84 lines (69 loc) · 1.87 KB
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
package gateway
import (
"context"
"github.com/pkg/errors"
gateway "github.com/threefoldtech/zos_base/pkg/gateway_light"
"github.com/threefoldtech/zos_base/pkg/utils"
"github.com/urfave/cli/v2"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zbus"
)
const (
module = "gateway"
)
// Module is entry point for module
var Module cli.Command = cli.Command{
Name: "gateway",
Usage: "manage web gateway proxy",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "root",
Usage: "`ROOT` working directory of the module",
Value: "/var/cache/modules/gateway",
},
&cli.StringFlag{
Name: "broker",
Usage: "connection string to the message `BROKER`",
Value: "unix:///var/run/redis.sock",
},
&cli.UintFlag{
Name: "workers",
Usage: "number of workers `N`",
Value: 1,
},
},
Action: action,
}
func action(cli *cli.Context) error {
var (
moduleRoot string = cli.String("root")
msgBrokerCon string = cli.String("broker")
workerNr uint = cli.Uint("workers")
)
server, err := zbus.NewRedisServer(module, msgBrokerCon, workerNr)
if err != nil {
return errors.Wrap(err, "fail to connect to message broker server")
}
client, err := zbus.NewRedisClient(msgBrokerCon)
if err != nil {
return errors.Wrap(err, "failed to connect to zbus broker")
}
mod, err := gateway.New(cli.Context, client, moduleRoot)
if err != nil {
return errors.Wrap(err, "failed to construct gateway object")
}
server.Register(zbus.ObjectID{Name: "manager", Version: "0.0.1"}, mod)
ctx, cancel := utils.WithSignal(context.Background())
defer cancel()
log.Info().
Str("broker", msgBrokerCon).
Uint("worker nr", workerNr).
Msg("starting gateway module")
utils.OnDone(ctx, func(_ error) {
log.Info().Msg("shutting down")
})
if err := server.Run(ctx); err != nil && err != context.Canceled {
return errors.Wrap(err, "unexpected error")
}
return nil
}