forked from gitter-badger/api-3
/
main.go
178 lines (152 loc) · 5.8 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"net"
"net/http"
"os"
"github.com/Sirupsen/logrus"
"github.com/lavab/flag"
"github.com/zenazn/goji/graceful"
"github.com/lavab/api/env"
"github.com/lavab/api/setup"
)
// TODO: "Middleware that implements a few quick security wins"
// https://github.com/unrolled/secure
var (
// Enable namsral/flag functionality
configFlag = flag.String("config", "", "config file to load")
// General flags
bindAddress = flag.String("bind", ":5000", "Network address used to bind")
apiVersion = flag.String("api_version", "v0", "Shown API version")
logFormatterType = flag.String("log", "text", "Log formatter type. Either \"json\" or \"text\"")
forceColors = flag.Bool("force_colors", false, "Force colored prompt?")
emailDomain = flag.String("email_domain", "lavaboom.io", "Domain of the default email service")
// Registration settings
sessionDuration = flag.Int("session_duration", 72, "Session duration expressed in hours")
// Cache-related flags
redisAddress = flag.String("redis_address", func() string {
address := os.Getenv("REDIS_PORT_6379_TCP_ADDR")
if address == "" {
address = "127.0.0.1"
}
return address + ":6379"
}(), "Address of the redis server")
redisDatabase = flag.Int("redis_db", 0, "Index of redis database to use")
redisPassword = flag.String("redis_password", "", "Password of the redis server")
// Database-related flags
rethinkdbAddress = flag.String("rethinkdb_address", func() string {
address := os.Getenv("RETHINKDB_PORT_28015_TCP_ADDR")
if address == "" {
address = "127.0.0.1"
}
return address + ":28015"
}(), "Address of the RethinkDB database")
rethinkdbKey = flag.String("rethinkdb_key", os.Getenv("RETHINKDB_AUTHKEY"), "Authentication key of the RethinkDB database")
rethinkdbDatabase = flag.String("rethinkdb_db", func() string {
database := os.Getenv("RETHINKDB_DB")
if database == "" {
database = "dev"
}
return database
}(), "Database name on the RethinkDB server")
// nsq and lookupd addresses
nsqdAddress = flag.String("nsqd_address", func() string {
address := os.Getenv("NSQD_PORT_4150_TCP_ADDR")
if address == "" {
address = "127.0.0.1"
}
return address + ":4150"
}(), "Address of the nsqd server")
lookupdAddress = flag.String("lookupd_address", func() string {
address := os.Getenv("NSQLOOKUPD_PORT_4160_TCP_ADDR")
if address == "" {
address = "127.0.0.1"
}
return address + ":4160"
}(), "Address of the lookupd server")
// YubiCloud params
yubiCloudID = flag.String("yubicloud_id", "", "YubiCloud API id")
yubiCloudKey = flag.String("yubicloud_key", "", "YubiCloud API key")
// etcd
etcdAddress = flag.String("etcd_address", "", "etcd peer addresses split by commas")
etcdCAFile = flag.String("etcd_ca_file", "", "etcd path to server cert's ca")
etcdCertFile = flag.String("etcd_cert_file", "", "etcd path to client cert file")
etcdKeyFile = flag.String("etcd_key_file", "", "etcd path to client key file")
etcdPath = flag.String("etcd_path", "settings/", "Path of the keys")
// slack
slackURL = flag.String("slack_url", "", "URL of the Slack Incoming webhook")
slackLevels = flag.String("slack_level", "warning", "minimal level required to have messages sent to slack")
slackChannel = flag.String("slack_channel", "#notif-api-logs", "channel to which Slack bot will send messages")
slackIcon = flag.String("slack_icon", ":ghost:", "emoji icon of the Slack bot")
slackUsername = flag.String("slack_username", "API", "username of the Slack bot")
// Password bloom filter path
bloomFilter = flag.String("bloom_filter", "bloom.db", "Bloom filter containing passwords")
bloomCount = flag.Uint("bloom_count", 14522336, "Estimated count of passwords in the bloom filter")
// raven dsn
ravenDSN = flag.String("raven_dsn", "", "DSN of the Raven connection")
)
func main() {
// Parse the flags
flag.Parse()
// Put config into the environment package
env.Config = &env.Flags{
BindAddress: *bindAddress,
APIVersion: *apiVersion,
LogFormatterType: *logFormatterType,
ForceColors: *forceColors,
EmailDomain: *emailDomain,
SessionDuration: *sessionDuration,
RedisAddress: *redisAddress,
RedisDatabase: *redisDatabase,
RedisPassword: *redisPassword,
RethinkDBAddress: *rethinkdbAddress,
RethinkDBKey: *rethinkdbKey,
RethinkDBDatabase: *rethinkdbDatabase,
NSQdAddress: *nsqdAddress,
LookupdAddress: *lookupdAddress,
YubiCloudID: *yubiCloudID,
YubiCloudKey: *yubiCloudKey,
SlackURL: *slackURL,
SlackLevels: *slackLevels,
SlackChannel: *slackChannel,
SlackIcon: *slackIcon,
SlackUsername: *slackUsername,
BloomFilter: *bloomFilter,
BloomCount: *bloomCount,
RavenDSN: *ravenDSN,
}
// Generate a mux
mux := setup.PrepareMux(env.Config)
// Make the mux handle every request
http.Handle("/", mux)
// Log that we're starting the server
env.Log.WithFields(logrus.Fields{
"address": env.Config.BindAddress,
}).Info("Starting the HTTP server")
// Initialize the goroutine listening to signals passed to the app
graceful.HandleSignals()
// Pre-graceful shutdown event
graceful.PreHook(func() {
env.Log.Info("Received a singnal, stopping the application")
})
// Post-shutdown event
graceful.PostHook(func() {
env.Log.Info("Stopped the application")
})
// Listen to the passed address
listener, err := net.Listen("tcp", env.Config.BindAddress)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
"address": *bindAddress,
}).Fatal("Cannot set up a TCP listener")
}
// Start the listening
err = graceful.Serve(listener, http.DefaultServeMux)
if err != nil {
// Don't use .Fatal! We need the code to shut down properly.
env.Log.Error(err)
}
// If code reaches this place, it means that it was forcefully closed.
// Wait until open connections close.
graceful.Wait()
}