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

initial import #1

Merged
merged 18 commits into from
Sep 15, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

key.json
*~
20 changes: 20 additions & 0 deletions CONTRIB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# How to become a contributor and submit your own code

## Contributor License Agreements

We'd love to accept your patches! Before we can take them, we have to jump a couple of legal hurdles.

Please fill out either the individual or corporate Contributor License Agreement (CLA).

* If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html).
* If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html).

Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests.

## Contributing A Patch

1. Submit an issue describing your proposed change to the repo in question.
1. The repo owner will respond to your issue promptly.
1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above).
1. Fork the desired repo, develop and test your code changes.
1. Submit a pull request.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM google/nodejs-runtime
12 changes: 12 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
application: gcloud-node-todos
module: default
version: 1
runtime: custom
api_version: 1
vm: true
manual_scaling:
instances: 1

handlers:
- url: .*
script: dynamic
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "todomvm-nodejs",
"description": "todomvc sample for gcloud-node",
"version": "0.0.1",
"license": "Apache 2.0",
"dependencies": {
"express": "^4.5.1",
"body-parser": "^1.4.3",
"gcloud": "^0.4.0"
}
}
127 changes: 127 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
var express = require('express'),
bodyParser = require('body-parser'),
app = express();

var gcloud = require('gcloud'),
datastore = gcloud.datastore;

var ds = new datastore.Dataset({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to do the following once googleapis/google-cloud-node#154 is merged.

var ds = new datastore.Dataset();

projectId: process.env.GAE_LONG_APP_ID || process.env.DATASET_ID,
keyFilename: 'key.json'
});

app.use(bodyParser.json());

var todoListName = 'default-list';

app.get('/', function(req, res) {
res.send(200, 'hello world');
});

app.get('/todos', function(req, res) {
var q = ds.createQuery('Todo')
.hasAncestor(ds.key('TodoList', todoListName));
ds.runQuery(q, function(err, items) {
if (err) {
console.error(err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comma after console.error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean semicolon?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean semicolon.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

res.status(500).send(err.message);
return;
}
res.json(items.map(function(obj, i) {
obj.data.id = obj.key.path_.pop();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some pseudo getters for IDs and names to avoid this.

return obj.data;
}));
});
});

app.get('/todos/:id', function(req, res) {
var id = req.param('id');
ds.get(ds.key('TodoList', todoListName, 'Todo', id), function(err, obj) {
if (err) {
console.error(err)
res.status(500).send(err.message);
return;
}
if (!obj) {
return res.send(404);
}
obj.data.id = obj.key.path_.pop();
res.json(obj.data);
});
});

app.post('/todos', function(req, res) {
var todo = req.body;
ds.save({
key: ds.key('TodoList', todoListName, 'Todo'),
data: todo
}, function(err, key) {
if (err) {
console.error(err)
res.status(500).send(err.message);
return;
}
todo.id = key.path_.pop();
res.json(todo);
});
});

app.put('/todos/:id', function(req, res) {
var id = req.param('id');
var todo = req.body;
ds.save({
key: ds.key('TodoList', todoListName, 'Todo', id),
data: todo
}, function(err, key) {
if (err) {
console.error(err)
res.status(500).send(err.message);
return;
}
todo.id = id;
res.json(todo);
});
});

app.delete('/todos/:id', function(req, res) {
var id = req.param('id');
ds.delete(ds.key('TodoList', todoListName, 'Todo', id), function(err) {
if (err) {
console.error(err)
res.status(500).send(err.message);
return;
}
res.send(200);
});
});

app.delete('/todos', function(req, res) {
ds.runInTransaction(function(t, done) {
var q = ds.createQuery('Todo')
.hasAncestor(ds.key('TodoList', todoListName))
.filter('completed =', true);
t.runQuery(q, function(err, items) {
if (err) {
t.rollback(done);
console.error(err)
res.status(500).send(err.message);
return;
}
var keys = items.map(function(obj) {
return obj.key;
});
t.delete(keys, function(err) {
if (err) {
t.rollback(done);
console.error(err)
res.status(500).send(err.message);
return;
}
done();
res.send(200);
});
});
});
});

app.listen(8080);