-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
126 lines (117 loc) · 3.41 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
package main
import (
"context"
"database/sql"
"fmt"
"net/http"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/garyburd/redigo/redis"
_ "github.com/lib/pq"
"github.com/travis-ci/cloud-brain/cbcontext"
"github.com/travis-ci/cloud-brain/cloudbrain"
"github.com/travis-ci/cloud-brain/database"
cbhttp "github.com/travis-ci/cloud-brain/http"
"gopkg.in/urfave/cli.v2"
)
func main() {
app := &cli.App{
Name: "cloudbrain-http",
Version: cloudbrain.VersionString,
Copyright: cloudbrain.CopyrightString,
Usage: "Run the HTTP server part of Cloud Brain",
Action: mainAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "redis-url",
EnvVars: []string{"CLOUDBRAIN_REDIS_URL", "REDIS_URL"},
},
&cli.IntFlag{
Name: "redis-max-idle",
Value: 3,
Usage: "The maximum number of idle Redis connections",
EnvVars: []string{"CLOUDBRAIN_REDIS_MAX_IDLE"},
},
&cli.IntFlag{
Name: "redis-max-active",
Value: 5,
Usage: "The maximum number of active Redis connections",
EnvVars: []string{"CLOUDBRAIN_REDIS_MAX_ACTIVE"},
},
&cli.DurationFlag{
Name: "redis-idle-timeout",
Value: 3 * time.Minute,
EnvVars: []string{"CLOUDBRAIN_REDIS_IDLE_TIMEOUT"},
},
&cli.StringFlag{
Name: "redis-worker-prefix",
Value: "cloud-brain:worker",
Usage: "The Redis key prefix to use for keys used by the background workers",
EnvVars: []string{"CLOUDBRAIN_REDIS_WORKER_PREFIX"},
},
&cli.StringFlag{
Name: "database-url",
Usage: "The URL for the PostgreSQL database to use",
EnvVars: []string{"CLOUDBRAIN_DATABASE_URL", "DATABASE_URL"},
},
&cli.StringFlag{
Name: "addr",
Usage: "host:port to listen to",
Value: func() string {
v := ":" + os.Getenv("PORT")
if v == ":" {
v = ":42191"
}
return v
}(),
EnvVars: []string{"CLOUDBRAIN_ADDR"},
},
&cli.StringSliceFlag{
Name: "auth-token",
Usage: "authentication token(s) to accept",
EnvVars: []string{"CLOUDBRAIN_AUTH_TOKEN"},
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}
func mainAction(c *cli.Context) error {
ctx := context.Background()
logrus.SetFormatter(&logrus.TextFormatter{DisableColors: true})
if c.String("redis-url") == "" {
cbcontext.LoggerFromContext(ctx).Fatal("redis-url flag is required")
}
redisURL := c.String("redis-url")
redisPool := &redis.Pool{
MaxIdle: c.Int("redis-max-idle"),
MaxActive: c.Int("redis-max-active"),
IdleTimeout: c.Duration("redis-idle-timeout"),
Dial: func() (redis.Conn, error) {
return redis.DialURL(redisURL)
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
if c.String("database-url") == "" {
cbcontext.LoggerFromContext(ctx).Fatal("database-url flag is required")
}
pgdb, err := sql.Open("postgres", c.String("database-url"))
if err != nil {
cbcontext.LoggerFromContext(ctx).WithField("err", err).Fatal("couldn't connect to postgres")
}
db := database.NewPostgresDB([32]byte{}, pgdb)
redisWorkerPrefix := c.String("redis-worker-prefix")
core := cloudbrain.NewCore(db, redisPool, redisWorkerPrefix)
err = http.ListenAndServe(c.String("addr"), cbhttp.Handler(ctx, core, c.StringSlice("auth-token")))
if err != nil {
cbcontext.LoggerFromContext(ctx).WithField("err", err).Fatal("ListenAndServe returned error")
}
return nil
}