Skip to content

Commit 58f7128

Browse files
committed
Added 14 Google App Engine samples.
1 parent b2272ca commit 58f7128

File tree

146 files changed

+1856
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+1856
-180
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015, Google, Inc.
1+
# Copyright 2016, Google, Inc.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

README.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ See [CONTRIBUTING.md](https://github.com/GoogleCloudPlatform/nodejs-docs-samples
6868
1. Start Redis
6969
1. Start Memcached
7070
1. Set the `TEST_PROJECT_ID` environment variable to id of your project
71+
1. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path to
72+
a service account file. You can download one from your Google project's
73+
"permissions" page.
7174
1. `npm test`
7275

7376
Since the tests use [Mocha.js](https://mochajs.org/), you can use the `--grep`
@@ -77,33 +80,23 @@ option causes the matched tests to be excluded instead of included.
7780
__Run only the tests that match a pattern:__
7881

7982

80-
```
81-
npm test -- -- --grep <pattern>
82-
```
83+
npm test -- -- --grep <pattern>
8384

8485
__Only run the tests for the `datastore` sample:__
8586

86-
```
87-
npm test -- -- --grep datastore
88-
```
87+
npm test -- -- --grep datastore
8988

9089
__Skip the tests that match a pattern:__
9190

92-
```
93-
npm test -- -- --grep <pattern> --invert
94-
```
91+
npm test -- -- --grep <pattern> --invert
9592

9693
__Run all but the `datastore` tests:__
9794

98-
```
99-
npm test -- -- --grep datastore --invert
100-
```
95+
npm test -- -- --grep datastore --invert
10196

10297
__Skip the tests that require Redis and Memcached:__
10398

104-
```
105-
npm test -- -- --grep "express-memcached-session|redis" --invert
106-
```
99+
npm test -- -- --grep "express-memcached-session|redis" --invert
107100

108101
## Licensing
109102

appengine/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Google App Engine Node.js Samples
2+
3+
These are samples for using Node.js on Google App Engine Managed VMs. These
4+
samples are referenced from the [docs](https://cloud.google.com/appengine/docs).
5+
6+
See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform)
7+
for sample applications and scaffolding for other frameworks and use cases.
8+
9+
## Run Locally
10+
11+
Some samples have specific instructions. If there is a README in the sample
12+
folder, please refer to it for any additional steps required to run the sample.
13+
14+
In general, the samples typically require:
15+
16+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the
17+
[gcloud tool](https://cloud.google.com/sdk/gcloud/), and
18+
[gcloud app component](https://cloud.google.com/sdk/gcloud-app).
19+
1. Setup the gcloud tool. This provides authentication to Google Cloud APIs and
20+
services.
21+
22+
gcloud init
23+
24+
1. Clone this repo.
25+
26+
git clone https://github.com/GoogleCloudPlatform/<REPO NAME>.git
27+
28+
1. Open a sample folder, install dependencies, and run the sample:
29+
30+
cd <sample-folder>/
31+
npm install
32+
npm start
33+
34+
1. Visit the application at [http://localhost:8080](http://localhost:8080).
35+
36+
## Deploying
37+
38+
Some samples in this repositories may have special deployment instructions.
39+
Refer to the README file in the sample folder.
40+
41+
1. Use the [Google Developers Console](https://console.developer.google.com) to
42+
create a project/app id. (App id and project id are identical.)
43+
1. Setup the gcloud tool, if you haven't already.
44+
45+
gcloud init
46+
47+
1. Use gcloud to deploy your app.
48+
49+
gcloud preview app deploy
50+
51+
1. Awesome! Your application is now live at `your-app-id.appspot.com`.

appengine/analytics/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Google Analytics Measurement Protocol on Google App Engine
2+
3+
This sample demonstrates how to use the [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/)
4+
(or any other SQL server) on [Google App Engine Managed VMs](https://cloud.google.com/appengine).
5+
6+
## Setup
7+
8+
Before you can run or deploy the sample, you need to do the following:
9+
10+
1. Create a Google Analytics Property and obtain the Tracking ID.
11+
1. Update the environment variables in in `app.yaml` with your Tracking ID.
12+
13+
## Running locally
14+
15+
Refer to the [appengine/README.md](../README.md) file for instructions on
16+
running and deploying.
17+
18+
To run locally, set the environment variables via your shell before running the
19+
sample:
20+
21+
export GA_TRACKING_ID=<your-tracking-id>
22+
npm install
23+
npm start

appengine/analytics/app.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2016, Google, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START app]
16+
'use strict';
17+
18+
var express = require('express');
19+
var request = require('request');
20+
21+
var app = express();
22+
23+
// The following environment variable is set by app.yaml when running on GAE,
24+
// but will need to be manually set when running locally. See README.md.
25+
var GA_TRACKING_ID = process.env.GA_TRACKING_ID;
26+
27+
function trackEvent(category, action, label, value, cb) {
28+
var data = {
29+
v: '1', // API Version.
30+
tid: GA_TRACKING_ID, // Tracking ID / Property ID.
31+
// Anonymous Client Identifier. Ideally, this should be a UUID that
32+
// is associated with particular user, device, or browser instance.
33+
cid: '555',
34+
t: 'event', // Event hit type.
35+
ec: category, // Event category.
36+
ea: action, // Event action.
37+
el: label, // Event label.
38+
ev: value, // Event value.
39+
};
40+
41+
request.post(
42+
'http://www.google-analytics.com/collect', {
43+
form: data
44+
},
45+
function(err, response) {
46+
if (err) { return cb(err); }
47+
if (response.statusCode !== 200) {
48+
return cb(new Error('Tracking failed'));
49+
}
50+
cb();
51+
}
52+
);
53+
}
54+
55+
app.get('/', function(req, res, next) {
56+
trackEvent(
57+
'Example category',
58+
'Example action',
59+
'Example label',
60+
'100', // Event value must be numeric.
61+
function(err) {
62+
// This sample treats an event tracking error as a fatal error. Depending
63+
// on your application's needs, failing to track an event may not be
64+
// considered an error.
65+
if (err) { return next(err); }
66+
res.status(200).send('Event tracked.');
67+
});
68+
});
69+
70+
// Start the server
71+
var server = app.listen(process.env.PORT || '8080', '0.0.0.0', function() {
72+
console.log('App listening at http://%s:%s', server.address().address,
73+
server.address().port);
74+
console.log('Press Ctrl+C to quit.');
75+
});
76+
// [END app]

appengine/analytics/app.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
18+
# [START env]
19+
env_variables:
20+
GA_TRACKING_ID: <your-tracking-id>
21+
# [END env]
22+
23+
skip_files:
24+
- ^(.*/)?.*/node_modules/.*$
25+
# [END app_yaml]

appengine/analytics/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "appengine-analytics",
3+
"description": "Sample for Google Analytics Measurement Protocol on Google App Engine",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": "~4.2"
10+
},
11+
"scripts": {
12+
"start": "node app.js",
13+
"monitor": "nodemon app.js",
14+
"deploy": "gcloud preview app deploy"
15+
},
16+
"dependencies": {
17+
"express": "^4.13.3",
18+
"request": "^2.67.0"
19+
}
20+
}

appengine/bower/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Bower on Google App Engine
1+
# Bower on Google App Engine
22

33
> [Bower][1]: A package manager for the web.
44

appengine/bower/app.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015, Google, Inc.
1+
# Copyright 2016, Google, Inc.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -10,10 +10,11 @@
1010
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
13-
#
13+
1414
# [START app_yaml]
1515
runtime: nodejs
1616
vm: true
17-
# [END app_yaml]
17+
1818
skip_files:
1919
- ^(.*/)?.*/node_modules/.*$
20+
# [END app_yaml]

appengine/bower/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"version": "0.0.1",
55
"private": true,
66
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
78
"engines": {
89
"node": "~4.2"
910
},
1011
"scripts": {
1112
"start": "node server.js",
1213
"postinstall": "bower install --config.interactive=false",
13-
"deploy": "gcloud preview app deploy app.yaml"
14+
"deploy": "gcloud preview app deploy"
1415
},
1516
"dependencies": {
1617
"bower": "^1.6.5",

0 commit comments

Comments
 (0)