Skip to content

Commit b4942ac

Browse files
committed
Cloud Functions samples.
1 parent d831992 commit b4942ac

File tree

20 files changed

+529
-8
lines changed

20 files changed

+529
-8
lines changed

.jshintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ appengine/sails/tasks/**
66
appengine/sails/assets/**
77
appengine/sails/api/responses/**
88
appengine/webpack/dist/**
9+
functions/**
910
**/node_modules/**
1011
coverage/

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This repository holds Node.js samples used throughout [cloud.google.com]().
77
## Table of Contents
88

99
* [Google App Engine](#google-app-engine)
10+
* [Google Cloud Functions](#google-cloud-functions)
1011
* [Google Cloud Logging](#google-cloud-logging)
1112
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
1213
* [Google Cloud Storage](#google-cloud-storage)
@@ -67,10 +68,14 @@ __Other Examples__
6768
- Reading/writing from/to disk - [Source code][aedisk_1]
6869
- Serving static files - [Source code][aestaticfiles_1]
6970

70-
## Google Cloud Datastorem
71+
## Google Cloud Datastore
7172

7273
- Tasks sample - [Source code][datastore_1] | [Documentation][datastore_2]
7374

75+
## Google Cloud Functions
76+
77+
- Samples - [Source code][functions_1] | [Documentation][functions_2]
78+
7479
## Google Cloud Logging
7580

7681
- Reading logs sample - [Source code][logging_read_1] | [Documentation][logging_read_2]
@@ -251,6 +256,9 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
251256
[datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js
252257
[datastore_2]: https://cloud.google.com/datastore/docs/concepts/overview
253258

259+
[functions_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/
260+
[functions_2]: https://cloud.google.com/functions/docs
261+
254262
[logging_read_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/list.js
255263
[logging_read_2]: https://cloud.google.com/logging/docs/api/tasks/authorization
256264
[logging_write_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/write.js

functions/helloworld/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Google Cloud Functions Hello World! sample
2+
3+
This sample shows a single exported function–a basic hello world example.
4+
5+
## Deploy sample
6+
7+
This example deploys the function with an HTTP trigger.
8+
9+
```
10+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
11+
```
12+
13+
## Test the function
14+
15+
```
16+
gcloud alpha functions call helloworld
17+
```
18+
19+
Running the above command should print "Hello World!".

functions/helloworld/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START helloworld]
17+
exports.helloworld = function (context, data) {
18+
context.success('Hello World!');
19+
};
20+
// [END helloworld]

functions/log/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Google Cloud Functions message sample
2+
3+
This sample shows writing to logs in a Cloud Function.
4+
5+
## Deploy sample
6+
7+
This example deploys the function with an HTTP trigger.
8+
9+
```
10+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
11+
```
12+
13+
## Test the function
14+
15+
```
16+
gcloud alpha functions call helloworld
17+
```

functions/log/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START log]
17+
exports.helloworld = function (context, data) {
18+
console.log('I am a log entry!');
19+
context.success();
20+
};
21+
// [END log]
22+
23+
// [START walkthrough_pubsub]
24+
exports.hellotopic = function (context, data) {
25+
console.log('My GCF Function: ' + data.message);
26+
context.success();
27+
};
28+
// [END walkthrough_pubsub]
29+
30+
// [START walkthrough_http]
31+
exports.hellohttp = function (context, data) {
32+
// Use the success argument to send data back to the caller
33+
context.success('My GCF Function: ' + data.message);
34+
};
35+
// [END walkthrough_http]

functions/message/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Google Cloud Functions message sample
2+
3+
This sample shows calling the `success` and `failure` callbacks.
4+
5+
## Deploy sample
6+
7+
This example deploys the function with an HTTP trigger.
8+
9+
```
10+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
11+
```
12+
13+
## Test the function
14+
15+
```
16+
gcloud alpha functions call helloworld
17+
```

functions/message/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START message]
17+
module.exports = {
18+
helloworld: function (context, data) {
19+
if (data.message !== undefined) {
20+
// Everything is ok
21+
console.log(data.message);
22+
context.success();
23+
} else {
24+
// This is an error case
25+
context.failure('No message defined!');
26+
}
27+
}
28+
};
29+
// [END message]

functions/module/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google Cloud Functions module sample
2+
3+
This sample shows exporting a Google Cloud Function as part of a module, which
4+
is the method one would use to store multiple function in a single source file.
5+
6+
## Deploy sample
7+
8+
This example deploys the function with an HTTP trigger.
9+
10+
```
11+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
12+
```
13+
14+
## Test the function
15+
16+
```
17+
gcloud alpha functions call helloworld
18+
```
19+
20+
Running the above command should print a generated UUID.

functions/module/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START module]
17+
module.exports = {
18+
helloworld: function (context, data) {
19+
context.success('Hello World!');
20+
}
21+
};
22+
// [END module]

0 commit comments

Comments
 (0)