-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch CloudSQL sample to 2nd gen. Added unit tests. #153
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,89 @@ | ||
# Node.js Cloud SQL sample on Google App Engine | ||
|
||
This sample demonstrates how to use [Cloud SQL](https://cloud.google.com/sql/) | ||
on [Google App Engine Managed VMs](https://cloud.google.com/appengine). | ||
This sample demonstrates how to use [Google Cloud SQL][sql] (or any other SQL | ||
server) on [Google App Engine Flexible][flexible]. | ||
|
||
## Setup | ||
|
||
Before you can run or deploy the sample, you will need to do the following: | ||
|
||
1. Create a Cloud SQL instance. You can do this from the [Google Developers Console](https://console.developers.google.com) | ||
or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK | ||
use the following command: | ||
1. Create a [Second Generation Cloud SQL][gen] instance. You can do this from | ||
the [Cloud Console][console] or via the [Cloud SDK][sdk]. To create it via the | ||
SDK use the following command: | ||
|
||
gcloud sql instances create [your-instance-name] \ | ||
--assign-ip \ | ||
--authorized-networks 0.0.0.0/0 \ | ||
--tier D0 | ||
gcloud sql instances create [YOUR_INSTANCE_NAME] \ | ||
--activation-policy=ALWAYS \ | ||
--tier=db-n1-standard-1 | ||
|
||
where `[YOUR_INSTANCE_NAME]` is a name of your choice. | ||
|
||
1. Set the root password on your Cloud SQL instance: | ||
|
||
gcloud sql instances set-root-password [YOUR_INSTANCE_NAME] --password [YOUR_INSTANCE_ROOT_PASSWORD] | ||
|
||
where `[YOUR_INSTANCE_NAME]` is the name you chose in step 1 and | ||
`[YOUR_INSTANCE_ROOT_PASSWORD]` is a password of your choice. | ||
|
||
1. Create a [Service Account][service] for your project. You will use this | ||
service account to connect to your Cloud SQL instance locally. | ||
|
||
1. Download and install the [Cloud SQL Proxy][proxy]. | ||
|
||
1. [Start the proxy][start] to allow connecting to your instance from your local | ||
machine: | ||
|
||
cloud_sql_proxy \ | ||
-dir /cloudsql \ | ||
-instances=[YOUR_INSTANCE_CONNECTION_NAME] \ | ||
-credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON | ||
|
||
where `[YOUR_INSTANCE_CONNECTION_NAME]` is the connection name of your | ||
instance on its Overview page in the Google Cloud Platform Console, or use | ||
`[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`. | ||
|
||
1. Use the MySQL command line tools (or a management tool of your choice) to | ||
create a [new user][user] and [database][database] for your application: | ||
|
||
mysql --socket [YOUR_SOCKET_PATH] -u root -p | ||
mysql> create database YOUR_DATABASE; | ||
mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD'; | ||
mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%'; | ||
|
||
where `[YOUR_SOCKET_PATH]` is that socket opened by the proxy. This path was | ||
printed to the console when you started the proxy, and is of the format: | ||
`/[DIR]/[YOUR_PROJECT_ID]:[YOUR_REGION]:[YOUR_INSTANCE_NAME]`. | ||
|
||
1. Set the `MYSQL_USER`, `MYSQL_PASSWORD`, `MYSQL_SOCKET_PATH`, and | ||
`MYSQL_DATABASE` environment variables. This allows the app to connect to your | ||
Cloud SQL instance through the proxy. | ||
|
||
1. Create a new user and database for the application. The easiest way to do | ||
this is via the [Google Developers Console](https://console.developers.google.com/project/_/sql/instances/example-instance2/access-control/users). | ||
Alternatively, you can use MySQL tools such as the command line client or | ||
workbench. | ||
1. Update the values in in `app.yaml` with your instance configuration. | ||
1. Finally, run `create_tables.js` to ensure that the database is properly | ||
|
||
1. Finally, run `createTables.js` to ensure that the database is properly | ||
configured and to create the tables needed for the sample. | ||
|
||
## Running locally | ||
|
||
Refer to the [appengine/README.md](../README.md) file for instructions on | ||
running and deploying. | ||
Refer to the [top-level README](../README.md) for instructions on running and deploying. | ||
|
||
To run locally, set the environment variables via your shell before running the | ||
sample: | ||
It's recommended to follow the instructions above to run the Cloud SQL proxy. | ||
You will need to set the following environment variables via your shell before | ||
running the sample: | ||
|
||
export MYSQL_HOST=<your-cloudsql-host> | ||
export MYSQL_USER=<your-cloudsql-user> | ||
export MYSQL_PASSWORD=<your-cloudsql-password> | ||
export MYSQL_DATABASE=<your-cloudsql-database> | ||
export MYSQL_USER="YOUR_USER" | ||
export MYSQL_PASSWORD="YOUR_PASSWORD" | ||
export MYSQL_SOCKET_PATH="YOUR_SOCKET_PATH" | ||
export MYSQL_DATABASE="YOUR_DATABASE" | ||
npm install | ||
npm start | ||
|
||
[sql]: https://cloud.google.com/sql/ | ||
[flexible]: https://cloud.google.com/appengine | ||
[gen]: https://cloud.google.com/sql/docs/create-instance | ||
[console]: https://console.developers.google.com | ||
[sdk]: https://cloud.google.com/sdk | ||
[service]: https://cloud.google.com/sql/docs/external#createServiceAccount | ||
[proxy]: https://cloud.google.com/sql/docs/external#install | ||
[start]: https://cloud.google.com/sql/docs/external#6_start_the_proxy | ||
[user]: https://cloud.google.com/sql/docs/create-user | ||
[database]: https://cloud.google.com/sql/docs/create-database |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2015-2016, Google, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START createTables] | ||
'use strict'; | ||
|
||
// [START setup] | ||
var mysql = require('mysql'); | ||
var prompt = require('prompt'); | ||
// [END setup] | ||
|
||
// [START createTable] | ||
var SQL_STRING = 'CREATE TABLE `visits` (\n' + | ||
' `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,\n' + | ||
' `timestamp` DATETIME NULL,\n' + | ||
' `userIp` VARCHAR(46) NULL,\n' + | ||
' PRIMARY KEY (`id`)\n' + | ||
');'; | ||
|
||
/** | ||
* Create a the "visits" table. | ||
* | ||
* @param {object} connection A mysql connection object. | ||
* @param {function} callback The callback function. | ||
*/ | ||
function createTable (connection, callback) { | ||
connection.query(SQL_STRING, callback); | ||
} | ||
// [END createTable] | ||
|
||
// [START getConnection] | ||
var FIELDS = ['socketPath', 'user', 'password', 'database']; | ||
|
||
/** | ||
* Ask the user for connection configuration and create a new connection. | ||
* | ||
* @param {function} callback The callback function. | ||
*/ | ||
function getConnection (callback) { | ||
prompt.start(); | ||
prompt.get(FIELDS, function (err, config) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
return callback(null, mysql.createConnection({ | ||
user: config.user, | ||
password: config.password, | ||
socketPath: config.socketPath, | ||
database: config.database | ||
})); | ||
}); | ||
} | ||
// [END getConnection] | ||
|
||
// [START main] | ||
getConnection(function (err, connection) { | ||
console.log(err, !!connection); | ||
if (err) { | ||
return console.error(err); | ||
} | ||
createTable(connection, function (err, result) { | ||
console.log(err, !!result); | ||
if (err) { | ||
return console.error(err); | ||
} | ||
console.log(result); | ||
connection.end(); | ||
}); | ||
}); | ||
// [END main] | ||
// [END createTables] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~ Create the "visits" table