@@ -4,18 +4,18 @@ This repo contains the Go source code for a simple web app that can be deployed
4
4
5
5
## Before you begin
6
6
7
- 1 . If you haven't already, set up a Go Development Environment by following the [ Go setup guide] ( https://cloud.google.com/go/docs/setup ) and
7
+ 1 . If you haven't already, set up a Go Development Environment by following the [ Go setup guide] ( https://cloud.google.com/go/docs/setup ) and
8
8
[ create a project] ( https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project ) .
9
9
10
- 1 . Create a Cloud SQL for MySQL instance by following these
10
+ 1 . Create a Cloud SQL for MySQL instance by following these
11
11
[ instructions] ( https://cloud.google.com/sql/docs/mysql/create-instance ) .
12
12
Note the connection string, database user, and database password that you create.
13
13
14
- 1 . Create a database for your application by following these
14
+ 1 . Create a database for your application by following these
15
15
[ instructions] ( https://cloud.google.com/sql/docs/mysql/create-manage-databases ) .
16
- Note the database name.
16
+ Note the database name.
17
17
18
- 1 . Create a service account with the 'Cloud SQL Client' permissions by following these
18
+ 1 . Create a service account with the 'Cloud SQL Client' permissions by following these
19
19
[ instructions] ( https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account ) .
20
20
Download a JSON key to use to authenticate your connection.
21
21
@@ -35,7 +35,7 @@ To run the sample locally with a TCP connection, set environment variables and l
35
35
Use these terminal commands to initialize environment variables:
36
36
``` bash
37
37
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
38
- export DB_TCP_HOST =' 127.0.0.1'
38
+ export INSTANCE_HOST =' 127.0.0.1'
39
39
export DB_PORT=' 3306'
40
40
export DB_USER=' <DB_USER_NAME>'
41
41
export DB_PASS=' <DB_PASSWORD>'
@@ -51,7 +51,7 @@ Then use this command to launch the proxy in the background:
51
51
Use these PowerShell commands to initialize environment variables:
52
52
``` powershell
53
53
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
54
- $env:DB_TCP_HOST ="127.0.0.1"
54
+ $env:INSTANCE_HOST ="127.0.0.1"
55
55
$env:DB_PORT="3306"
56
56
$env:DB_USER="<DB_USER_NAME>"
57
57
$env:DB_PASS="<DB_PASSWORD>"
@@ -72,24 +72,18 @@ sudo mkdir ./cloudsql
72
72
sudo chown -R $USER ./cloudsql
73
73
```
74
74
75
- You'll also need to initialize an environment variable containing the directory you just created:
76
-
77
- ``` bash
78
- export DB_SOCKET_DIR=./cloudsql
79
- ```
80
-
81
75
Use these terminal commands to initialize environment variables:
82
76
``` bash
83
77
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
84
- export INSTANCE_CONNECTION_NAME= ' <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
78
+ export INSTANCE_UNIX_SOCKET= ' ./cloudsql/ <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
85
79
export DB_USER=' <DB_USER_NAME>'
86
80
export DB_PASS=' <DB_PASSWORD>'
87
81
export DB_NAME=' <DB_NAME>'
88
82
```
89
83
90
84
Then use this command to launch the proxy in the background:
91
85
``` bash
92
- ./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
86
+ ./cloud_sql_proxy -dir=./cloudsql --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
93
87
```
94
88
95
89
### Testing the application
@@ -102,16 +96,16 @@ To test the application locally, follow these steps after the proxy is running:
102
96
103
97
## Deploying to App Engine Standard
104
98
105
- To run the sample on GAE-Standard, create an App Engine project by following the setup for these
99
+ To run the sample on GAE-Standard, create an App Engine project by following the setup for these
106
100
[ instructions] ( https://cloud.google.com/appengine/docs/standard/go/quickstart#before-you-begin ) .
107
101
108
- First, update ` app.standard.yaml ` with the correct values to pass the environment
102
+ First, update ` app.standard.yaml ` with the correct values to pass the environment
109
103
variables into the runtime. Your ` app.standard.yaml ` file should look like this:
110
104
111
105
``` yaml
112
106
runtime : go113
113
107
env_variables :
114
- INSTANCE_CONNECTION_NAME : <project-id>:<region>:<instance-name>
108
+ INSTANCE_UNIX_SOCKET : /cloudsql/ <project-id>:<region>:<instance-name>
115
109
DB_USER : YOUR_DB_USER
116
110
DB_PASS : YOUR_DB_PASS
117
111
DB_NAME : YOUR_DB
@@ -127,22 +121,22 @@ gcloud app deploy app.standard.yaml
127
121
128
122
## Deploying to App Engine Flexible
129
123
130
- To run the sample on GAE-Flex, create an App Engine project by following the setup for these
124
+ To run the sample on GAE-Flex, create an App Engine project by following the setup for these
131
125
[ instructions] ( https://cloud.google.com/appengine/docs/standard/go/quickstart#before-you-begin ) .
132
126
133
- First, update ` app.flexible.yaml ` with the correct values to pass the environment
127
+ First, update ` app.flexible.yaml ` with the correct values to pass the environment
134
128
variables into the runtime. Your ` app.flexible.yaml ` file should look like this:
135
129
``` yaml
136
130
runtime : custom
137
131
env : flex
138
132
139
133
env_variables :
140
- INSTANCE_CONNECTION_NAME : <project>:<region>:<instance>
134
+ INSTANCE_UNIX_SOCKET : /cloudsql/ <project>:<region>:<instance>
141
135
DB_USER : <your_database_username>
142
136
DB_PASS : <your_database_password>
143
137
DB_NAME : <your_database_name>
144
138
145
- beta_settings :
139
+ beta_settings :
146
140
cloud_sql_instances : <project>:<region>:<instance>
147
141
` ` `
148
142
@@ -169,7 +163,7 @@ gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
169
163
``` sh
170
164
gcloud run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
171
165
--add-cloudsql-instances ' <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
172
- --update-env-vars INSTANCE_CONNECTION_NAME= ' <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
166
+ --update-env-vars INSTANCE_UNIX_SOCKET= ' /cloudsql/ <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
173
167
--update-env-vars DB_USER=' <DB_USER_NAME>' \
174
168
--update-env-vars DB_PASS=' <DB_PASSWORD>' \
175
169
--update-env-vars DB_NAME=' <DB_NAME>'
@@ -202,4 +196,31 @@ gcloud beta run deploy SERVICE --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
202
196
203
197
4 . Navigate your browser to the URL noted in step 2.
204
198
205
- For more details about using Cloud Run see http://cloud.run.
199
+ For more details about using Cloud Run see http://cloud.run .
200
+
201
+ ## Running Integration Tests
202
+
203
+ The integration tests depend on a Unix socket and a TCP listener provided by the
204
+ Cloud SQL Auth Proxy. To run the tests, you will need to start two instances of
205
+ the Cloud SQL Auth Proxy, one for a TCP connection and one for a Unix socket
206
+ connection.
207
+
208
+ ```
209
+ cloud_sql_proxy -instances=some-project:some-region:some-instance=tcp:3306
210
+ cloud_sql_proxy -instances=some-project:some-region:some-instance -dir /cloudsql
211
+ ```
212
+
213
+ To run integration tests, use the following command, setting environment
214
+ variables to the correct values:
215
+
216
+ ```
217
+ GOLANG_SAMPLES_E2E_TEST="yes" \
218
+ MYSQL_USER=some-user \
219
+ MYSQL_PASSWORD=some-pass
220
+ MYSQL_DATABASE=some-db \
221
+ MYSQL_PORT=3307 \
222
+ MYSQL_HOST=127.0.0.1
223
+ MYSQL_UNIX_SOCKET='/cloudsql/some-project:some-region:some-instance'
224
+ MYSQL_INSTANCE='some-project:some-region:some-instance' \
225
+ go test -v
226
+ ```
0 commit comments