diff --git a/.kokoro/appengine/node10/cloudsql.cfg b/.kokoro/appengine/node10/cloudsql-flex.cfg similarity index 83% rename from .kokoro/appengine/node10/cloudsql.cfg rename to .kokoro/appengine/node10/cloudsql-flex.cfg index bb1e51ec17..9675ea117a 100644 --- a/.kokoro/appengine/node10/cloudsql.cfg +++ b/.kokoro/appengine/node10/cloudsql-flex.cfg @@ -3,11 +3,11 @@ # Set the folder in which the tests are run env_vars: { key: "PROJECT" - value: "appengine/cloudsql" + value: "appengine/cloudsql/flexible" } # Tell the trampoline which build file to use. env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/nodejs-docs-samples/.kokoro/build.sh" -} +} diff --git a/.kokoro/appengine/node10/cloudsql_postgresql.cfg b/.kokoro/appengine/node10/cloudsql-standard-basic.cfg similarity index 82% rename from .kokoro/appengine/node10/cloudsql_postgresql.cfg rename to .kokoro/appengine/node10/cloudsql-standard-basic.cfg index 800be905ca..c981163383 100644 --- a/.kokoro/appengine/node10/cloudsql_postgresql.cfg +++ b/.kokoro/appengine/node10/cloudsql-standard-basic.cfg @@ -3,11 +3,11 @@ # Set the folder in which the tests are run env_vars: { key: "PROJECT" - value: "appengine/cloudsql_postgresql" + value: "appengine/cloudsql/standard/basic" } # Tell the trampoline which build file to use. env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/nodejs-docs-samples/.kokoro/build.sh" -} +} diff --git a/.kokoro/appengine/node10/cloudsql-standard-cnx-pooling.cfg b/.kokoro/appengine/node10/cloudsql-standard-cnx-pooling.cfg new file mode 100644 index 0000000000..5af2979639 --- /dev/null +++ b/.kokoro/appengine/node10/cloudsql-standard-cnx-pooling.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql/standard/connection-pooling" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node10/cloudsql_postgresql-flex.cfg b/.kokoro/appengine/node10/cloudsql_postgresql-flex.cfg new file mode 100644 index 0000000000..65ad728d52 --- /dev/null +++ b/.kokoro/appengine/node10/cloudsql_postgresql-flex.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/flexible" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node10/cloudsql_postgresql-standard-basic.cfg b/.kokoro/appengine/node10/cloudsql_postgresql-standard-basic.cfg new file mode 100644 index 0000000000..c0ef537bfb --- /dev/null +++ b/.kokoro/appengine/node10/cloudsql_postgresql-standard-basic.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/standard/basic" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node10/cloudsql_postgresql-standard-cnx-pooling.cfg b/.kokoro/appengine/node10/cloudsql_postgresql-standard-cnx-pooling.cfg new file mode 100644 index 0000000000..829dcdfd16 --- /dev/null +++ b/.kokoro/appengine/node10/cloudsql_postgresql-standard-cnx-pooling.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/standard/connection-pooling" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node8/cloudsql.cfg b/.kokoro/appengine/node6/cloudsql-flex.cfg similarity index 83% rename from .kokoro/appengine/node8/cloudsql.cfg rename to .kokoro/appengine/node6/cloudsql-flex.cfg index bb1e51ec17..9675ea117a 100644 --- a/.kokoro/appengine/node8/cloudsql.cfg +++ b/.kokoro/appengine/node6/cloudsql-flex.cfg @@ -3,11 +3,11 @@ # Set the folder in which the tests are run env_vars: { key: "PROJECT" - value: "appengine/cloudsql" + value: "appengine/cloudsql/flexible" } # Tell the trampoline which build file to use. env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/nodejs-docs-samples/.kokoro/build.sh" -} +} diff --git a/.kokoro/appengine/node6/cloudsql_postgresql-flex.cfg b/.kokoro/appengine/node6/cloudsql_postgresql-flex.cfg new file mode 100644 index 0000000000..65ad728d52 --- /dev/null +++ b/.kokoro/appengine/node6/cloudsql_postgresql-flex.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/flexible" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node6/cloudsql.cfg b/.kokoro/appengine/node8/cloudsql-flex.cfg similarity index 83% rename from .kokoro/appengine/node6/cloudsql.cfg rename to .kokoro/appengine/node8/cloudsql-flex.cfg index bb1e51ec17..9675ea117a 100644 --- a/.kokoro/appengine/node6/cloudsql.cfg +++ b/.kokoro/appengine/node8/cloudsql-flex.cfg @@ -3,11 +3,11 @@ # Set the folder in which the tests are run env_vars: { key: "PROJECT" - value: "appengine/cloudsql" + value: "appengine/cloudsql/flexible" } # Tell the trampoline which build file to use. env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/nodejs-docs-samples/.kokoro/build.sh" -} +} diff --git a/.kokoro/appengine/node6/cloudsql_postgresql.cfg b/.kokoro/appengine/node8/cloudsql-standard-basic.cfg similarity index 82% rename from .kokoro/appengine/node6/cloudsql_postgresql.cfg rename to .kokoro/appengine/node8/cloudsql-standard-basic.cfg index 800be905ca..c981163383 100644 --- a/.kokoro/appengine/node6/cloudsql_postgresql.cfg +++ b/.kokoro/appengine/node8/cloudsql-standard-basic.cfg @@ -3,11 +3,11 @@ # Set the folder in which the tests are run env_vars: { key: "PROJECT" - value: "appengine/cloudsql_postgresql" + value: "appengine/cloudsql/standard/basic" } # Tell the trampoline which build file to use. env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/nodejs-docs-samples/.kokoro/build.sh" -} +} diff --git a/.kokoro/appengine/node8/cloudsql-standard-cnx-pooling.cfg b/.kokoro/appengine/node8/cloudsql-standard-cnx-pooling.cfg new file mode 100644 index 0000000000..5af2979639 --- /dev/null +++ b/.kokoro/appengine/node8/cloudsql-standard-cnx-pooling.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql/standard/connection-pooling" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node8/cloudsql_postgresql-flex.cfg b/.kokoro/appengine/node8/cloudsql_postgresql-flex.cfg new file mode 100644 index 0000000000..65ad728d52 --- /dev/null +++ b/.kokoro/appengine/node8/cloudsql_postgresql-flex.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/flexible" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node8/cloudsql_postgresql-standard-basic.cfg b/.kokoro/appengine/node8/cloudsql_postgresql-standard-basic.cfg new file mode 100644 index 0000000000..c0ef537bfb --- /dev/null +++ b/.kokoro/appengine/node8/cloudsql_postgresql-standard-basic.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/standard/basic" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node8/cloudsql_postgresql-standard-cnx-pooling.cfg b/.kokoro/appengine/node8/cloudsql_postgresql-standard-cnx-pooling.cfg new file mode 100644 index 0000000000..829dcdfd16 --- /dev/null +++ b/.kokoro/appengine/node8/cloudsql_postgresql-standard-cnx-pooling.cfg @@ -0,0 +1,13 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Set the folder in which the tests are run +env_vars: { + key: "PROJECT" + value: "appengine/cloudsql_postgresql/standard/connection-pooling" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-docs-samples/.kokoro/build.sh" +} diff --git a/.kokoro/appengine/node8/cloudsql_postgresql.cfg b/.kokoro/appengine/node8/cloudsql_postgresql.cfg deleted file mode 100644 index 800be905ca..0000000000 --- a/.kokoro/appengine/node8/cloudsql_postgresql.cfg +++ /dev/null @@ -1,13 +0,0 @@ -# Format: //devtools/kokoro/config/proto/build.proto - -# Set the folder in which the tests are run -env_vars: { - key: "PROJECT" - value: "appengine/cloudsql_postgresql" -} - -# Tell the trampoline which build file to use. -env_vars: { - key: "TRAMPOLINE_BUILD_FILE" - value: "github/nodejs-docs-samples/.kokoro/build.sh" -} diff --git a/appengine/cloudsql/README.md b/appengine/cloudsql/flexible/README.md similarity index 63% rename from appengine/cloudsql/README.md rename to appengine/cloudsql/flexible/README.md index 3300ef325b..120cea970f 100644 --- a/appengine/cloudsql/README.md +++ b/appengine/cloudsql/flexible/README.md @@ -3,13 +3,9 @@ This sample application shows how to use [Google Cloud SQL][sql] for [MySQL][mysql] on Google App Engine. -[App Engine standard environment][appengine-std] users: See tutorial [Using Cloud SQL for MySQL (App Engine Standard Environment)][std-tutorial] for more information on running and deploying this app. - [App Engine flexible environment][appengine-flex] users: See tutorial [Using Cloud SQL for MySQL (App Engine Flexible Environment)][flex-tutorial] for more information on running and deploying this app. [sql]: https://cloud.google.com/sql/ [mysql]: https://www.mysql.com/downloads/ [appengine-flex]: https://cloud.google.com/appengine/docs/flexible/nodejs -[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs [flex-tutorial]: https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-sql -[std-tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/using-cloud-sql diff --git a/appengine/cloudsql/app.flexible.yaml b/appengine/cloudsql/flexible/app.yaml similarity index 100% rename from appengine/cloudsql/app.flexible.yaml rename to appengine/cloudsql/flexible/app.yaml diff --git a/appengine/cloudsql/createTables.js b/appengine/cloudsql/flexible/createTables.js similarity index 100% rename from appengine/cloudsql/createTables.js rename to appengine/cloudsql/flexible/createTables.js diff --git a/appengine/cloudsql/package.json b/appengine/cloudsql/flexible/package.json similarity index 96% rename from appengine/cloudsql/package.json rename to appengine/cloudsql/flexible/package.json index 895806ce13..cbe12270c5 100644 --- a/appengine/cloudsql/package.json +++ b/appengine/cloudsql/flexible/package.json @@ -1,6 +1,6 @@ { - "name": "appengine-cloudsql-mysql", - "description": "Node.js MySQL sample for Cloud SQL on App Engine.", + "name": "appengine-cloudsql-mysql-flexible", + "description": "Node.js MySQL sample for Cloud SQL on App Engine flexible.", "version": "0.0.1", "private": true, "license": "Apache-2.0", diff --git a/appengine/cloudsql/server.js b/appengine/cloudsql/flexible/server.js similarity index 100% rename from appengine/cloudsql/server.js rename to appengine/cloudsql/flexible/server.js diff --git a/appengine/cloudsql/test/createTables.test.js b/appengine/cloudsql/flexible/test/createTables.test.js similarity index 100% rename from appengine/cloudsql/test/createTables.test.js rename to appengine/cloudsql/flexible/test/createTables.test.js diff --git a/appengine/cloudsql/test/server.test.js b/appengine/cloudsql/flexible/test/server.test.js similarity index 100% rename from appengine/cloudsql/test/server.test.js rename to appengine/cloudsql/flexible/test/server.test.js diff --git a/appengine/cloudsql/standard/basic/README.md b/appengine/cloudsql/standard/basic/README.md new file mode 100644 index 0000000000..9731d40faf --- /dev/null +++ b/appengine/cloudsql/standard/basic/README.md @@ -0,0 +1,11 @@ +# Cloud SQL for MySQL Node.js sample on App Engine standard environment + +This sample application shows how to use [Google Cloud SQL][sql] for [MySQL][mysql] +on Google App Engine. + +See [Using Cloud SQL for MySQL (App Engine Standard Environment)][std-tutorial] for more information. + +[sql]: https://cloud.google.com/sql/ +[mysql]: https://www.mysql.com/downloads/ +[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs +[std-tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/using-cloud-sql diff --git a/appengine/cloudsql/app.standard.yaml b/appengine/cloudsql/standard/basic/app.yaml similarity index 78% rename from appengine/cloudsql/app.standard.yaml rename to appengine/cloudsql/standard/basic/app.yaml index 62dfbbb386..eeb59b5fe5 100644 --- a/appengine/cloudsql/app.standard.yaml +++ b/appengine/cloudsql/standard/basic/app.yaml @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -runtime: nodejs8 +runtime: nodejs10 # [START gae_mysql_env] # The following env variables may contain sensitive information that grants @@ -23,9 +23,3 @@ env_variables: # e.g. my-awesome-project:us-central1:my-cloud-sql-instance INSTANCE_CONNECTION_NAME: YOUR_INSTANCE_CONNECTION_NAME # [END gae_mysql_env] - -beta_settings: - # The connection name of your instance, available by using - # 'gcloud beta sql instances describe [INSTANCE_NAME]' or from - # the Instance details page in the Google Cloud Platform Console. - cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME diff --git a/appengine/cloudsql/standard/basic/package.json b/appengine/cloudsql/standard/basic/package.json new file mode 100644 index 0000000000..08d8b79449 --- /dev/null +++ b/appengine/cloudsql/standard/basic/package.json @@ -0,0 +1,62 @@ +{ + "name": "appengine-cloudsql-mysql-standard", + "description": "Node.js MySQL sample for Cloud SQL on App Engine standard.", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "lint": "repo-tools lint", + "pretest": "npm run lint", + "unit-test": "ava --verbose test/*.test.js", + "start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -instances=$INSTANCE_CONNECTION_NAME=tcp:$SQL_PORT &", + "system-test": "repo-tools test app -- server.js", + "system-test-proxy": "npm run start-proxy; npm run system-test", + "all-test": "npm run unit-test && npm run system-test", + "test": "repo-tools test run --cmd npm -- run all-test" + }, + "dependencies": { + "express": "4.16.3", + "mysql": "2.15.0" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "2.3.0", + "ava": "0.25.0", + "semistandard": "^12.0.1" + }, + "cloud-repo-tools": { + "requiresKeyFile": true, + "requiresProjectId": true, + "test": { + "app": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ], + "msg": "Current time:", + "args": [ + "server.js" + ] + }, + "build": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ] + } + } + } +} diff --git a/appengine/cloudsql/standard/basic/server.js b/appengine/cloudsql/standard/basic/server.js new file mode 100644 index 0000000000..f5ade916f7 --- /dev/null +++ b/appengine/cloudsql/standard/basic/server.js @@ -0,0 +1,63 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require('express'); +const mysql = require('mysql'); + +const app = express(); + +const dbConfig = { + user: process.env.SQL_USER, + password: process.env.SQL_PASSWORD, + database: process.env.SQL_DATABASE +}; + +// When deployed to App Engine, the `NODE_ENV` environment variable will be +// set to `production`. +if (process.env.INSTANCE_CONNECTION_NAME && + process.env.NODE_ENV === 'production') { + // If deployed, use the local socket interface for accessing Cloud SQL. + // If running locally, use the the TCP connection instead. + // mysql package by default connects to localhost:3306. + // Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) + // so that your application can use localhost:3306 to connect to your + // Cloud SQL instance. + dbConfig.socketPath = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`; +} + +app.get(`/`, (req, res, next) => { + var connection = mysql.createConnection(dbConfig); + connection.query( + `SELECT NOW() as now`, + function (error, results, fields) { + if (error) throw error; + var now = results[0].now; + res + .status(200) + .send(`Current time: ${now}`) + .end(); + } + ); +}); + +const port = process.env.PORT || 8080; +app.listen(port, () => { + console.log(`App listening on port ${port}`); + console.log('Press Ctrl+C to quit.'); +}); + +module.exports = app; diff --git a/appengine/cloudsql/standard/basic/test/server.test.js b/appengine/cloudsql/standard/basic/test/server.test.js new file mode 100644 index 0000000000..d65254a6ee --- /dev/null +++ b/appengine/cloudsql/standard/basic/test/server.test.js @@ -0,0 +1,65 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require(`express`); +const path = require(`path`); +const proxyquire = require(`proxyquire`).noCallThru(); +const request = require(`supertest`); +const sinon = require(`sinon`); +const test = require(`ava`); +const tools = require(`@google-cloud/nodejs-repo-tools`); + +const SAMPLE_PATH = path.join(__dirname, `../server.js`); + +function getSample () { + const testApp = express(); + sinon.stub(testApp, `listen`).yields(); + const expressMock = sinon.stub().returns(testApp); + const mysqlMock = { + createConnection: sinon.stub().returns({ + query: sinon.stub().yields(null, [{now: `0`}], null) + }) + }; + + const app = proxyquire(SAMPLE_PATH, { + mysql: mysqlMock, + express: expressMock + }); + + return { + app: app, + mocks: { + express: expressMock, + mysql: mysqlMock + } + }; +} + +test.beforeEach(tools.stubConsole); +test.afterEach.always(tools.restoreConsole); + +test.cb(`should return current time`, (t) => { + const sample = getSample(); + + request(sample.app) + .get(`/`) + .expect(200) + .expect(res => { + t.is(res.text, `Current time: 0`); + }) + .end(t.end); +}); diff --git a/appengine/cloudsql/standard/connection-pooling/README.md b/appengine/cloudsql/standard/connection-pooling/README.md new file mode 100644 index 0000000000..8c519721d8 --- /dev/null +++ b/appengine/cloudsql/standard/connection-pooling/README.md @@ -0,0 +1,11 @@ +# Cloud SQL for MySQL Node.js sample on App Engine standard environment + +This sample application shows how to use [Google Cloud SQL][sql] for [MySQL][mysql] +on Google App Engine with connection pooling enabled. + +See [Using Cloud SQL for MySQL (App Engine Standard Environment)][std-tutorial] for more information. + +[sql]: https://cloud.google.com/sql/ +[mysql]: https://www.mysql.com/downloads/ +[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs +[std-tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/using-cloud-sql diff --git a/appengine/cloudsql/standard/connection-pooling/app.yaml b/appengine/cloudsql/standard/connection-pooling/app.yaml new file mode 100644 index 0000000000..eeb59b5fe5 --- /dev/null +++ b/appengine/cloudsql/standard/connection-pooling/app.yaml @@ -0,0 +1,25 @@ +# Copyright 2017, 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. + +runtime: nodejs10 + +# [START gae_mysql_env] +# The following env variables may contain sensitive information that grants +# anyone access to your database. Do not add this file to your source control. +env_variables: + SQL_USER: YOUR_SQL_USER + SQL_PASSWORD: YOUR_SQL_PASSWORD + SQL_DATABASE: YOUR_SQL_DATABASE + # e.g. my-awesome-project:us-central1:my-cloud-sql-instance + INSTANCE_CONNECTION_NAME: YOUR_INSTANCE_CONNECTION_NAME +# [END gae_mysql_env] diff --git a/appengine/cloudsql/standard/connection-pooling/package.json b/appengine/cloudsql/standard/connection-pooling/package.json new file mode 100644 index 0000000000..36a451c512 --- /dev/null +++ b/appengine/cloudsql/standard/connection-pooling/package.json @@ -0,0 +1,62 @@ +{ + "name": "appengine-cloudsql-mysql-standard-cnx-pooling", + "description": "Node.js MySQL sample for Cloud SQL on App Engine standard.", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "lint": "repo-tools lint", + "pretest": "npm run lint", + "unit-test": "ava --verbose test/*.test.js", + "start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -instances=$INSTANCE_CONNECTION_NAME=tcp:$SQL_PORT &", + "system-test": "repo-tools test app -- server.js", + "system-test-proxy": "npm run start-proxy; npm run system-test", + "all-test": "npm run unit-test && npm run system-test", + "test": "repo-tools test run --cmd npm -- run all-test" + }, + "dependencies": { + "express": "4.16.3", + "mysql": "2.15.0" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "2.3.0", + "ava": "0.25.0", + "semistandard": "^12.0.1" + }, + "cloud-repo-tools": { + "requiresKeyFile": true, + "requiresProjectId": true, + "test": { + "app": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ], + "msg": "Current time:", + "args": [ + "server.js" + ] + }, + "build": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ] + } + } + } +} diff --git a/appengine/cloudsql/standard/connection-pooling/server.js b/appengine/cloudsql/standard/connection-pooling/server.js new file mode 100644 index 0000000000..0966df4976 --- /dev/null +++ b/appengine/cloudsql/standard/connection-pooling/server.js @@ -0,0 +1,66 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require('express'); +const mysql = require('mysql'); + +const app = express(); + +const poolConfig = { + connectionLimit: 3, + user: process.env.SQL_USER, + password: process.env.SQL_PASSWORD, + database: process.env.SQL_DATABASE +}; + +if (process.env.INSTANCE_CONNECTION_NAME && + process.env.NODE_ENV === 'production') { + // If deployed, use the local socket interface for accessing Cloud SQL. + // If running locally, use the the TCP connection instead. + // mysql package by default connects to localhost:3306. + // Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) + // so that your application can use localhost:3306 to connect to your + // Cloud SQL instance. + poolConfig.socketPath = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`; +} + +var pool = mysql.createPool(poolConfig); + +app.get(`/`, (req, res, next) => { + // pool.query() method from the mysql package automatically requests a + // connection from the pool, run the query, and return the connection back + // to the pool. + pool.query( + `SELECT NOW() as now`, + function (error, results, fields) { + if (error) throw error; + var now = results[0].now; + res + .status(200) + .send(`Current time: ${now}`) + .end(); + } + ); +}); + +const port = process.env.PORT || 8080; +app.listen(port, () => { + console.log(`App listening on port ${port}`); + console.log('Press Ctrl+C to quit.'); +}); + +module.exports = app; diff --git a/appengine/cloudsql/standard/connection-pooling/test/server.test.js b/appengine/cloudsql/standard/connection-pooling/test/server.test.js new file mode 100644 index 0000000000..ece4ca555a --- /dev/null +++ b/appengine/cloudsql/standard/connection-pooling/test/server.test.js @@ -0,0 +1,65 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require(`express`); +const path = require(`path`); +const proxyquire = require(`proxyquire`).noCallThru(); +const request = require(`supertest`); +const sinon = require(`sinon`); +const test = require(`ava`); +const tools = require(`@google-cloud/nodejs-repo-tools`); + +const SAMPLE_PATH = path.join(__dirname, `../server.js`); + +function getSample () { + const testApp = express(); + sinon.stub(testApp, `listen`).yields(); + const expressMock = sinon.stub().returns(testApp); + const mysqlMock = { + createPool: sinon.stub().returns({ + query: sinon.stub().yields(null, [{now: `0`}], null) + }) + }; + + const app = proxyquire(SAMPLE_PATH, { + mysql: mysqlMock, + express: expressMock + }); + + return { + app: app, + mocks: { + express: expressMock, + mysql: mysqlMock + } + }; +} + +test.beforeEach(tools.stubConsole); +test.afterEach.always(tools.restoreConsole); + +test.cb(`should return current time`, (t) => { + const sample = getSample(); + + request(sample.app) + .get(`/`) + .expect(200) + .expect(res => { + t.is(res.text, `Current time: 0`); + }) + .end(t.end); +}); diff --git a/appengine/cloudsql_postgresql/README.md b/appengine/cloudsql_postgresql/flexible/README.md similarity index 100% rename from appengine/cloudsql_postgresql/README.md rename to appengine/cloudsql_postgresql/flexible/README.md diff --git a/appengine/cloudsql_postgresql/app.flexible.yaml b/appengine/cloudsql_postgresql/flexible/app.yaml similarity index 100% rename from appengine/cloudsql_postgresql/app.flexible.yaml rename to appengine/cloudsql_postgresql/flexible/app.yaml diff --git a/appengine/cloudsql_postgresql/createTables.js b/appengine/cloudsql_postgresql/flexible/createTables.js similarity index 100% rename from appengine/cloudsql_postgresql/createTables.js rename to appengine/cloudsql_postgresql/flexible/createTables.js diff --git a/appengine/cloudsql_postgresql/package.json b/appengine/cloudsql_postgresql/flexible/package.json similarity index 96% rename from appengine/cloudsql_postgresql/package.json rename to appengine/cloudsql_postgresql/flexible/package.json index 17b1598262..2fdcedc7b1 100644 --- a/appengine/cloudsql_postgresql/package.json +++ b/appengine/cloudsql_postgresql/flexible/package.json @@ -1,6 +1,6 @@ { - "name": "appengine-cloudsql-postgres", - "description": "Node.js PostgreSQL sample for Cloud SQL on App Engine.", + "name": "appengine-cloudsql-postgres-flexible", + "description": "Node.js PostgreSQL sample for Cloud SQL on App Engine flexible.", "version": "0.0.1", "private": true, "license": "Apache-2.0", diff --git a/appengine/cloudsql_postgresql/server.js b/appengine/cloudsql_postgresql/flexible/server.js similarity index 100% rename from appengine/cloudsql_postgresql/server.js rename to appengine/cloudsql_postgresql/flexible/server.js diff --git a/appengine/cloudsql_postgresql/test/createTables.test.js b/appengine/cloudsql_postgresql/flexible/test/createTables.test.js similarity index 100% rename from appengine/cloudsql_postgresql/test/createTables.test.js rename to appengine/cloudsql_postgresql/flexible/test/createTables.test.js diff --git a/appengine/cloudsql_postgresql/test/server.test.js b/appengine/cloudsql_postgresql/flexible/test/server.test.js similarity index 100% rename from appengine/cloudsql_postgresql/test/server.test.js rename to appengine/cloudsql_postgresql/flexible/test/server.test.js diff --git a/appengine/cloudsql_postgresql/standard/basic/README.md b/appengine/cloudsql_postgresql/standard/basic/README.md new file mode 100644 index 0000000000..badead7b14 --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/basic/README.md @@ -0,0 +1,11 @@ +# Cloud SQL for PostgreSQL Node.js sample on App Engine standard environment + +This sample application shows how to use [Google Cloud SQL[[sql] for [PostgreSQL][postgres] +on Google App Engine. + +See tutorial [Using Cloud SQL for PostgreSQL (App Engine Standard Environment)][std-tutorial] for more information. + +[sql]: https://cloud.google.com/sql/ +[postgres]: https://www.postgresql.org/download/ +[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs +[std-tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/using-cloud-sql-postgres diff --git a/appengine/cloudsql_postgresql/app.standard.yaml b/appengine/cloudsql_postgresql/standard/basic/app.yaml similarity index 78% rename from appengine/cloudsql_postgresql/app.standard.yaml rename to appengine/cloudsql_postgresql/standard/basic/app.yaml index 7f80395609..9617438f73 100644 --- a/appengine/cloudsql_postgresql/app.standard.yaml +++ b/appengine/cloudsql_postgresql/standard/basic/app.yaml @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -runtime: nodejs8 +runtime: nodejs10 # [START gae_postgres_env] # The following env variables may contain sensitive information that grants @@ -23,9 +23,3 @@ env_variables: # e.g. my-awesome-project:us-central1:my-cloud-sql-instance INSTANCE_CONNECTION_NAME: YOUR_INSTANCE_CONNECTION_NAME # [END gae_postgres_env] - -beta_settings: - # The connection name of your instance, available by using - # 'gcloud beta sql instances describe [INSTANCE_NAME]' or from - # the Instance details page in the Google Cloud Platform Console. - cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME diff --git a/appengine/cloudsql_postgresql/standard/basic/package.json b/appengine/cloudsql_postgresql/standard/basic/package.json new file mode 100644 index 0000000000..1bccb7746a --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/basic/package.json @@ -0,0 +1,62 @@ +{ + "name": "appengine-cloudsql-postgres-standard", + "description": "Node.js PostgreSQL sample for Cloud SQL on App Engine standard.", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "lint": "repo-tools lint", + "pretest": "npm run lint", + "unit-test": "ava --verbose test/*.test.js", + "start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -instances=$INSTANCE_CONNECTION_NAME=tcp:$SQL_PORT &", + "system-test": "repo-tools test app -- server.js", + "system-test-proxy": "npm run start-proxy; npm run system-test", + "all-test": "npm run unit-test && npm run system-test", + "test": "repo-tools test run --cmd npm -- run all-test" + }, + "dependencies": { + "express": "4.16.2", + "pg": "7.4.1" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "2.2.1", + "ava": "^0.25.0", + "semistandard": "^12.0.1" + }, + "cloud-repo-tools": { + "requiresKeyFile": true, + "requiresProjectId": true, + "test": { + "app": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ], + "msg": "Current time:", + "args": [ + "server.js" + ] + }, + "build": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ] + } + } + } +} diff --git a/appengine/cloudsql_postgresql/standard/basic/server.js b/appengine/cloudsql_postgresql/standard/basic/server.js new file mode 100644 index 0000000000..bbed790c7d --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/basic/server.js @@ -0,0 +1,65 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require('express'); +const pg = require('pg'); + +const app = express(); + +const dbConfig = { + user: process.env.SQL_USER, + password: process.env.SQL_PASSWORD, + database: process.env.SQL_DATABASE +}; + +// When deployed to App Engine, the `NODE_ENV` environment variable will be +// set to `production`. +if (process.env.INSTANCE_CONNECTION_NAME && + process.env.NODE_ENV === 'production') { + // If deployed, use the local socket interface for accessing Cloud SQL. + // If running locally, use the the TCP connection instead. + // mysql package by default connects to localhost:3306. + // Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) + // so that your application can use localhost:3306 to connect to your + // Cloud SQL instance. + dbConfig.socketPath = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`; +} + +app.get(`/`, (req, res, next) => { + var client = new pg.Client(dbConfig); + client.connect(); + client + .query(`SELECT NOW() as now`) + .then(result => { + var now = result.rows[0].now; + res + .status(200) + .send(`Current time: ${now}`) + .end(); + }) + .catch(err => { + next(err); + }); +}); + +const port = process.env.PORT || 8080; +app.listen(port, () => { + console.log(`App listening on port ${port}`); + console.log('Press Ctrl+C to quit.'); +}); + +module.exports = app; diff --git a/appengine/cloudsql_postgresql/standard/basic/test/server.test.js b/appengine/cloudsql_postgresql/standard/basic/test/server.test.js new file mode 100644 index 0000000000..01f957b34f --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/basic/test/server.test.js @@ -0,0 +1,72 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require(`express`); +const path = require(`path`); +const proxyquire = require(`proxyquire`).noCallThru(); +const request = require(`supertest`); +const sinon = require(`sinon`); +const test = require(`ava`); +const tools = require(`@google-cloud/nodejs-repo-tools`); + +const SAMPLE_PATH = path.join(__dirname, `../server.js`); + +function getSample () { + const testApp = express(); + sinon.stub(testApp, `listen`).yields(); + const expressMock = sinon.stub().returns(testApp); + const pgMock = { + Client: class { + constructor () { + this.connect = sinon.stub().returnsThis(); + this.query = sinon.stub().returns( + Promise.resolve({ + rows: [{now: `0`}] + }) + ); + } + } + }; + + const app = proxyquire(SAMPLE_PATH, { + pg: pgMock, + express: expressMock + }); + + return { + app: app, + mocks: { + express: expressMock, + pg: pgMock + } + }; +} + +test.beforeEach(tools.stubConsole); +test.afterEach.always(tools.restoreConsole); + +test.cb(`should return current time`, (t) => { + const sample = getSample(); + + request(sample.app) + .get(`/`) + .expect(200) + .expect(res => { + t.is(res.text, `Current time: 0`); + }) + .end(t.end); +}); diff --git a/appengine/cloudsql_postgresql/standard/connection-pooling/README.md b/appengine/cloudsql_postgresql/standard/connection-pooling/README.md new file mode 100644 index 0000000000..7995d1a0a7 --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/connection-pooling/README.md @@ -0,0 +1,11 @@ +# Cloud SQL for PostgreSQL Node.js sample on App Engine standard environment + +This sample application shows how to use [Google Cloud SQL[[sql] for [PostgreSQL][postgres] +on Google App Engine with connection pooling. + +See tutorial [Using Cloud SQL for PostgreSQL (App Engine Standard Environment)][std-tutorial] for more information. + +[sql]: https://cloud.google.com/sql/ +[postgres]: https://www.postgresql.org/download/ +[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs +[std-tutorial]: https://cloud.google.com/appengine/docs/standard/nodejs/using-cloud-sql-postgres diff --git a/appengine/cloudsql_postgresql/standard/connection-pooling/app.yaml b/appengine/cloudsql_postgresql/standard/connection-pooling/app.yaml new file mode 100644 index 0000000000..9617438f73 --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/connection-pooling/app.yaml @@ -0,0 +1,25 @@ +# Copyright 2017, 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. + +runtime: nodejs10 + +# [START gae_postgres_env] +# The following env variables may contain sensitive information that grants +# anyone access to your database. Do not add this file to your source control. +env_variables: + SQL_USER: YOUR_SQL_USER + SQL_PASSWORD: YOUR_SQL_PASSWORD + SQL_DATABASE: YOUR_SQL_DATABASE + # e.g. my-awesome-project:us-central1:my-cloud-sql-instance + INSTANCE_CONNECTION_NAME: YOUR_INSTANCE_CONNECTION_NAME +# [END gae_postgres_env] diff --git a/appengine/cloudsql_postgresql/standard/connection-pooling/package.json b/appengine/cloudsql_postgresql/standard/connection-pooling/package.json new file mode 100644 index 0000000000..bb840f239d --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/connection-pooling/package.json @@ -0,0 +1,62 @@ +{ + "name": "appengine-cloudsql-postgres-standard-cnx-pooling", + "description": "Node.js PostgreSQL sample for Cloud SQL on App Engine standard.", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "lint": "repo-tools lint", + "pretest": "npm run lint", + "unit-test": "ava --verbose test/*.test.js", + "start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -instances=$INSTANCE_CONNECTION_NAME=tcp:$SQL_PORT &", + "system-test": "repo-tools test app -- server.js", + "system-test-proxy": "npm run start-proxy; npm run system-test", + "all-test": "npm run unit-test && npm run system-test", + "test": "repo-tools test run --cmd npm -- run all-test" + }, + "dependencies": { + "express": "4.16.2", + "pg": "7.4.1" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "2.2.1", + "ava": "0.25.0", + "semistandard": "^12.0.1" + }, + "cloud-repo-tools": { + "requiresKeyFile": true, + "requiresProjectId": true, + "test": { + "app": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ], + "msg": "Current time:", + "args": [ + "server.js" + ] + }, + "build": { + "requiredEnvVars": [ + "SQL_USER", + "SQL_PASSWORD", + "SQL_DATABASE", + "SQL_PORT", + "INSTANCE_CONNECTION_NAME" + ] + } + } + } +} diff --git a/appengine/cloudsql_postgresql/standard/connection-pooling/server.js b/appengine/cloudsql_postgresql/standard/connection-pooling/server.js new file mode 100644 index 0000000000..48b56f3c1c --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/connection-pooling/server.js @@ -0,0 +1,68 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require('express'); +const pg = require('pg'); + +const app = express(); + +const poolConfig = { + user: process.env.SQL_USER, + password: process.env.SQL_PASSWORD, + database: process.env.SQL_DATABASE +}; + +// When deployed to App Engine, the `NODE_ENV` environment variable will be +// set to `production`. +if (process.env.INSTANCE_CONNECTION_NAME && + process.env.NODE_ENV === 'production') { + // If deployed, use the local socket interface for accessing Cloud SQL. + // If running locally, use the the TCP connection instead. + // mysql package by default connects to localhost:3306. + // Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) + // so that your application can use localhost:3306 to connect to your + // Cloud SQL instance. + poolConfig.socketPath = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`; +} + +const pool = new pg.Pool(poolConfig); + +app.get(`/`, (req, res, next) => { + // pool.query() method from the pg package automatically requests a + // connection from the pool, run the query, and return the connection back + // to the pool. + pool + .query(`SELECT NOW() as now`) + .then(result => { + var now = result.rows[0].now; + res + .status(200) + .send(`Current time: ${now}`) + .end(); + }) + .catch(err => { + next(err); + }); +}); + +const port = process.env.PORT || 8080; +app.listen(port, () => { + console.log(`App listening on port ${port}`); + console.log('Press Ctrl+C to quit.'); +}); + +module.exports = app; diff --git a/appengine/cloudsql_postgresql/standard/connection-pooling/test/server.test.js b/appengine/cloudsql_postgresql/standard/connection-pooling/test/server.test.js new file mode 100644 index 0000000000..0fa43f283b --- /dev/null +++ b/appengine/cloudsql_postgresql/standard/connection-pooling/test/server.test.js @@ -0,0 +1,71 @@ +/** + * Copyright 2018, Google, LLC. + * 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. + */ + +'use strict'; + +const express = require(`express`); +const path = require(`path`); +const proxyquire = require(`proxyquire`).noCallThru(); +const request = require(`supertest`); +const sinon = require(`sinon`); +const test = require(`ava`); +const tools = require(`@google-cloud/nodejs-repo-tools`); + +const SAMPLE_PATH = path.join(__dirname, `../server.js`); + +function getSample () { + const testApp = express(); + sinon.stub(testApp, `listen`).yields(); + const expressMock = sinon.stub().returns(testApp); + const pgMock = { + Pool: class { + constructor () { + this.query = sinon.stub().returns( + Promise.resolve({ + rows: [{now: `0`}] + }) + ); + } + } + }; + + const app = proxyquire(SAMPLE_PATH, { + pg: pgMock, + express: expressMock + }); + + return { + app: app, + mocks: { + express: expressMock, + pg: pgMock + } + }; +} + +test.beforeEach(tools.stubConsole); +test.afterEach.always(tools.restoreConsole); + +test.cb(`should return current time`, (t) => { + const sample = getSample(); + + request(sample.app) + .get(`/`) + .expect(200) + .expect(res => { + t.is(res.text, `Current time: 0`); + }) + .end(t.end); +});