Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit b620c19

Browse files
author
Matthew Gaunt
committed
Merge pull request #41 from GoogleChrome/deployment
Add support for deploying to Google Cloud
2 parents aa40d47 + afa4a63 commit b620c19

File tree

7 files changed

+119
-8
lines changed

7 files changed

+119
-8
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
.dockerignore
3+
Dockerfile
4+
.git
5+
.hg
6+
.svn

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
*.pyc
44
dist
55
tests
6+
npm-debug.log

Dockerfile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Dockerfile extending the generic Node image with application files for a
2+
# single application.
3+
FROM beta.gcr.io/google_appengine/nodejs
4+
# Check to see if the the version included in the base runtime satisfies \
5+
# >=4.1.0, if not then do an npm install of the latest available \
6+
# version that satisfies it. \
7+
RUN npm install https://storage.googleapis.com/gae_node_packages/semver.tar.gz && \
8+
(node -e 'var semver = require("semver"); \
9+
if (!semver.satisfies(process.version, ">=4.1.0")) \
10+
process.exit(1);' || \
11+
(version=$(curl -L https://storage.googleapis.com/gae_node_packages/node_versions | \
12+
node -e ' \
13+
var semver = require("semver"); \
14+
var http = require("http"); \
15+
var spec = process.argv[1]; \
16+
var latest = ""; \
17+
var versions = ""; \
18+
var selected_version; \
19+
\
20+
function verifyBinary(version) { \
21+
var options = { \
22+
"host": "storage.googleapis.com", \
23+
"method": "HEAD", \
24+
"path": "/gae_node_packages/node-" + version + \
25+
"-linux-x64.tar.gz" \
26+
}; \
27+
var req = http.request(options, function (res) { \
28+
if (res.statusCode == 404) { \
29+
console.error("Binaries for Node satisfying version " + \
30+
version + " are not available."); \
31+
process.exit(1); \
32+
} \
33+
}); \
34+
req.end(); \
35+
} \
36+
function satisfies(version) { \
37+
if (semver.satisfies(version, spec)) { \
38+
process.stdout.write(version); \
39+
verifyBinary(version); \
40+
return true; \
41+
} \
42+
} \
43+
process.stdin.on("data", function(data) { \
44+
versions += data; \
45+
}); \
46+
process.stdin.on("end", function() { \
47+
versions = \
48+
versions.split("\n").sort().reverse(); \
49+
if (!versions.some(satisfies)) { \
50+
console.error("No version of Node found satisfying: " + \
51+
spec); \
52+
process.exit(1); \
53+
} \
54+
});' \
55+
">=4.1.0") && \
56+
rm -rf /nodejs/* && \
57+
(curl https://storage.googleapis.com/gae_node_packages/node-$version-linux-x64.tar.gz | \
58+
tar xzf - -C /nodejs --strip-components=1 \
59+
) \
60+
) \
61+
)
62+
COPY . /app/
63+
# You have to specify "--unsafe-perm" with npm install
64+
# when running as root. Failing to do this can cause
65+
# install to appear to succeed even if a preinstall
66+
# script fails, and may have other adverse consequences
67+
# as well.
68+
RUN npm --unsafe-perm install
69+
CMD npm start

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ server with:
4545
$ nodemon server/app.js
4646
```
4747

48+
Alternatively, you can just run `npm monitor`. The application shell should now be available on port `8080`.
49+
50+
### Deployment
51+
52+
We've deployed the project to Node.js on [Google Cloud](https://cloud.google.com/nodejs/). To do the same, follow the steps in their Node.js deployment [getting started](https://cloud.google.com/nodejs/getting-started/hello-world) guide and after running `npm install` run `gcloud preview app deploy app.yaml --promote`. If everything works correctly, you should have the project deployed to your custom AppSpot endpoint.
53+
4854
## Why?
4955

5056
[Service Workers](http://www.html5rocks.com/en/tutorials/service-worker/introduction/) are fantastic for offline caching but they also offer significant performance wins in the form of instant loading for repeat visits. This is possible with just a few changes to our overall application’s UI architecture.

app.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2015, 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: custom
16+
vm: true
17+
api_version: 1
18+
env_variables:
19+
PORT: 8080
20+
21+
automatic_scaling:
22+
min_num_instances: 2
23+
max_num_instances: 20
24+
cool_down_period_sec: 60
25+
cpu_utilization:
26+
target_utilization: 0.5
27+
# [END app_yaml]

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
"private": true,
55
"license": "Apache Version 2.0",
66
"engines": {
7-
"node": ">=0.12.7"
7+
"node": ">=4.1.0"
88
},
99
"scripts": {
10-
"start": "nodemon server/app.js"
10+
"start": "node server/app.js",
11+
"monitor": "nodemon server/app.js",
12+
"postinstall": "gulp",
13+
"deploy": "gcloud preview app deploy app.yaml --promote"
1114
},
1215
"main": "server/app.js",
1316
"dependencies": {
14-
"express": "^4.13.3",
15-
"express-handlebars": "^2.0.1"
16-
},
17-
"devDependencies": {
1817
"babel-preset-es2015": "^6.0.15",
1918
"babelify": "^7.2.0",
2019
"browserify": "^11.2.0",
2120
"del": "^2.0.2",
21+
"express": "^4.13.3",
22+
"express-handlebars": "^2.0.1",
23+
"gcloud": "^0.24.1",
2224
"glob": "^5.0.15",
2325
"gulp": "^3.9.0",
2426
"gulp-autoprefixer": "^3.1.0",

server/controllers/server-controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ ServerController.prototype.setUpServer = function(app) {
2929
app.use('/',
3030
express.static(path.join(__dirname + '/../../dist/')));
3131

32-
console.log('Starting server on 3123');
33-
return app.listen('3123');
32+
console.log('Starting server on 8080');
33+
return app.listen('8080');
3434
};
3535

3636
ServerController.prototype.addEndpoint = function(endpoint, controller) {

0 commit comments

Comments
 (0)