Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott committed Dec 1, 2015
0 parents commit 20cd0a2
Show file tree
Hide file tree
Showing 109 changed files with 9,072 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
indent_size = 4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
*.log
15 changes: 15 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"camelcase" : true,
"curly": true,
"eqeqeq": true,
"globalstrict": true,
"indent": 2,
"newcap": true,
"maxlen": 80,
"node": true,
"quotmark": "single",
"strict": true,
"trailing": true,
"undef": true,
"unused": true
}
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: node_js
node_js:
- "stable"
- "0.12"
- "0.10"
37 changes: 37 additions & 0 deletions 1-hello-world/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2015, 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 app = express();


// [START hello_world]
// Say hello!
app.get('/', function(req, res) {
res.status(200).send('Hello, world!');
});
// [END hello_world]


// [START server]
// Start the server
var server = app.listen(process.env.PORT || 8080, function () {
var host = server.address().address;
var port = server.address().port;

console.log('App listening at http://%s:%s', host, port);
});
// [END server]
21 changes: 21 additions & 0 deletions 1-hello-world/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2015, 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 runtime]
runtime: nodejs
vm: true
# [END runtime]

# Temporary setting to keep gcloud from uploading node_modules
skip_files:
- ^node_modules$
39 changes: 39 additions & 0 deletions 1-hello-world/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "nodejs-getting-started",
"version": "1.0.0",
"description": "End to end sample for running Node.JS applications on Google Cloud Platform",
"repository": "https://github.com/GoogleCloudPlatform/nodejs-getting-started",
"private": true,
"scripts": {
"start": "node app.js",
"monitor": "nodemon app.js",
"deploy": "gcloud preview app deploy app.yaml",
"lint": "jshint --exclude-path=.gitignore .",
"test": "npm run lint"
},
"author": "Google Inc.",
"contributors": [
{
"name": "Jon Wayne Parrott",
"email": "jonwayne@google.com"
},
{
"name": "Jonathan Simon",
"email": "jbsimon@google.com"
},
{
"name": "Jason Dobry",
"email": "jdobry@google.com"
}
],
"license": "Apache Version 2.0",
"dependencies": {
"express": "^4.13.3"
},
"devDependencies": {
"jshint": "^2.8.0"
},
"engines": {
"node": ">=0.12.7"
}
}
54 changes: 54 additions & 0 deletions 2-structured-data/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2015, 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 path = require('path');
var express = require('express');
var config = require('./config');

var app = express();

app.disable('etag');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('trust proxy', true);


// Books
var model = require('./books/model-' + config.dataBackend)(config);
app.use('/books', require('./books/crud')(model));
app.use('/api/books', require('./books/api')(model));


// Redirect root to /books
app.get('/', function(req, res) {
res.redirect('/books');
});


// Basic error handler
app.use(function(err, req, res, next) {
/* jshint unused:false */
console.error(err.stack);
res.status(500).send('Something broke!');
});


// Start the server
var server = app.listen(config.port, function () {
var host = server.address().address;
var port = server.address().port;

console.log('App listening at http://%s:%s', host, port);
});
19 changes: 19 additions & 0 deletions 2-structured-data/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2015, 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.
#
runtime: nodejs
vm: true

# Temporary setting to keep gcloud from uploading node_modules
skip_files:
- ^node_modules$
81 changes: 81 additions & 0 deletions 2-structured-data/books/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2015, 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 bodyParser = require('body-parser');


module.exports = function(model) {

var router = express.Router();

router.use(bodyParser.json());


function handleRpcError(err, res) {
if (err.code === 404) { return res.status(404); }
res.status(500).json({
message: err.message,
internalCode: err.code
});
}


router.get('/', function list(req, res) {
model.list(10, req.query.pageToken,
function(err, entities, cursor) {
if (err) { return handleRpcError(err, res); }
res.json({
items: entities,
nextPageToken: cursor
});
});
});


router.post('/', function insert(req, res) {
model.create(req.body, function(err, entity) {
if (err) { return handleRpcError(err, res); }
res.json(entity);
});
});


router.get('/:book(\\d+)', function get(req, res) {
model.read(req.params.book, function(err, entity) {
if (err) { return handleRpcError(err, res); }
res.json(entity);
});
});


router.put('/:book(\\d+)', function update(req, res) {
model.update(req.params.book, req.body, function(err, entity) {
if (err) { return handleRpcError(err, res); }
res.json(entity);
});
});


router.delete('/:book(\\d+)', function _delete(req, res) {
model.delete(req.params.book, function(err) {
if (err) { return handleRpcError(err, res); }
res.status(200).send('OK');
});
});

return router;

};
Loading

0 comments on commit 20cd0a2

Please sign in to comment.