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

Upgrade GAE apps to env: flex #253

Merged
merged 1 commit into from
Nov 14, 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 appengine/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Google App Engine Node.js Samples

These are samples for using Node.js on Google App Engine Managed VMs. These
These are samples for using Node.js on Google App Engine Flexible Environment. 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)
Expand Down
2 changes: 1 addition & 1 deletion appengine/analytics/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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).
(or any other SQL server) on [Google App Engine Flexible Environment](https://cloud.google.com/appengine).

## Setup

Expand Down
61 changes: 33 additions & 28 deletions appengine/analytics/app.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
// 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.
/**
* 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';

// [START setup]
var express = require('express');
var request = require('request');
const express = require('express');
const request = require('request');

var app = express();
const app = express();
app.enable('trust proxy');
// [END setup]

// [START track]
// 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;
const GA_TRACKING_ID = process.env.GA_TRACKING_ID;

function trackEvent (category, action, label, value, cb) {
var data = {
const data = {
v: '1', // API Version.
tid: GA_TRACKING_ID, // Tracking ID / Property ID.
// Anonymous Client Identifier. Ideally, this should be a UUID that
Expand All @@ -43,15 +44,18 @@ function trackEvent (category, action, label, value, cb) {
};

request.post(
'http://www.google-analytics.com/collect', {
'http://www.google-analytics.com/collect',
{
form: data
},
function (err, response) {
(err, response) => {
if (err) {
return cb(err);
cb(err);
return;
}
if (response.statusCode !== 200) {
return cb(new Error('Tracking failed'));
cb(new Error('Tracking failed'));
return;
}
cb();
}
Expand All @@ -60,28 +64,29 @@ function trackEvent (category, action, label, value, cb) {
// [END track]

// [START endpoint]
app.get('/', function (req, res, next) {
app.get('/', (req, res, next) => {
trackEvent(
'Example category',
'Example action',
'Example label',
'100', // Event value must be numeric.
function (err) {
(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);
next(err);
return;
}
res.status(200).send('Event tracked.');
});
});
// [END endpoint]

// [START listen]
var PORT = process.env.PORT || 8080;
app.listen(PORT, function () {
console.log('App listening on port %s', PORT);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END listen]
Expand Down
4 changes: 2 additions & 2 deletions appengine/analytics/app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2016, 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 @@ -13,7 +13,7 @@

# [START app_yaml]
runtime: nodejs
vm: true
env: flex

# [START env]
env_variables:
Expand Down
8 changes: 4 additions & 4 deletions appengine/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~4.2"
"node": ">=4.3.2"
},
"scripts": {
"start": "node app.js",
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
},
"dependencies": {
"express": "^4.13.4",
"request": "^2.69.0"
"express": "^4.14.0",
"request": "^2.75.0"
},
"devDependencies": {
"mocha": "^2.5.3"
"mocha": "^3.1.0"
}
}
81 changes: 42 additions & 39 deletions appengine/analytics/test/app.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
// 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.
/**
* 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 express = require('express');
var path = require('path');
var proxyquire = require('proxyquire').noPreserveCache();
var request = require('supertest');
const express = require(`express`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
const request = require(`supertest`);

var SAMPLE_PATH = path.join(__dirname, '../app.js');
const SAMPLE_PATH = path.join(__dirname, `../app.js`);

function getSample () {
var testApp = express();
sinon.stub(testApp, 'listen').callsArg(1);
var expressMock = sinon.stub().returns(testApp);
var resultsMock = {
const testApp = express();
sinon.stub(testApp, `listen`).callsArg(1);
const expressMock = sinon.stub().returns(testApp);
const resultsMock = {
statusCode: 200,
foo: 'bar'
foo: `bar`
};

var requestMock = {
post: sinon.stub().callsArgWith(2, null, resultsMock)
const requestMock = {
post: sinon.stub().yields(null, resultsMock)
};

var app = proxyquire(SAMPLE_PATH, {
const app = proxyquire(SAMPLE_PATH, {
request: requestMock,
express: expressMock
});

return {
app: app,
mocks: {
Expand All @@ -47,52 +50,52 @@ function getSample () {
};
}

describe('appengine/analytics/app.js', function () {
var sample;
describe(`appengine/analytics/app.js`, () => {
let sample;

beforeEach(function () {
beforeEach(() => {
sample = getSample();

assert(sample.mocks.express.calledOnce);
assert(sample.app.listen.calledOnce);
assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
});

it('should record a visit', function (done) {
var expectedResult = 'Event tracked.';
it(`should record a visit`, (done) => {
const expectedResult = `Event tracked.`;

request(sample.app)
.get('/')
.get(`/`)
.expect(200)
.expect(function (response) {
.expect((response) => {
assert.equal(response.text, expectedResult);
})
.end(done);
});

it('should handle request error', function (done) {
var expectedResult = 'request_error';
it(`should handle request error`, (done) => {
const expectedResult = `request_error`;

sample.mocks.request.post.onFirstCall().callsArgWith(2, expectedResult);

request(sample.app)
.get('/')
.get(`/`)
.expect(500)
.expect(function (response) {
assert.equal(response.text, expectedResult + '\n');
.expect((response) => {
assert.equal(response.text, expectedResult + `\n`);
})
.end(done);
});

it('should handle track error', function (done) {
it(`should handle track error`, (done) => {
sample.mocks.request.post.onFirstCall().callsArgWith(2, null, {
statusCode: 400
});

request(sample.app)
.get('/')
.expect(500)
.expect(function (response) {
.expect((response) => {
assert.notEqual(response.text.indexOf('Error: Tracking failed'), -1);
})
.end(done);
Expand Down
4 changes: 2 additions & 2 deletions appengine/bower/app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2016, 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 @@ -13,5 +13,5 @@

# [START app_yaml]
runtime: nodejs
vm: true
env: flex
# [END app_yaml]
10 changes: 5 additions & 5 deletions appengine/bower/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~4.2"
"node": ">=4.3.2"
},
"scripts": {
"postinstall": "bower install --config.interactive=false",
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
},
"dependencies": {
"bower": "^1.7.7",
"express": "^4.13.4",
"jade": "^1.11.0"
"bower": "^1.7.9",
"express": "^4.14.0",
"pug": "^2.0.0-beta6"
},
"devDependencies": {
"mocha": "^2.5.3"
"mocha": "^3.1.0"
}
}
Loading