/
backend_connections.go
199 lines (167 loc) · 4.4 KB
/
backend_connections.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package config
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/url"
"strconv"
"time"
"github.com/Sirupsen/logrus"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/ory-am/hydra/pkg"
"github.com/pkg/errors"
"github.com/spf13/viper"
r "gopkg.in/gorethink/gorethink.v3"
"github.com/go-redis/redis"
"strings"
)
type MemoryConnection struct{}
type SQLConnection struct {
db *sqlx.DB
URL *url.URL
}
func (c *SQLConnection) GetDatabase() *sqlx.DB {
if c.db != nil {
return c.db
}
var err error
if err = pkg.Retry(time.Second*15, time.Minute*2, func() error {
logrus.Infof("Connecting with %s", c.URL.Scheme+"://*:*@"+c.URL.Host+c.URL.Path+"?"+c.URL.RawQuery)
u := c.URL.String()
if c.URL.Scheme == "mysql" {
u = strings.Replace(u, "mysql://", "", -1)
}
if c.db, err = sqlx.Open(c.URL.Scheme, u); err != nil {
return errors.Errorf("Could not connect to SQL: %s", err)
} else if err := c.db.Ping(); err != nil {
return errors.Errorf("Could not connect to SQL: %s", err)
}
logrus.Infof("Connected to SQL!")
return nil
}); err != nil {
logrus.Fatalf("Could not connect to SQL: %s", err)
}
return c.db
}
type RethinkDBConnection struct {
session *r.Session
URL *url.URL
}
func (c *RethinkDBConnection) GetSession() *r.Session {
if c.session != nil {
return c.session
}
var err error
var username, password string
if len(c.URL.Path) <= 1 {
logrus.Fatalf("Database hostname specified, but database name is missing.")
}
database := c.URL.Path[1:]
if c.URL.User != nil {
password, _ = c.URL.User.Password()
username = c.URL.User.Username()
}
if err := pkg.Retry(time.Second*15, time.Minute*2, func() error {
logrus.Infof("Connecting with RethinkDB: %s@%s/%s", username, c.URL.Host, database)
options := r.ConnectOpts{
Address: c.URL.Host,
Username: username,
Password: password,
KeepAlivePeriod: 10 * time.Second,
}
importRethinkDBRootCA(&options)
if c.session, err = r.Connect(options); err != nil {
return errors.Errorf("Could not connect to RethinkDB: %s", err)
}
if _, err := r.DBList().Contains(database).Do(func(e r.Term) r.Term {
return r.Branch(
e,
map[string]interface{}{"dbs_created": 0},
r.DBCreate(database),
)
}).RunWrite(c.session); err != nil {
return errors.Errorf("Could not create database: %s", err)
}
c.session.Use(database)
logrus.Infof("Connected to RethinkDB!")
return nil
}); err != nil {
logrus.Fatalf("Could not connect to RethinkDB: %s", err)
}
return c.session
}
// importRethinkDBRootCA checks for the configuration values RETHINK_TLS_CERT_PATH
// or RETHINK_TLS_CERT and adds the certificate to the connect options
func importRethinkDBRootCA(opts *r.ConnectOpts) {
var cert []byte
certPath := viper.GetString("RETHINK_TLS_CERT_PATH")
if certPath != "" {
var err error
cert, err = ioutil.ReadFile(certPath)
if err != nil {
logrus.Warningf("Could not read rethinkdb certificate: %s", err)
return
}
}
certString := viper.GetString("RETHINK_TLS_CERT")
if certString != "" {
cert = []byte(certString)
}
if cert != nil {
roots := x509.NewCertPool()
roots.AppendCertsFromPEM(cert)
opts.TLSConfig = &tls.Config{
RootCAs: roots,
}
logrus.Warnln("Loaded self-signed certificate for rethinkdb")
}
}
func (c *RethinkDBConnection) CreateTableIfNotExists(table string) {
if _, err := r.TableList().Contains(table).Do(func(e r.Term) r.Term {
return r.Branch(
e,
map[string]interface{}{"tables_created": 0},
r.TableCreate(table),
)
}).RunWrite(c.GetSession()); err != nil {
logrus.Fatalf("Could not create table: %s", err)
}
}
type RedisConnection struct {
db *redis.Client
URL *url.URL
}
func (c *RedisConnection) RedisSession() *redis.Client {
if c.db != nil {
return c.db
}
var password string
var database int
var err error
if len(c.URL.Path) <= 1 {
logrus.Infof("Defaulting database to 0.")
database = 0
} else {
database, err = strconv.Atoi(c.URL.Path[1:])
if err != nil {
logrus.Fatalf("Database must be an integer.")
}
}
if c.URL.User != nil {
if p, exists := c.URL.User.Password(); exists {
password = p
} else {
// No username, so first value is taken as password
password = c.URL.User.Username()
}
}
options := &redis.Options{
Addr: c.URL.Host,
Password: password,
DB: database,
}
c.db = redis.NewClient(options)
return c.db
}