-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
105 lines (96 loc) · 2.71 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
package main
import (
"fmt"
"os"
"time"
"github.com/pkg/errors"
"github.com/rancher/channelserver/pkg/config"
"github.com/rancher/channelserver/pkg/server"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
Version = "v0.0.0-dev"
GitCommit = "HEAD"
URLs = cli.NewStringSlice("channels.yaml")
RefreshInterval string
ListenAddress string
SubKeys cli.StringSlice
ChannelServerVersion string
PathPrefix cli.StringSlice
AppName string
)
func main() {
app := cli.NewApp()
app.Name = "Channel Server"
app.Version = fmt.Sprintf("%s (%s)", Version, GitCommit)
app.Flags = []cli.Flag{
&cli.StringSliceFlag{
Name: "url",
EnvVars: []string{"URL"},
Value: URLs,
},
&cli.StringSliceFlag{
Name: "config-key",
EnvVars: []string{"SUBKEY"},
Destination: &SubKeys,
},
&cli.StringFlag{
Name: "refresh-interval",
EnvVars: []string{"REFRESH_INTERVAL"},
Value: "15m",
Destination: &RefreshInterval,
},
&cli.StringFlag{
Name: "listen-address",
EnvVars: []string{"LISTEN_ADDRESS"},
Value: "0.0.0.0:8080",
Destination: &ListenAddress,
},
&cli.StringFlag{
Name: "channel-server-version",
EnvVars: []string{"CHANNEL_SERVER_VERSION"},
Destination: &ChannelServerVersion,
},
&cli.StringFlag{
Name: "app-name",
Usage: "the app for which to retrieve the app default versions",
EnvVars: []string{"APP_NAME"},
Destination: &AppName,
},
&cli.StringSliceFlag{
Name: "path-prefix",
EnvVars: []string{"PATH_PREFIX"},
Value: cli.NewStringSlice("v1-release"),
Destination: &PathPrefix,
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func run(c *cli.Context) error {
logrus.SetOutput(os.Stderr)
ctx := signals.SetupSignalContext()
intval, err := time.ParseDuration(RefreshInterval)
if err != nil {
return errors.Wrapf(err, "failed to parse %s", RefreshInterval)
}
if len(SubKeys.Value()) != len(PathPrefix.Value()) {
return errors.Errorf("keys-prefix lengths are not equal %s %s %s ", PathPrefix.Value(), SubKeys.Value(), ListenAddress)
}
var (
configs = map[string]*config.Config{}
sources []config.Source
)
for _, url := range URLs.Value() {
sources = append(sources, config.StringSource(url))
}
for index, subkey := range SubKeys.Value() {
config := config.NewConfig(ctx, subkey, &config.DurationWait{Duration: intval}, ChannelServerVersion, AppName, sources)
configs[PathPrefix.Value()[index]] = config
}
return server.ListenAndServe(ctx, ListenAddress, configs)
}