-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
80 lines (73 loc) · 1.9 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
package main
import (
"crypto/tls"
"flag"
"github.com/Sirupsen/logrus"
"github.com/kelproject/router/datasources"
"github.com/kelproject/router/server"
"golang.org/x/net/context"
)
var (
flagDataStore = flag.String("data-store", "kubernetes", "data store")
flagTLSCert = flag.String("tls-cert", "", "TLS fallback certificate")
flagTLSKey = flag.String("tls-key", "", "TLS fallback key")
flagLogLevel = flag.String("log-level", "warn", "log level")
logger = logrus.New()
)
func main() {
flag.Parse()
logLevel, err := logrus.ParseLevel(*flagLogLevel)
if err != nil {
logger.Fatalln(err)
}
logger.Level = logLevel
var tlsConfig *tls.Config
if *flagTLSCert != "" && *flagTLSKey != "" {
cert, err := tls.LoadX509KeyPair(*flagTLSCert, *flagTLSKey)
if err != nil {
logger.Fatal(err)
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
s := server.NewServer(&server.Server{
Logger: logger,
DefaultTLSConfig: tlsConfig,
})
if err := s.Listen(); err != nil {
logger.Fatal(err)
}
ctx := context.Background()
ctx, stopSync := context.WithCancel(ctx)
switch *flagDataStore {
case "file":
startFileDataStore(ctx, logger, s)
break
case "kubernetes":
startKubernetesDataStore(ctx, logger, s)
break
default:
logger.Fatalf("unknown data store %q", *flagDataStore)
}
if err := s.Serve(); err != nil {
stopSync()
logger.Fatal(err)
}
}
func startFileDataStore(ctx context.Context, logger *logrus.Logger, sh datasources.SyncHandler) {
ds, err := datasources.NewFileDataStore()
if err != nil {
logger.Fatal(err)
}
logger.Println("Starting file watcher")
go ds.Watch(ctx, sh)
}
func startKubernetesDataStore(ctx context.Context, logger *logrus.Logger, sh datasources.SyncHandler) {
ds, err := datasources.NewKubernetesDataStore()
if err != nil {
logger.Fatal(err)
}
logger.Println("Starting kubernetes watcher")
go ds.Watch(ctx, sh)
}