@@ -17,8 +17,6 @@ import (
17
17
)
18
18
19
19
const (
20
- // App Env determines type of DB connection.
21
- appEnv = "APP_ENV"
22
20
// Postgres DB vars - Required for all Envs.
23
21
pgUser = "PG_USER"
24
22
pgPassword = "PG_PASSWORD"
@@ -51,7 +49,6 @@ const (
51
49
type (
52
50
options struct {
53
51
// Required vars
54
- appEnv string
55
52
pgHost , pgPort , pgUser , pgPassword , pgDatabase string
56
53
dbInstanceConnectionName string
57
54
apiKeys map [string ]bool
@@ -73,21 +70,23 @@ type (
73
70
cloudSQLConfig struct {
74
71
options
75
72
}
76
- TestDBConfig struct {
73
+ testDBConfig struct {
77
74
options
78
75
}
79
76
)
80
77
81
78
func gatherOptions () options {
82
- appEnv := environment .GetString (appEnv , "production" )
83
-
84
- options := options {
85
- appEnv : appEnv ,
79
+ return options {
86
80
// Required vars
81
+ apiKeys : environment .MustGetStringMap (apiKeys , "," ),
87
82
pgUser : environment .MustGetString (pgUser ),
88
83
pgPassword : environment .MustGetString (pgPassword ),
89
84
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 , "" ),
91
90
// Optional vars
92
91
port : environment .GetString (port , defaultPort ),
93
92
maxRelayBatchSize : int (environment .GetInt64 (maxRelayBatchSize , defaultBatchSize )),
@@ -98,21 +97,9 @@ func gatherOptions() options {
98
97
debug : environment .GetBool (debug , defaultDebug ),
99
98
chanSize : int (environment .GetInt64 (chanSize , defaultChanSize )),
100
99
}
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
113
100
}
114
101
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.
116
103
// Intended for production use. Will be used if APP_ENV is 'production'.
117
104
func (c * cloudSQLConfig ) GetDriver (ctx context.Context ) (driver * postgresdriver.PostgresDriver , cleanup func () error , err error ) {
118
105
driverConfig := postgresdriver.CloudSQLConfig {
@@ -130,9 +117,9 @@ func (c *cloudSQLConfig) GetDriver(ctx context.Context) (driver *postgresdriver.
130
117
return driver , cleanup , nil
131
118
}
132
119
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.
134
121
// 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 ) {
136
123
connectionString := fmt .Sprintf ("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable" ,
137
124
c .options .pgHost ,
138
125
c .options .pgPort ,
@@ -166,15 +153,19 @@ func main() {
166
153
167
154
log := zap .Must (logConfig .Build ())
168
155
169
- // Choose DB configuration based on APP_ENV
156
+ // Choose DB configuration based on DB config vars
170
157
var dbConfig DBConfig
171
- switch options . appEnv {
172
- case "production" :
173
- // For CloudSQL DB
158
+ switch {
159
+ // For CloudSQL DB
160
+ case options . dbInstanceConnectionName != "" :
174
161
dbConfig = & cloudSQLConfig {options : options }
162
+
163
+ // For local DB
164
+ case options .pgHost != "" && options .pgPort != "" :
165
+ dbConfig = & testDBConfig {options : options }
166
+
175
167
default :
176
- // For local DB
177
- dbConfig = & TestDBConfig {options : options }
168
+ panic ("invalid DB configuration" )
178
169
}
179
170
180
171
driver , cleanup , err := dbConfig .GetDriver (context .Background ())
0 commit comments