Skip to content

Commit

Permalink
Added 14 Google App Engine samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Jan 20, 2016
1 parent b2272ca commit 58f7128
Show file tree
Hide file tree
Showing 146 changed files with 1,856 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, Google, Inc.
# 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
Expand Down
23 changes: 8 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ See [CONTRIBUTING.md](https://github.com/GoogleCloudPlatform/nodejs-docs-samples
1. Start Redis
1. Start Memcached
1. Set the `TEST_PROJECT_ID` environment variable to id of your project
1. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path to
a service account file. You can download one from your Google project's
"permissions" page.
1. `npm test`

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


```
npm test -- -- --grep <pattern>
```
npm test -- -- --grep <pattern>

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

```
npm test -- -- --grep datastore
```
npm test -- -- --grep datastore

__Skip the tests that match a pattern:__

```
npm test -- -- --grep <pattern> --invert
```
npm test -- -- --grep <pattern> --invert

__Run all but the `datastore` tests:__

```
npm test -- -- --grep datastore --invert
```
npm test -- -- --grep datastore --invert

__Skip the tests that require Redis and Memcached:__

```
npm test -- -- --grep "express-memcached-session|redis" --invert
```
npm test -- -- --grep "express-memcached-session|redis" --invert

## Licensing

Expand Down
51 changes: 51 additions & 0 deletions appengine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Google App Engine Node.js Samples

These are samples for using Node.js on Google App Engine Managed VMs. These
samples are referenced from the [docs](https://cloud.google.com/appengine/docs).

See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform)
for sample applications and scaffolding for other frameworks and use cases.

## Run Locally

Some samples have specific instructions. If there is a README in the sample
folder, please refer to it for any additional steps required to run the sample.

In general, the samples typically require:

1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the
[gcloud tool](https://cloud.google.com/sdk/gcloud/), and
[gcloud app component](https://cloud.google.com/sdk/gcloud-app).
1. Setup the gcloud tool. This provides authentication to Google Cloud APIs and
services.

gcloud init

1. Clone this repo.

git clone https://github.com/GoogleCloudPlatform/<REPO NAME>.git

1. Open a sample folder, install dependencies, and run the sample:

cd <sample-folder>/
npm install
npm start

1. Visit the application at [http://localhost:8080](http://localhost:8080).

## Deploying

Some samples in this repositories may have special deployment instructions.
Refer to the README file in the sample folder.

1. Use the [Google Developers Console](https://console.developer.google.com) to
create a project/app id. (App id and project id are identical.)
1. Setup the gcloud tool, if you haven't already.

gcloud init

1. Use gcloud to deploy your app.

gcloud preview app deploy

1. Awesome! Your application is now live at `your-app-id.appspot.com`.
23 changes: 23 additions & 0 deletions appengine/analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Google Analytics Measurement Protocol on Google App Engine

This sample demonstrates how to use the [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/)
(or any other SQL server) on [Google App Engine Managed VMs](https://cloud.google.com/appengine).

## Setup

Before you can run or deploy the sample, you need to do the following:

1. Create a Google Analytics Property and obtain the Tracking ID.
1. Update the environment variables in in `app.yaml` with your Tracking ID.

## Running locally

Refer to the [appengine/README.md](../README.md) file for instructions on
running and deploying.

To run locally, set the environment variables via your shell before running the
sample:

export GA_TRACKING_ID=<your-tracking-id>
npm install
npm start
76 changes: 76 additions & 0 deletions appengine/analytics/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.

// [START app]
'use strict';

var express = require('express');
var request = require('request');

var app = express();

// The following environment variable is set by app.yaml when running on GAE,
// but will need to be manually set when running locally. See README.md.
var GA_TRACKING_ID = process.env.GA_TRACKING_ID;

function trackEvent(category, action, label, value, cb) {
var data = {
v: '1', // API Version.
tid: GA_TRACKING_ID, // Tracking ID / Property ID.
// Anonymous Client Identifier. Ideally, this should be a UUID that
// is associated with particular user, device, or browser instance.
cid: '555',
t: 'event', // Event hit type.
ec: category, // Event category.
ea: action, // Event action.
el: label, // Event label.
ev: value, // Event value.
};

request.post(
'http://www.google-analytics.com/collect', {
form: data
},
function(err, response) {
if (err) { return cb(err); }
if (response.statusCode !== 200) {
return cb(new Error('Tracking failed'));
}
cb();
}
);
}

app.get('/', function(req, res, next) {
trackEvent(
'Example category',
'Example action',
'Example label',
'100', // Event value must be numeric.
function(err) {
// This sample treats an event tracking error as a fatal error. Depending
// on your application's needs, failing to track an event may not be
// considered an error.
if (err) { return next(err); }
res.status(200).send('Event tracked.');
});
});

// Start the server
var server = app.listen(process.env.PORT || '8080', '0.0.0.0', function() {
console.log('App listening at http://%s:%s', server.address().address,
server.address().port);
console.log('Press Ctrl+C to quit.');
});
// [END app]
25 changes: 25 additions & 0 deletions appengine/analytics/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.

# [START app_yaml]
runtime: nodejs
vm: true

# [START env]
env_variables:
GA_TRACKING_ID: <your-tracking-id>
# [END env]

skip_files:
- ^(.*/)?.*/node_modules/.*$
# [END app_yaml]
20 changes: 20 additions & 0 deletions appengine/analytics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "appengine-analytics",
"description": "Sample for Google Analytics Measurement Protocol on Google App Engine",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~4.2"
},
"scripts": {
"start": "node app.js",
"monitor": "nodemon app.js",
"deploy": "gcloud preview app deploy"
},
"dependencies": {
"express": "^4.13.3",
"request": "^2.67.0"
}
}
2 changes: 1 addition & 1 deletion appengine/bower/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Bower on Google App Engine
# Bower on Google App Engine

> [Bower][1]: A package manager for the web.
Expand Down
7 changes: 4 additions & 3 deletions appengine/bower/app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, Google, Inc.
# 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
Expand All @@ -10,10 +10,11 @@
# 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.
#

# [START app_yaml]
runtime: nodejs
vm: true
# [END app_yaml]

skip_files:
- ^(.*/)?.*/node_modules/.*$
# [END app_yaml]
3 changes: 2 additions & 1 deletion appengine/bower/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~4.2"
},
"scripts": {
"start": "node server.js",
"postinstall": "bower install --config.interactive=false",
"deploy": "gcloud preview app deploy app.yaml"
"deploy": "gcloud preview app deploy"
},
"dependencies": {
"bower": "^1.6.5",
Expand Down
2 changes: 1 addition & 1 deletion appengine/bower/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015, Google, Inc.
// 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
Expand Down
2 changes: 1 addition & 1 deletion appengine/bower/views/index.jade
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015, Google, Inc.
// 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
Expand Down
40 changes: 40 additions & 0 deletions appengine/cloudsql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Node.js Cloud SQL sample on Google App Engine

This sample demonstrates how to use [Cloud SQL](https://cloud.google.com/sql/)
on [Google App Engine Managed VMs](https://cloud.google.com/appengine).

## Setup

Before you can run or deploy the sample, you will need to do the following:

1. Create a Cloud SQL instance. You can do this from the [Google Developers Console](https://console.developers.google.com)
or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK
use the following command:

gcloud sql instances create [your-instance-name] \
--assign-ip \
--authorized-networks 0.0.0.0/0 \
--tier D0

1. Create a new user and database for the application. The easiest way to do
this is via the [Google Developers Console](https://console.developers.google.com/project/_/sql/instances/example-instance2/access-control/users).
Alternatively, you can use MySQL tools such as the command line client or
workbench.
1. Update the values in in `app.yaml` with your instance configuration.
1. Finally, run `create_tables.js` to ensure that the database is properly
configured and to create the tables needed for the sample.

## Running locally

Refer to the [appengine/README.md](../README.md) file for instructions on
running and deploying.

To run locally, set the environment variables via your shell before running the
sample:

export MYSQL_HOST=<your-cloudsql-host>
export MYSQL_USER=<your-cloudsql-user>
export MYSQL_PASSWORD=<your-cloudsql-password>
export MYSQL_DATABASE=<your-cloudsql-database>
npm install
npm start
Loading

0 comments on commit 58f7128

Please sign in to comment.