Skip to content
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

Added 14 Google App Engine samples. #54

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 2015-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
121 changes: 94 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
# Google Cloud Platform NodeJS Samples

This repository holds the samples used in the nodejs documentation on
[cloud.google.com/nodejs](https://cloud.google.com/nodejs).
This repository holds Node.js samples used throughout [cloud.google.com]().

[![Build Status](https://travis-ci.org/GoogleCloudPlatform/nodejs-docs-samples.svg)](https://travis-ci.org/GoogleCloudPlatform/nodejs-docs-samples)

## Table of Contents

* [Google App Engine](#google-app-engine)
* [Google Cloud Logging](#google-cloud-logging)
* [Google Cloud Storage](#google-cloud-storage)
* [Google Prediction API](#google-prediction-api)
* [Other Example Apps](#other-example-apps)
* [More Information](#more-information)
* [Contributing](#contributing)
* [License](#license)

## Google App Engine

This is a collection of samples and instructions to run common nodejs frameworks
and applications on [Google App Engine](http://cloud.google.com/nodejs).

### Frameworks
__Google (Cloud) Products__

- Google Analytics - [Source code][aeanalytics_1]
- Google Cloud Logging - [Source code][aelogging_1]
- Google Cloud Pub/Sub - [Source code][aepubsub_1]
- Google Cloud Storage - [Source code][aestorage_1]

__Frameworks__

- Express.js - [Source code][express_1] | [App Engine Tutorial][express_2] | [Live demo][express_3] | [Documentation][express_4]
- Express.js + Memcached Sessions - [Source code][express_5] | [Documentation][express_6]
Expand All @@ -22,25 +39,51 @@ and applications on [Google App Engine](http://cloud.google.com/nodejs).
- Restify.js - [Source code][restify_1] | [App Engine Tutorial][restify_2] | [Live demo][restify_3] | [Documentation][restify_4]
- Sails.js - [Source code][sails_1] | [App Engine Tutorial][sails_2] | [Live demo][sails_3] | [Documentation][sails_4]

### Databases
__Databases__

- Google Cloud Datastore - [Source code][aedatastore_1]
- Google Cloud SQL - [Source code][aecloudsql_1]
- Memcached - [Source code][memcached_1]
- MongoDB - [Source code][mongodb_1] | [App Engine Tutorial][mongodb_2] | [Documentation][mongodb_3]
- Redis - [Source code][redis_1] | [App Engine Tutorial][redis_2] | [Documentation][redis_3]

### Tools
__Tools__

- gcloud-node - [Source code][gcloud_1] | [Documentation][gcloud_2]
- Bower - [Source code][bower_1] | [App Engine Tutorial][bower_2] | [Documentation][bower_3]
- Grunt - [Source code][grunt_1] | [App Engine Tutorial][grunt_2] | [Live demo][grunt_3] | [Documentation][grunt_4]
- Mailgun - [Source code][mailgun_1] | [App Engine Tutorial][mailgun_2] | [Documentation][mailgun_3]
- Sendgrid - [Source code][sendgrid_1] | [App Engine Tutorial][sendgrid_2] | [Documentation][sendgrid_3]
- Twilio - [Source code][twilio_1]
- Webpack - [Source code][webpack_1] | [App Engine Tutorial][webpack_2] | [Documentation][webpack_3]
- WebSockets - [Source code][websockets_1]

__Other Examples__

- Express.js Hello World - [Source code][expresshw_1]
- Extending the runtime - [Source code][aeextending_1]
- Reading/writing from/to disk - [Source code][aedisk_1]
- Serving static files - [Source code][aestaticfiles_1]

## Google Cloud Datastore

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

## Google Cloud Logging

- Reading logs sample - [Source code][logging_read_1] | [Documentation][logging_read_2]
- Writing logs sample - [Source code][logging_write_1] | [Documentation][logging_write_2]
- Exporting logs sample - [Source code][logging_export_1] | [Documentation][logging_export_2]

## Google Cloud Storage

- Auth sample - [Source code][storage_1] | [Documentation][storage_2]

## Google Storage
## Google Prediction API

- Auth sample - [Source code][storage_1] | [Google Cloud Docs][storage_2]
- Hosted Models sample - [Source code][predictionapi_1] | [Documentation][predictionapi_2]

## Example Apps
## Other Example Apps

- nodejs-getting-started - [Source code][nodejs_1] | [App Engine Tutorial 1][nodejs_2] | [App Engine Tutorial 2][nodejs_3]
- gcloud-node-todos - [Source code][todos_1]
Expand All @@ -54,7 +97,7 @@ and applications on [Google App Engine](http://cloud.google.com/nodejs).
- [Using the `gcloud` npm module](https://googlecloudplatform.github.io/gcloud-node/#/)
- [Logging to Google Cloud with Winston](https://github.com/GoogleCloudPlatform/winston-gae)

## Contributing changes
## Contributing

Contributions welcome!

Expand All @@ -68,6 +111,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,42 +123,34 @@ 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
## License

Apache Version 2.0

See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/LICENSE)

[storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js
[storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples
[aeanalytics_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/analytics
[aelogging_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/logging
[aepubsub_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/pubsub
[aestorage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/storage

[express_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/express
[express_2]: https://cloud.google.com/nodejs/resources/frameworks/express
Expand Down Expand Up @@ -156,6 +194,10 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
[sails_3]: http://sails-dot-nodejs-docs-samples.appspot.com
[sails_4]: http://sailsjs.org/

[aedatastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/datastore
[aecloudsql_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/cloudsql
[memcached_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/memcached

[mongodb_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/mongodb
[mongodb_2]: https://cloud.google.com/nodejs/resources/databases/mongo
[mongodb_3]: https://docs.mongodb.org/
Expand Down Expand Up @@ -184,10 +226,35 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
[sendgrid_2]: https://cloud.google.com/nodejs/resources/tools/sendgrid
[sendgrid_3]: http://sendgrid.com/

[twilio_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/twilio

[webpack_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/webpack
[webpack_2]: https://cloud.google.com/nodejs/resources/tools/webpack
[webpack_3]: https://webpack.github.io/

[websockets_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/websockets

[expresshw_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/hello-world
[aedisk_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/disk
[aeextending_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/extending-runtime
[aestaticfiles_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/static-files

[datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js
[datastore_2]: https://cloud-dot-devsite.googleplex.com/datastore/docs/concepts/overview

[logging_read_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/list.js
[logging_read_2]: https://cloud-dot-devsite.googleplex.com/logging/docs/api/tasks/authorization
[logging_write_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/write.js
[logging_write_2]: https://cloud.google.com/logging/docs/api/tasks/creating-logs
[logging_export_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/export.js
[logging_export_2]: https://cloud.google.com/logging/docs/api/tasks/exporting-logs

[storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js
[storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples

[predictionapi_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/prediction/hostedmodels.js
[predictionapi_2]: https://cloud.google.com/prediction/docs/developer-guide#predictionfromappengine

[nodejs_1]: https://github.com/GoogleCloudPlatform/nodejs-getting-started
[nodejs_2]: https://cloud.google.com/nodejs/getting-started/hello-world
[nodejs_3]: https://cloud.google.com/nodejs/getting-started/tutorial-app
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 2015-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 2015-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]
Loading