-
Notifications
You must be signed in to change notification settings - Fork 2k
Cloud Functions samples. #65
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Google Cloud Functions Hello World! sample | ||
|
|
||
| This is a basic hello world sample that shows a single exported function. | ||
|
|
||
| ## Deploy sample | ||
|
|
||
| This example deploys the function with an HTTP trigger. | ||
|
|
||
| ``` | ||
| gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http | ||
| ``` | ||
|
|
||
| ## Test the function | ||
|
|
||
| ``` | ||
| gcloud alpha functions call helloworld | ||
| ``` | ||
|
|
||
| Running the above command should print "Hello World!". | ||
|
|
||
| You can also use `curl` to trigger the function: | ||
|
|
||
| curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // 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] | ||
| exports.helloworld = function (context, data) { | ||
| context.success('Hello World!'); | ||
| }; | ||
| // [END helloworld] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Google Cloud Functions message sample | ||
|
|
||
| This sample shows writing to logs in a Cloud Function. | ||
|
|
||
| ## Deploy sample | ||
|
|
||
| This example deploys the function with an HTTP trigger. | ||
|
|
||
| ``` | ||
| gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http | ||
| ``` | ||
|
|
||
| ## Test the function | ||
|
|
||
| ``` | ||
| gcloud alpha functions call helloworld | ||
| ``` | ||
|
|
||
| You can also use `curl` to trigger the function: | ||
|
|
||
| curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // 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 log] | ||
| exports.helloworld = function (context, data) { | ||
| console.log('I am a log entry!'); | ||
| context.success(); | ||
| }; | ||
| // [END log] | ||
|
|
||
| exports.log = exports.helloworld; | ||
|
|
||
| // [START walkthrough_pubsub] | ||
| exports.helloworld = function (context, data) { | ||
| console.log('My GCF Function: ' + data.message); | ||
| context.success(); | ||
| }; | ||
| // [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); | ||
| }; | ||
| // [END walkthrough_http] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Google Cloud Functions message sample | ||
|
|
||
| This sample shows calling the `success` and `failure` callbacks. | ||
|
|
||
| ## Deploy sample | ||
|
|
||
| This example deploys the function with an HTTP trigger. | ||
|
|
||
| ``` | ||
| gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http | ||
| ``` | ||
|
|
||
| ## Test the function | ||
|
|
||
| ``` | ||
| gcloud alpha functions call helloworld | ||
| ``` | ||
|
|
||
| You can also use `curl` to trigger the function: | ||
|
|
||
| curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld --data '{"message":"cat"}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Google Cloud Functions module sample | ||
|
|
||
| This sample shows exporting a Google Cloud Function as part of a module, which | ||
| is the method one would use to store multiple function in a single source file. | ||
|
|
||
| ## Deploy sample | ||
|
|
||
| This example deploys the function with an HTTP trigger. | ||
|
|
||
| ``` | ||
| gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http | ||
| ``` | ||
|
|
||
| ## Test the function | ||
|
|
||
| ``` | ||
| gcloud alpha functions call helloworld | ||
| ``` | ||
|
|
||
| Running the above command should print "Hello World!". | ||
|
|
||
| You can also use `curl` to trigger the function: | ||
|
|
||
| curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // 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 module] | ||
| module.exports = { | ||
| helloworld: function (context, data) { | ||
| context.success('Hello World!'); | ||
| } | ||
| }; | ||
| // [END module] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Google Cloud Functions UUID sample | ||
|
|
||
| This sample shows a Google Cloud Function that uses a dependency from NPM, and | ||
| is a handy way to generate a v4 UUID from the command-line. | ||
|
|
||
| ## Deploy sample | ||
|
|
||
| This example deploys the function with an HTTP trigger. | ||
|
|
||
| ``` | ||
| gcloud alpha functions deploy uuid --bucket <your-bucket-name> --trigger-http | ||
| ``` | ||
|
|
||
| ## Test the function | ||
|
|
||
| ``` | ||
| gcloud alpha functions call uuid | ||
| ``` | ||
|
|
||
| Running the above command should print a generated UUID. | ||
|
|
||
| You can also use `curl` to trigger the function: | ||
|
|
||
| curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/uuid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // 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 uuid] | ||
| var uuid = require('node-uuid'); | ||
|
|
||
| exports.uuid = function (context, data) { | ||
| context.success(uuid.v4()); | ||
| }; | ||
| // [END uuid] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "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.", | ||
| "contributors": [ | ||
| { | ||
| "name": "Jason Dobry", | ||
| "email": "jason.dobry@gmail.com" | ||
| } | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
| }, | ||
| "dependencies": { | ||
| "node-uuid": "^1.4.7" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // 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'; | ||
|
|
||
| var assert = require('assert'); | ||
|
|
||
| var helloworldSample = require('../../functions/helloworld'); | ||
|
|
||
| describe('functions/helloworld', function () { | ||
|
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. Sigh. More code to test than to write. I generally abhor these kinds of tests as its usually more of a burden to maintain the tests than the code, but it's fine. I'm just venting. |
||
| it('should return a hello world message', function (done) { | ||
| helloworldSample.helloworld({ | ||
| success: function (result) { | ||
| try { | ||
| assert.equal(result, 'Hello World!'); | ||
| done(); | ||
| } catch (err) { | ||
| done(err); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you show them how to trigger it via curl?
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.
Fixed