/
main.go
79 lines (64 loc) · 2.1 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
package main
import (
"context"
"encoding/json"
"flag"
"os"
"path/filepath"
"time"
jcr "github.com/DisposaBoy/JsonConfigReader"
"github.com/rs/zerolog"
"github.com/unit-io/unitd/broker"
"github.com/unit-io/unitd/config"
"github.com/unit-io/unitd/pkg/log"
)
func main() {
// Get the directory of the process
exe, err := os.Executable()
if err != nil {
panic(err.Error())
}
var configfile = flag.String("config", "unitd.conf", "Path to config file.")
var listenOn = flag.String("listen", "", "Override address and port to listen on for HTTP(S) clients.")
var clusterSelf = flag.String("cluster_self", "", "Override the name of the current cluster node")
var varzPath = flag.String("varz", "/varz", "Expose runtime stats at the given endpoint, e.g. /varz. Disabled if not set")
flag.Parse()
// Default level for is fatal, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
//*configfile = toAbsolutePath(rootpath, *configfile)
*configfile = filepath.Join(filepath.Dir(exe), *configfile)
log.Debug("main", "Using config from "+*configfile)
var cfg *config.Config
if file, err := os.Open(*configfile); err != nil {
log.Fatal("main", "Failed to read config file", err)
} else if err = json.NewDecoder(jcr.New(file)).Decode(&cfg); err != nil {
log.Fatal("main", "Failed to parse config file", err)
}
zerolog.DurationFieldUnit = time.Nanosecond
if cfg.LoggingLevel != "" {
l := log.ParseLevel(cfg.LoggingLevel, zerolog.InfoLevel)
zerolog.SetGlobalLevel(l)
}
if *listenOn != "" {
cfg.Listen = *listenOn
}
if *varzPath != "" {
cfg.VarzPath = *varzPath
}
// Initialize cluster and receive calculated workerId.
// Cluster won't be started here yet.
broker.ClusterInit(cfg.Cluster, clusterSelf)
broker.Globals.ConnCache = broker.NewConnCache()
svc, err := broker.NewService(context.Background(), cfg)
if err != nil {
panic(err.Error())
}
// Start accepting cluster traffic.
if broker.Globals.Cluster != nil {
broker.Globals.Cluster.Start()
}
broker.Globals.Service = svc
//Listen and serve
svc.Listen()
log.Info("main", "Service is running at port "+cfg.Listen)
}