Skip to content

Commit

Permalink
Merge branch 'master' into gcf-kokoro-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri authored Sep 17, 2018
2 parents a0cc7a4 + 7eb558a commit aeb6859
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Follow either of the two links above to access the appropriate CLA and
instructions for how to sign and return it. Once we receive it, we'll be able to
accept your pull requests.

## Setting Up An Environment
For instructions regarding development environment setup, please visit [the documentation](https://cloud.google.com/nodejs/docs/setup).

## Contributing A Patch

1. Submit an issue describing your proposed change to the repo in question.
Expand Down
35 changes: 22 additions & 13 deletions appengine/headless-chrome/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,41 @@ const express = require('express');
const puppeteer = require('puppeteer');
const app = express();

let browser;

async function init () {
// [START browser]
browser = await puppeteer.launch({
args: ['--no-sandbox']
});
// [END browser]

const server = app.listen(process.env.PORT || 8080, async err => {
if (err) {
await browser.close();
return console.error(err);
}
const port = server.address().port;
console.info(`App listening on port ${port}`);
});
}

init();

app.use(async (req, res) => {
const url = req.query.url;

if (!url) {
return res.send('Please provide URL as GET parameter, for example: <a href="/?url=https://example.com">?url=https://example.com</a>');
}

// [START browser]
const browser = await puppeteer.launch({
args: ['--no-sandbox']
});
// [END browser]
const page = await browser.newPage();
let page = await browser.newPage();
await page.goto(url);
const imageBuffer = await page.screenshot();
browser.close();

res.set('Content-Type', 'image/png');
res.send(imageBuffer);
});

const server = app.listen(process.env.PORT || 8080, err => {
if (err) return console.error(err);
const port = server.address().port;
console.info(`App listening on port ${port}`);
});
// [END full_sample]

module.exports = app;
2 changes: 1 addition & 1 deletion auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const cli = require(`yargs`)
)
.command(
`auth-cloud-explicit`,
`Loads credentials implicitly.`,
`Loads credentials explicitly.`,
{},
authCloudExplicit
)
Expand Down
28 changes: 26 additions & 2 deletions functions/sql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ if (process.env.NODE_ENV === 'production') {
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}

const mysqlPool = mysql.createPool(mysqlConfig);
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;

exports.mysqlDemo = (req, res) => {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!mysqlPool) {
mysqlPool = mysql.createPool(mysqlConfig);
}

mysqlPool.query('SELECT NOW() AS now', (err, results) => {
if (err) {
console.error(err);
Expand All @@ -61,6 +70,9 @@ exports.mysqlDemo = (req, res) => {
res.send(JSON.stringify(results));
}
});

// Close any SQL resources that were declared inside this function.
// Keep any declared in global scope (e.g. mysqlPool) for later reuse.
};
// [END functions_sql_mysql]

Expand All @@ -76,9 +88,18 @@ if (process.env.NODE_ENV === 'production') {
pgConfig.socketPath = `/cloudsql/${connectionName}`;
}

const pgPool = new pg.Pool(pgConfig);
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let pgPool;

exports.postgresDemo = (req, res) => {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!pgPool) {
pgPool = new pg.Pool(pgConfig);
}

pgPool.query('SELECT NOW() as now', (err, results) => {
if (err) {
console.error(err);
Expand All @@ -87,5 +108,8 @@ exports.postgresDemo = (req, res) => {
res.send(JSON.stringify(results));
}
});

// Close any SQL resources that were declared inside this function.
// Keep any declared in global scope (e.g. mysqlPool) for later reuse.
};
// [END functions_sql_postgres]

0 comments on commit aeb6859

Please sign in to comment.