/
config.go
197 lines (169 loc) · 6.27 KB
/
config.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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/kcarretto/realm/tavern/internal/ent"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"entgo.io/ent/dialect/sql"
"github.com/go-sql-driver/mysql"
)
var (
// EnvEnableTestData if set will populate the database with test data.
EnvEnableTestData = EnvString{"ENABLE_TEST_DATA", ""}
// EnvOAuthClientID set to configure OAuth Client ID.
// EnvOAuthClientSecret set to configure OAuth Client Secret.
// EnvOAuthDomain set to configure OAuth domain for consent flow redirect.
EnvOAuthClientID = EnvString{"OAUTH_CLIENT_ID", ""}
EnvOAuthClientSecret = EnvString{"OAUTH_CLIENT_SECRET", ""}
EnvOAuthDomain = EnvString{"OAUTH_DOMAIN", ""}
// EnvMySQLAddr defines the MySQL address to connect to, if unset SQLLite is used.
// EnvMySQLNet defines the network used to connect to MySQL (e.g. unix).
// EnvMySQLUser defines the MySQL user to authenticate as.
// EnvMySQLPasswd defines the password for the MySQL user to authenticate with.
// EnvMySQLDB defines the name of the MySQL database to use.
EnvMySQLAddr = EnvString{"MYSQL_ADDR", ""}
EnvMySQLNet = EnvString{"MYSQL_NET", "tcp"}
EnvMySQLUser = EnvString{"MYSQL_USER", "root"}
EnvMySQLPasswd = EnvString{"MYSQL_PASSWD", ""}
EnvMySQLDB = EnvString{"MYSQL_DB", "tavern"}
// EnvDBMaxIdleConns defines the maximum number of idle db connections to allow.
// EnvDBMaxOpenConns defines the maximum number of open db connections to allow.
// EnvDBMaxConnLifetime defines the maximum lifetime of a db connection.
EnvDBMaxIdleConns = EnvInteger{"DB_MAX_IDLE_CONNS", 10}
EnvDBMaxOpenConns = EnvInteger{"DB_MAX_OPEN_CONNS", 100}
EnvDBMaxConnLifetime = EnvInteger{"DB_MAX_CONN_LIFETIME", 3600}
// EnvEnablePProf enables performance profiling and should not be enabled in production.
EnvEnablePProf = EnvString{"ENABLE_PPROF", ""}
)
// Config holds information that controls the behaviour of Tavern
type Config struct {
srv *http.Server
mysqlDSN string
client *ent.Client
oauth oauth2.Config
}
// Connect to the database using configured drivers and uri
func (cfg *Config) Connect(options ...ent.Option) (*ent.Client, error) {
if cfg != nil && cfg.client != nil {
return cfg.client, nil
}
var (
mysqlDSN = "file:ent?mode=memory&cache=shared&_fk=1"
driver = "sqlite3"
)
if cfg != nil && cfg.mysqlDSN != "" {
mysqlDSN = cfg.mysqlDSN
driver = "mysql"
}
drv, err := sql.Open(driver, mysqlDSN)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %w", err)
}
// Setup DB Pool Config
var (
maxIdleConns = EnvDBMaxIdleConns.Int()
maxOpenConns = EnvDBMaxOpenConns.Int()
maxConnLifetime = time.Duration(EnvDBMaxConnLifetime.Int()) * time.Second
)
if maxIdleConns < 0 {
log.Fatalf("[FATAL] %q must be greater than or equal to 0 if set, got: %d", EnvDBMaxIdleConns.Key, maxIdleConns)
}
if maxOpenConns <= 0 {
log.Fatalf("[FATAL] %q must be greater than 0 if set, got: %d", EnvDBMaxOpenConns.Key, maxOpenConns)
}
if maxConnLifetime <= 10*time.Second {
log.Fatalf("[FATAL] %q must be greater than 10 seconds if set, got: %d", EnvDBMaxConnLifetime.Key, maxConnLifetime)
}
// Get the underlying sql.DB object of the driver.
db := drv.DB()
db.SetMaxIdleConns(maxIdleConns)
db.SetMaxOpenConns(maxOpenConns)
db.SetConnMaxLifetime(maxConnLifetime)
return ent.NewClient(append(options, ent.Driver(drv))...), nil
}
// IsPProfEnabled returns true if performance profiling has been enabled.
func (cfg *Config) IsPProfEnabled() bool {
return EnvEnablePProf.String() != ""
}
// IsTestDataEnabled returns true if a value for the "ENABLE_TEST_DATA" environment variable is set.
func (cfg *Config) IsTestDataEnabled() bool {
return EnvEnableTestData.String() != ""
}
// ConfigureHTTPServer enables the configuration of the Tavern HTTP server. The endpoint field will be
// overwritten with Tavern's HTTP handler when Tavern is run.
func ConfigureHTTPServer(address string, options ...func(*http.Server)) func(*Config) {
srv := &http.Server{
Addr: address,
}
for _, opt := range options {
opt(srv)
}
return func(cfg *Config) {
cfg.srv = srv
}
}
// ConfigureOAuthFromEnv sets OAuth config values from the environment
func ConfigureOAuthFromEnv(redirectPath string) func(*Config) {
return func(cfg *Config) {
var (
clientID = EnvOAuthClientID.String()
clientSecret = EnvOAuthClientSecret.String()
domain = EnvOAuthDomain.String()
)
// If none are set, default to auth disabled
if clientID == "" && clientSecret == "" && domain == "" {
log.Printf("[WARN] OAuth is not configured, authentication disabled")
return
}
// If partially set, panic to indicate OAuth is improperly configured
if clientID == "" {
log.Fatalf("[FATAL] To configure OAuth, must provide value for environment var 'OAUTH_CLIENT_ID'")
}
if clientSecret == "" {
log.Fatalf("[FATAL] To configure OAuth, must provide value for environment var 'OAUTH_CLIENT_SECRET'")
}
if domain == "" {
log.Fatalf("[FATAL] To configure OAuth, must provide value for environment var 'OAUTH_DOMAIN'")
}
if !strings.HasPrefix(domain, "http") {
log.Printf("[WARN] Domain (%q) not prefixed with scheme (http:// or https://), defaulting to https://", domain)
domain = fmt.Sprintf("https://%s", domain)
}
cfg.oauth = oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: domain + redirectPath,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
}
}
// ConfigureMySQLFromEnv sets MySQL config values from the environment
func ConfigureMySQLFromEnv() func(*Config) {
return func(cfg *Config) {
mysqlConfig := mysql.NewConfig()
mysqlConfig.Addr = EnvMySQLAddr.String()
if mysqlConfig.Addr == "" {
log.Printf("[WARN] MySQL is not configured, using SQLite")
return
}
mysqlConfig.ParseTime = true
mysqlConfig.Net = EnvMySQLNet.String()
mysqlConfig.User = EnvMySQLUser.String()
mysqlConfig.Passwd = EnvMySQLPasswd.String()
mysqlConfig.DBName = EnvMySQLDB.String()
cfg.mysqlDSN = mysqlConfig.FormatDSN()
}
}
// ConfigureMySQLFromClient sets the provided Ent client as the main interface for DB access.
func ConfigureMySQLFromClient(client *ent.Client) func(*Config) {
return func(cfg *Config) {
cfg.client = client
}
}