-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
106 lines (88 loc) · 2.89 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
package main
import (
"flag"
"fmt"
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/negroni"
"github.com/ello/streams/api"
"github.com/ello/streams/service"
"github.com/ello/streams/util"
"github.com/julienschmidt/httprouter"
nlog "github.com/meatballhat/negroni-logrus"
librato "github.com/mihasya/go-metrics-librato"
metrics "github.com/rcrowley/go-metrics"
)
var commit string
var startTime = time.Now()
var help bool
var helpMessage = `ELLO STREAM API
--------------------------
Set ENV Variables to configure:
PORT for the port to run this service on. Default is 8080
ROSHI_URL for the location of the roshi instance. Default is http://localhost:6302
ROSHI_TIMEOUT for the timeout (in Seconds) for roshi connections. Default is 5s.
AUTH_ENABLED any value will enable basic auth. Default is disabled.
AUTH_USERNAME for the auth username. Default is 'ello'.
AUTH_PASSWORD for the auth password. Default is 'password'.
LOG_LEVEL for the log level. Valid levels are "debug", "info", "warn", "error". Default is warn.
LIBRATO_EMAIL librato config
LIBRATO_TOKEN librato config
LIBRATO_HOSTNAME librato config
`
func main() {
flag.BoolVar(&help, "h", false, "help?")
flag.Parse()
if help {
fmt.Println(helpMessage)
os.Exit(0)
}
var logLevel log.Level
switch util.GetEnvWithDefault("LOG_LEVEL", "warn") {
case "debug":
logLevel = log.DebugLevel
case "info":
logLevel = log.InfoLevel
case "error":
logLevel = log.ErrorLevel
default:
logLevel = log.WarnLevel
}
log.SetLevel(logLevel)
fmt.Printf("Using log level [%v]\n", logLevel)
roshi := util.GetEnvWithDefault("ROSHI_URL", "http://localhost:6302")
streamsService, err := service.NewRoshiStreamService(roshi, time.Duration(util.GetEnvIntWithDefault("ROSHI_TIMEOUT", 5))*time.Second)
if err != nil {
log.Panic(err)
}
authConfig := api.AuthConfig{
Username: []byte(util.GetEnvWithDefault("AUTH_USERNAME", "ello")),
Password: []byte(util.GetEnvWithDefault("AUTH_PASSWORD", "password")),
Enabled: util.IsEnvPresent("AUTH_ENABLED"),
}
log.Infof(authConfig.String())
if util.IsEnvPresent("LIBRATO_TOKEN") {
go librato.Librato(metrics.DefaultRegistry,
10e9, // interval
os.Getenv("LIBRATO_EMAIL"), // account owner email address
os.Getenv("LIBRATO_TOKEN"), // Librato API token
os.Getenv("LIBRATO_HOSTNAME"), // source
[]float64{0.95}, // percentiles to send
time.Millisecond, // time unit
)
}
router := httprouter.New()
streamsController := api.NewStreamController(streamsService, authConfig)
streamsController.Register(router)
healthController := api.NewHealthController(startTime, commit, roshi)
healthController.Register(router)
n := negroni.New(
negroni.NewRecovery(),
nlog.NewCustomMiddleware(logLevel, &log.TextFormatter{}, "web"),
)
n.UseHandler(router)
port := util.GetEnvWithDefault("PORT", "8080")
serverAt := ":" + port
n.Run(serverAt)
}