Skip to content

Commit

Permalink
Switch CloudSQL sample to 2nd gen. Add units tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Jul 19, 2016
1 parent dc54846 commit b511bd3
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 162 deletions.
93 changes: 71 additions & 22 deletions appengine/cloudsql/README.md
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
79 changes: 0 additions & 79 deletions appengine/cloudsql/app.js

This file was deleted.

19 changes: 13 additions & 6 deletions appengine/cloudsql/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ vm: true

# [START env]
env_variables:
# Replace user, password, and host with the values obtained when
# configuring your Cloud SQL instance.
MYSQL_HOST: <your-cloudsql-host>
MYSQL_USER: <your-cloudsql-user>
MYSQL_PASSWORD: <your-cloudsql-password>
MYSQL_DATABASE: <your-cloudsql-database>
MYSQL_USER: YOUR_USER
MYSQL_PASSWORD: YOUR_PASSWORD
MYSQL_DATABASE: YOUR_DATABASE
# 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]`
MYSQL_SOCKET_PATH: YOUR_SOCKET_PATH
# [END env]

# [START cloudsql_settings]
beta_settings:
# 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]`
cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME
# [END cloudsql_settings]
# [END app_yaml]
83 changes: 83 additions & 0 deletions appengine/cloudsql/createTables.js
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]
45 changes: 0 additions & 45 deletions appengine/cloudsql/create_tables.js

This file was deleted.

11 changes: 3 additions & 8 deletions appengine/cloudsql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
"engines": {
"node": "~4.2"
},
"scripts": {
"start": "node app.js",
"monitor": "nodemon app.js",
"deploy": "gcloud app deploy"
},
"dependencies": {
"express": "^4.13.4",
"mysql": "^2.10.2",
"prompt": "^0.2.14"
"express": "^4.14.0",
"mysql": "^2.11.1",
"prompt": "^1.0.0"
}
}
Loading

0 comments on commit b511bd3

Please sign in to comment.