diff --git a/functions/README.md b/functions/README.md index 02f526e4a8..f604a8b184 100644 --- a/functions/README.md +++ b/functions/README.md @@ -24,6 +24,7 @@ environment. * [Cloud Storage](gcs/) * [Cloud Datastore](datastore/) * [Cloud Pub/Sub](pubsub/) -* [Dependencies](uid/) +* [Dependencies](uuid/) +* [HTTP](http/) * [Logging](log/) * [Modules](module/) diff --git a/functions/message/README.md b/functions/background/README.md similarity index 100% rename from functions/message/README.md rename to functions/background/README.md diff --git a/functions/background/index.js b/functions/background/index.js new file mode 100644 index 0000000000..0f8df69834 --- /dev/null +++ b/functions/background/index.js @@ -0,0 +1,49 @@ +// Copyright 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. + +'use strict'; + +// [START helloworld] +/** + * Background Cloud Function. + * + * @param {Object} context Cloud Function context. + * @param {Object} data Request data, provided by a trigger. + */ +exports.helloworld = function helloworld (context, data) { + if (data.message === undefined) { + // This is an error case, "message" is required + context.failure('No message defined!'); + } else { + // Everything is ok + console.log(data.message); + context.success(); + } +}; +// [END helloworld] + +// [START helloPromise] +var request = require('request-promise'); + +/** + * Background Cloud Function that returns a Promise. + * + * @param {Object} data Request data, provided by a trigger. + * @returns {Promise} + */ +exports.helloPromise = function helloPromise (data) { + return request({ + uri: data.endpoint + }); +}; +// [END helloPromise] diff --git a/functions/background/package.json b/functions/background/package.json new file mode 100644 index 0000000000..dda9a497b0 --- /dev/null +++ b/functions/background/package.json @@ -0,0 +1,15 @@ +{ + "name": "nodejs-docs-samples-functions", + "description": "Node.js samples found on https://cloud.google.com", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "dependencies": { + "request-promise": "^3.0.0" + } +} diff --git a/functions/helloworld/index.js b/functions/helloworld/index.js index 66ae47f49f..f16f576f0f 100644 --- a/functions/helloworld/index.js +++ b/functions/helloworld/index.js @@ -14,7 +14,62 @@ 'use strict'; // [START helloworld] -exports.helloworld = function (context, data) { - context.success('Hello World!'); +/** + * Cloud Function. + * + * @param {Object} context Cloud Function context. + * @param {Object} data Request data, provided by a trigger. + */ +exports.helloworld = function helloworld (context, data) { + console.log('My Cloud Function: ' + data.message); + context.success(); }; // [END helloworld] + +// [START helloHttp] +/** + * HTTP Cloud Function. + * + * @param {Object} req Cloud Function request context. + * @param {Object} res Cloud Function response context. + */ +exports.helloHttp = function helloHttp (req, res) { + res.send('Hello ' + (req.body.name || 'World') + '!'); +}; +// [END helloHttp] + +// [START helloBackground] +/** + * Background Cloud Function. + * + * @param {Object} context Cloud Function context. + * @param {Object} data Request data, provided by a trigger. + */ +exports.helloBackground = function helloBackground (context, data) { + context.success('Hello ' + (data.name || 'World') + '!'); +}; +// [END helloBackground] + +// [START helloPubSub] +/** + * Background Cloud Function to be triggered by Pub/Sub. + * + * @param {Object} context Cloud Function context. + * @param {Object} data Request data, provided by a Pub/Sub trigger. + */ +exports.helloPubSub = function helloPubSub (context, data) { + context.success('Hello ' + (data.name || 'World') + '!'); +}; +// [END helloPubSub] + +// [START helloGCS] +/** + * Background Cloud Function to be triggered by Cloud Storage. + * + * @param {Object} context Cloud Function context. + * @param {Object} data Request data, provided by a Cloud Storage trigger. + */ +exports.helloGCS = function helloGCS (context, data) { + context.success('Hello ' + (data.name || 'World') + '!'); +}; +// [END helloGCS] diff --git a/functions/http/README.md b/functions/http/README.md new file mode 100644 index 0000000000..fc2f61eb91 --- /dev/null +++ b/functions/http/README.md @@ -0,0 +1,35 @@ +Google Cloud Platform logo + +# Google Cloud Functions HTTP sample + +This recipe shows you how to respond to HTTP requests with a Cloud Function. + +View the [source code][code]. + +[code]: index.js + +## Deploy and Test + +1. Follow the [Cloud Functions quickstart guide][quickstart] to setup Cloud +Functions for your project. + +1. Clone this repository: + + git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git + cd nodejs-docs-samples/functions/http + +1. Create a Cloud Storage Bucket to stage our deployment: + + gsutil mb gs://[YOUR_BUCKET_NAME] + + * Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket. + +1. Deploy the "helloGET" function with an HTTP trigger + + gcloud alpha functions deploy publish --bucket [YOUR_BUCKET_NAME] --trigger-http + +1. Call the "helloGET" function: + + curl https://[YOUR_PROJECT_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/helloGET + +[quickstart]: https://cloud.google.com/functions/quickstart diff --git a/functions/http/index.js b/functions/http/index.js new file mode 100644 index 0000000000..ee0e906444 --- /dev/null +++ b/functions/http/index.js @@ -0,0 +1,104 @@ +// Copyright 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. + +'use strict'; + +// [START helloworld] +/** + * Responds to any HTTP request that can provide a "message" field in the body. + * + * @param {Object} req Cloud Function request context. + * @param {Object} res Cloud Function response context. + */ +exports.helloworld = function helloworld (req, res) { + if (req.body.message === undefined) { + // This is an error case, as "message" is required + res.status(400).send('No message defined!'); + } else { + // Everything is ok + console.log(req.body.message); + res.status(200).end(); + } +}; +// [END helloworld] + +// [START helloHttp] +function handleGET (req, res) { + // Do something with the GET request + res.status(200).send('Hello World!'); +} + +function handlePUT (req, res) { + // Do something with the PUT request + res.status(403).send('Forbidden!'); +} + +/** + * Responds to a GET request with "Hello World!". Forbids a PUT request. + * + * @example + * gcloud alpha functions call helloHttp + * + * @param {Object} req Cloud Function request context. + * @param {Object} res Cloud Function response context. + */ +exports.helloHttp = function helloHttp (req, res) { + switch (req.method) { + case 'GET': + handleGET(req, res); + break; + case 'PUT': + handlePUT(req, res) + break; + default: + res.status(500).send({ error: 'Something blew up!' }); + break; + } +}; +// [END helloHttp] + +// [START helloContent] +/** + * Responds to any HTTP request that can provide a "message" field in the body. + * + * @param {Object} req Cloud Function request context. + * @param {Object} res Cloud Function response context. + */ +exports.helloContent = function helloContent (req, res) { + var name; + + switch (req.get('content-type')) { + // '{"name":"John"}' + case 'application/json': + name = req.body.name; + break; + + // 'John', stored in a Buffer + case 'application/octet-stream': + name = req.body.toString(); // Convert buffer to a string + break; + + // 'John' + case 'text/plain': + name = req.body; + break; + + // 'name=John' + case 'application/x-www-form-urlencoded': + name = req.body.name; + break; + } + + res.status(200).send('Hello ' + (name || 'World') + '!'); +}; +// [END helloContent] diff --git a/functions/log2/index.js b/functions/log2/index.js index a1f7b20439..9413ff3f23 100644 --- a/functions/log2/index.js +++ b/functions/log2/index.js @@ -21,8 +21,8 @@ exports.helloworld = function (context, data) { // [END walkthrough_pubsub] // [START walkthrough_http] -exports.hellohttp = function (context, data) { - // Use the success argument to send data back to the caller - context.success('My GCF Function: ' + data.message); +exports.hellohttp = function (req, res) { + // Use the response argument to send data back to the caller + res.send('My GCF Function: ' + req.body.message); }; // [END walkthrough_http] diff --git a/functions/message/index.js b/functions/message/index.js deleted file mode 100644 index defd9ab577..0000000000 --- a/functions/message/index.js +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 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. - -'use strict'; - -// [START message] -module.exports = { - helloworld: function (context, data) { - if (data.message !== undefined) { - // Everything is ok - console.log(data.message); - context.success(); - } else { - // This is an error case - context.failure('No message defined!'); - } - } -}; -// [END message] diff --git a/functions/pubsub/index.js b/functions/pubsub/index.js index da73061ed3..39ce065985 100644 --- a/functions/pubsub/index.js +++ b/functions/pubsub/index.js @@ -31,7 +31,7 @@ var pubsub = gcloud.pubsub(); * @param {string} data.topic Topic name on which to publish. * @param {string} data.message Message to publish. */ -function publish (context, data) { +exports.publish = function publish (context, data) { try { if (!data.topic) { throw new Error('Topic not provided. Make sure you have a ' + @@ -63,7 +63,7 @@ function publish (context, data) { console.error(err); return context.failure(err.message); } -} +}; /** * Triggered from a message on a Pub/Sub topic. @@ -74,13 +74,10 @@ function publish (context, data) { * @param {Object} data Request data, in this case an object provided by the Pub/Sub trigger. * @param {Object} data.message Message that was published via Pub/Sub. */ -function subscribe (context, data) { +exports.subscribe = function subscribe (context, data) { // We're just going to log the message to prove that it worked! console.log(data.message); // Don't forget to call success! context.success(); -} - -exports.publish = publish; -exports.subscribe = subscribe; +};