-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
156 lines (142 loc) · 3.57 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"os"
"time"
"github.com/DataDog/datadog-go/statsd"
"github.com/MEDIGO/laika/api"
"github.com/MEDIGO/laika/notifier"
"github.com/MEDIGO/laika/store"
log "github.com/Sirupsen/logrus"
"github.com/urfave/cli"
graceful "gopkg.in/tylerb/graceful.v1"
)
func init() {
log.SetFormatter(&log.JSONFormatter{})
}
func main() {
app := cli.NewApp()
app.Author = "MEDIGO GmbH"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "port",
Value: "8000",
Usage: "Service port",
EnvVar: "LAIKA_PORT",
},
cli.IntFlag{
Name: "timeout",
Value: 10,
Usage: "Shutdown timeout",
EnvVar: "LAIKA_TIMEOUT",
},
cli.StringFlag{
Name: "mysql-host",
Value: "db",
Usage: "MySQL host",
EnvVar: "LAIKA_MYSQL_HOST",
},
cli.StringFlag{
Name: "mysql-port",
Value: "3306",
Usage: "MySQL port",
EnvVar: "LAIKA_MYSQL_PORT",
},
cli.StringFlag{
Name: "mysql-username",
Value: "root",
Usage: "MySQL username",
EnvVar: "LAIKA_MYSQL_USERNAME",
},
cli.StringFlag{
Name: "mysql-password",
Value: "root",
Usage: "MySQL password",
EnvVar: "LAIKA_MYSQL_PASSWORD",
},
cli.StringFlag{
Name: "mysql-dbname",
Value: "laika",
Usage: "MySQL dbname",
EnvVar: "LAIKA_MYSQL_DBNAME",
},
cli.StringFlag{
Name: "statsd-host",
Value: "localhost",
Usage: "Statsd host",
EnvVar: "LAIKA_STATSD_HOST",
},
cli.StringFlag{
Name: "statsd-port",
Value: "8125",
Usage: "Statsd port",
EnvVar: "LAIKA_STATSD_PORT",
},
cli.StringFlag{
Name: "root-username",
Usage: "Root username",
Value: "root",
EnvVar: "LAIKA_ROOT_USERNAME",
},
cli.StringFlag{
Name: "root-password",
Usage: "Root password",
Value: "root",
EnvVar: "LAIKA_ROOT_PASSWORD",
},
cli.StringFlag{
Name: "slack-webhook-url",
Usage: "Slack webhook URL",
EnvVar: "LAIKA_SLACK_WEBHOOK_URL",
},
}
app.Commands = []cli.Command{
{
Name: "run",
Usage: "Runs laika's feature flag service",
Action: func(c *cli.Context) error {
store, err := store.NewMySQLStore(
c.GlobalString("mysql-username"),
c.GlobalString("mysql-password"),
c.GlobalString("mysql-host"),
c.GlobalString("mysql-port"),
c.GlobalString("mysql-dbname"),
)
if err != nil {
return fmt.Errorf("failed to create store: %s", err)
}
if err := store.Ping(); err != nil {
return fmt.Errorf("could not ping database: %s", err)
}
if err := store.Migrate(); err != nil {
return fmt.Errorf("failed to migrate store schema: %s", err)
}
if _, err := store.State(); err != nil {
return fmt.Errorf("failed to compute initial state: %s", err)
}
stats, err := statsd.New(c.GlobalString("statsd-host") + ":" + c.GlobalString("statsd-port"))
if err != nil {
return fmt.Errorf("failed to create Statsd client: %s", err)
}
notifier := notifier.NewSlackNotifier(c.GlobalString("slack-webhook-url"))
server, err := api.NewServer(api.ServerConfig{
RootUsername: c.GlobalString("root-username"),
RootPassword: c.GlobalString("root-password"),
Store: store,
Stats: stats,
Notifier: notifier,
})
if err != nil {
return fmt.Errorf("failed to create server: %s", err)
}
log.Info("Starting server on port ", c.GlobalString("port"))
log.Fatal(graceful.RunWithErr(":"+c.GlobalString("port"), time.Duration(c.Int("timeout"))*time.Second, server))
log.Info("Server exiting")
return nil
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}