This repository was archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (83 loc) · 2.7 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
package main;
import (
"time"
"github.com/rcrowley/go-metrics"
"github.com/vrischmann/go-metrics-influxdb"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/bugsnag/bugsnag-go"
"strconv"
"log"
"os"
"github.com/caarlos0/env"
)
type ConfigS struct {
Bind string `env:"BIND"`
ApiPort int `env:"PORT" envDefault:"8080"`
LFPort int `env:"LEGACY_PORT" envDefault:"8000"`
RedisUrl string `env:"REDIS_URL" envDefault:"redis://localhost:6379/"`
RedisCluster bool `env:"REDIS_CLUSTER"`
WanAddr string `env:"WAN_ADDR"`
BugsnagApiKey string `env:"BUGSNAG_API_KEY"`
}
var Config = &ConfigS{}
func (c *ConfigS) ApiAddr() string {
return c.Bind + ":" + strconv.Itoa(c.ApiPort);
}
func (c *ConfigS) LFAddr() string {
return c.Bind + ":" + strconv.Itoa(c.LFPort);
}
func getWanAddr() {
sess, err := session.NewSession()
if err != nil {
panic(err)
}
if len(Config.WanAddr) < 1 {
if len(Config.Bind) > 0 {
Config.WanAddr = Config.ApiAddr();
} else {
log.Println("resolving bind address through ec2 api");
e2m := ec2metadata.New(sess)
ec2id,err := e2m.GetInstanceIdentityDocument();
if err != nil {
log.Println("GetInstanceIdentityDocument failed. Specify BIND= env if not on aws!")
log.Panic(err)
return
}
Config.WanAddr = ec2id.PrivateIP + Config.ApiAddr()
}
}
log.Println("registry wan", Config.WanAddr);
}
func main() {
err := env.Parse(Config)
if (err != nil) {
log.Panic(err);
}
getWanAddr();
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
bugsnag.Configure(bugsnag.Configuration{
APIKey: Config.BugsnagApiKey,
})
reg := metrics.NewPrefixedChildRegistry(metrics.DefaultRegistry, "lifeline.");
metrics.RegisterDebugGCStats(reg)
metrics.RegisterRuntimeMemStats(reg)
go metrics.CaptureDebugGCStats(reg, time.Second*5)
go metrics.CaptureRuntimeMemStats(reg, time.Second*5)
go influxdb.InfluxDBWithTags(
metrics.DefaultRegistry, // metrics registry
time.Second * 10, // interval
"http://influxdb-monitor.superscale.io:8086/", // the InfluxDB url
"monitor", // your InfluxDB database
"myuser", // your InfluxDB user
"mypassword", // your InfluxDB password
map[string]string{"host": hostname},
)
lifelines = make(map[string]*Lifeline)
initRegistry();
go LifeLineServer()
ApiServer()
}