-
Notifications
You must be signed in to change notification settings - Fork 87
/
main.go
83 lines (69 loc) · 1.97 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"context"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
dhcplb "github.com/facebookincubator/dhcplb/lib"
"github.com/golang/glog"
)
// Program parameters
var (
version = flag.Int("version", 4, "Run in v4/v6 mode")
configPath = flag.String("config", "", "Path to JSON config file")
overridesPath = flag.String("overrides", "", "Path to JSON overrides file")
pprofPort = flag.Int("pprof", 0, "Port to run pprof HTTP server on")
serverMode = flag.Bool("server", false, "Run in server mode. The default is relay mode.")
)
func main() {
flag.Parse()
flag.Lookup("logtostderr").Value.Set("true")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if *configPath == "" {
glog.Fatal("Config file is necessary")
}
if *pprofPort != 0 {
go func() {
glog.Infof("Started pprof server on port %d", *pprofPort)
err := http.ListenAndServe(fmt.Sprintf(":%d", *pprofPort), nil)
if err != nil {
glog.Fatal("Error starting pprof server: ", err)
}
}()
}
logger := NewGlogLogger()
// load initial config
provider := NewDefaultConfigProvider()
config, err := dhcplb.LoadConfig(
*configPath, *overridesPath, *version, provider)
if err != nil {
glog.Fatalf("Failed to load config: %s", err)
}
// start watching config
configChan, err := dhcplb.WatchConfig(
*configPath, *overridesPath, *version, provider)
if err != nil {
glog.Fatalf("Failed to watch config: %s", err)
}
server, err := dhcplb.NewServer(config, *serverMode, logger)
if err != nil {
glog.Fatal(err)
}
// update server config whenever file changes
go func() {
for config := range configChan {
glog.Info("Config changed")
server.SetConfig(config)
}
}()
glog.Infof("Starting dhcplb in v%d mode", *version)
glog.Fatal(server.ListenAndServe(ctx))
}