Skip to content
This repository was archived by the owner on May 2, 2024. It is now read-only.

Commit 4bc0ff9

Browse files
author
commoddity
committed
feat: derive connection method for which config vars are present
1 parent 3302048 commit 4bc0ff9

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Required vars
2-
APP_ENV=
32
PG_USER=
43
PG_PASSWORD=
54
PG_DATABASE=

main.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
)
1818

1919
const (
20-
// App Env determines type of DB connection.
21-
appEnv = "APP_ENV"
2220
// Postgres DB vars - Required for all Envs.
2321
pgUser = "PG_USER"
2422
pgPassword = "PG_PASSWORD"
@@ -51,7 +49,6 @@ const (
5149
type (
5250
options struct {
5351
// Required vars
54-
appEnv string
5552
pgHost, pgPort, pgUser, pgPassword, pgDatabase string
5653
dbInstanceConnectionName string
5754
apiKeys map[string]bool
@@ -73,21 +70,23 @@ type (
7370
cloudSQLConfig struct {
7471
options
7572
}
76-
TestDBConfig struct {
73+
testDBConfig struct {
7774
options
7875
}
7976
)
8077

8178
func gatherOptions() options {
82-
appEnv := environment.GetString(appEnv, "production")
83-
84-
options := options{
85-
appEnv: appEnv,
79+
return options{
8680
// Required vars
81+
apiKeys: environment.MustGetStringMap(apiKeys, ","),
8782
pgUser: environment.MustGetString(pgUser),
8883
pgPassword: environment.MustGetString(pgPassword),
8984
pgDatabase: environment.MustGetString(pgDatabase),
90-
apiKeys: environment.MustGetStringMap(apiKeys, ","),
85+
// CloudSQL DB Config var
86+
dbInstanceConnectionName: environment.GetString(dbInstanceConnectionName, ""),
87+
// Local DB Config vars
88+
pgHost: environment.GetString(pgHost, ""),
89+
pgPort: environment.GetString(pgPort, ""),
9190
// Optional vars
9291
port: environment.GetString(port, defaultPort),
9392
maxRelayBatchSize: int(environment.GetInt64(maxRelayBatchSize, defaultBatchSize)),
@@ -98,21 +97,9 @@ func gatherOptions() options {
9897
debug: environment.GetBool(debug, defaultDebug),
9998
chanSize: int(environment.GetInt64(chanSize, defaultChanSize)),
10099
}
101-
102-
switch appEnv {
103-
case "production":
104-
// For CloudSQL DB
105-
options.dbInstanceConnectionName = environment.MustGetString(dbInstanceConnectionName)
106-
default:
107-
// For local DB
108-
options.pgHost = environment.MustGetString(pgHost)
109-
options.pgPort = environment.MustGetString(pgPort)
110-
}
111-
112-
return options
113100
}
114101

115-
// cloudSQLConfig.GetDriver connects to a GCP CloudSQL instance using the cloudsqlconn lib.
102+
// CloudSQLConfig.GetDriver connects to a GCP CloudSQL instance using the cloudsqlconn lib.
116103
// Intended for production use. Will be used if APP_ENV is 'production'.
117104
func (c *cloudSQLConfig) GetDriver(ctx context.Context) (driver *postgresdriver.PostgresDriver, cleanup func() error, err error) {
118105
driverConfig := postgresdriver.CloudSQLConfig{
@@ -130,9 +117,9 @@ func (c *cloudSQLConfig) GetDriver(ctx context.Context) (driver *postgresdriver.
130117
return driver, cleanup, nil
131118
}
132119

133-
// TestDBConfig.GetDriver connects to a Postgres database using standard connection string and user/PW.
120+
// testDBConfig.GetDriver connects to a Postgres database using standard connection string and user/PW.
134121
// Intended to be used for running tests on a local Docker container. Will be used if APP_ENV is 'test' or 'development'.
135-
func (c *TestDBConfig) GetDriver(ctx context.Context) (driver *postgresdriver.PostgresDriver, cleanup func() error, err error) {
122+
func (c *testDBConfig) GetDriver(ctx context.Context) (driver *postgresdriver.PostgresDriver, cleanup func() error, err error) {
136123
connectionString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
137124
c.options.pgHost,
138125
c.options.pgPort,
@@ -166,15 +153,19 @@ func main() {
166153

167154
log := zap.Must(logConfig.Build())
168155

169-
// Choose DB configuration based on APP_ENV
156+
// Choose DB configuration based on DB config vars
170157
var dbConfig DBConfig
171-
switch options.appEnv {
172-
case "production":
173-
// For CloudSQL DB
158+
switch {
159+
// For CloudSQL DB
160+
case options.dbInstanceConnectionName != "":
174161
dbConfig = &cloudSQLConfig{options: options}
162+
163+
// For local DB
164+
case options.pgHost != "" && options.pgPort != "":
165+
dbConfig = &testDBConfig{options: options}
166+
175167
default:
176-
// For local DB
177-
dbConfig = &TestDBConfig{options: options}
168+
panic("invalid DB configuration")
178169
}
179170

180171
driver, cleanup, err := dbConfig.GetDriver(context.Background())

0 commit comments

Comments
 (0)