-
Notifications
You must be signed in to change notification settings - Fork 2k
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
New samples for Cloud Functions docs #132
Changes from all commits
4a1c20d
b981fe4
de7c2ba
79ae033
eec53f6
3b2c6fc
85ec9ba
e27e128
2b57ec0
6ea5b95
0493fa3
d278df7
6a070cc
94913d1
cf1f7ff
67f1810
b1cfb26
48f87cd
f69a769
b2eb9d9
d5a684e
7633d3d
78540e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document that data expects a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @param {string} data.message Message, provided by the 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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,76 @@ | |
'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 helloGET] | ||
/** | ||
* HTTP Cloud Function. | ||
* | ||
* @param {Object} req Cloud Function request context. | ||
* @param {Object} res Cloud Function response context. | ||
*/ | ||
exports.helloGET = function helloGET (req, res) { | ||
res.send('Hello World!'); | ||
}; | ||
// [END helloGET] | ||
|
||
// [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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we have a separate background example if we already have one here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are multiple background samples in the docs. Once the docs are published, you'll see somewhat of a mapping between sample folders here and pages in the docs. |
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this data look like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like whatever the user publishes to the topic. |
||
*/ | ||
exports.helloPubSub = function helloPubSub (context, data) { | ||
console.log('Hello ' + (data.name || 'World') + '!'); | ||
context.success(); | ||
}; | ||
// [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) { | ||
console.log('Hello ' + (data.name || 'World') + '!'); | ||
context.success(); | ||
}; | ||
// [END helloGCS] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/> | ||
|
||
# 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// 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; | ||
|
||
console.log(req.get('content-type')); | ||
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] |
This file was deleted.
This file was deleted.
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.
What's a background function?
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.
Any function that's triggered, without the "triggerer" waiting for a response. This will be thoroughly documented in the docs.