-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modularizing server and introduce cli example.
- Loading branch information
1 parent
9be5bb4
commit a0a1349
Showing
16 changed files
with
585 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"bitwise": true, | ||
"curly": true, | ||
"eqeqeq": true, | ||
"esnext": true, | ||
"freeze": true, | ||
"immed": true, | ||
"indent": 2, | ||
"latedef": "nofunc", | ||
"newcap": true, | ||
"node": true, | ||
"noarg": true, | ||
"quotmark": "single", | ||
"strict": true, | ||
"trailing": true, | ||
"undef": true, | ||
"unused": true | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,109 @@ | ||
gcloud-node-todos | ||
================= | ||
# gcloud-node-todos | ||
> TodoMVC backend using [gcloud-node](//github.com/GoogleCloudPlatform/gcloud-node). | ||
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-node-todos.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-node-todos) | ||
|
||
TodoMVC backend using [gcloud-node](//github.com/GoogleCloudPlatform/gcloud-node). | ||
## Examples | ||
|
||
# API | ||
See the [apps](https://github.com/GoogleCloudPlatform/gcloud-node-todos/apps) directory for a full list of examples. | ||
|
||
- Insert a todo | ||
Have an idea for an implementation? Please [raise an issue](https://github.com/GoogleCloudPlatform/gcloud-node-todos/issues/new). | ||
|
||
curl -X POST -d '{text: "do this"}' http://localhost:8080/todos | ||
## API | ||
|
||
- Get a todo | ||
#### Insert a todo | ||
```sh | ||
$ curl -X POST -d '{text: "do this"}' http://localhost:8080/todos | ||
``` | ||
|
||
curl -X GET http://localhost:8080/todos/{{todo id}} | ||
#### Get a todo | ||
```sh | ||
$ curl -X GET http://localhost:8080/todos/{{todo id}} | ||
``` | ||
|
||
- Mark a todo as done | ||
#### Mark a todo as done | ||
```sh | ||
$ curl -X PUT -d '{text: "do this", "done": true}' http://localhost:8080/todos/{{todo id}} | ||
``` | ||
|
||
curl -X PUT -d '{text: "do this", "done": true}' http://localhost:8080/todos/{{todo id}} | ||
#### Delete a todo | ||
```sh | ||
$ curl -X DELETE http://localhost:8080/todos/{{todo id}} | ||
``` | ||
|
||
- Delete a todo | ||
#### Get all todos | ||
```sh | ||
$ curl -X GET http://localhost:8080/todos | ||
``` | ||
|
||
curl -X DELETE http://localhost:8080/todos/{{todo id}} | ||
#### Clear all `done` todos | ||
```sh | ||
$ curl -X DELETE http://localhost:8080/todos | ||
``` | ||
|
||
- Get all todos | ||
|
||
curl -X GET http://localhost:8080/todos | ||
## Prerequisites | ||
|
||
- Clear all `done` todos | ||
1. Create a new cloud project on [console.developers.google.com](http://console.developers.google.com) | ||
2. [Enable](https://console.developers.google.com/flows/enableapi?apiid=datastore) the [Google Cloud Datastore API](https://developers.google.com/datastore) | ||
3. Create a new service account and copy the JSON credentials to `key.json` | ||
4. Export your project id: | ||
```sh | ||
$ export PROJECT_ID=<project id> | ||
``` | ||
|
||
curl -X DELETE http://localhost:8080/todos | ||
## Running | ||
|
||
# Prerequisites | ||
#### Locally | ||
```sh | ||
# Set your default Dataset | ||
$ export DATASET_ID=$PROJECT_ID | ||
- Create a new cloud project on [console.developers.google.com](http://console.developers.google.com) | ||
- [Enable](https://console.developers.google.com/flows/enableapi?apiid=datastore) the [Google Cloud Datastore API](https://developers.google.com/datastore) | ||
- Create a new service account and copy the `JSON` credentials to `key.json` | ||
- Export your project id | ||
|
||
export PROJECT_ID=<project id> | ||
# Install the dependencies | ||
$ npm install | ||
# Run locally | ||
# Start the server | ||
$ npm start | ||
# set your default dataset | ||
export DATASET_ID=$PROJECT_ID | ||
# fetch the dependencies | ||
npm install | ||
# start the app | ||
npm start | ||
# run acceptance test | ||
dredd todos.apib http://localhost:8080 --hookfiles test_hooks.js | ||
# Run acceptance test | ||
$ npm test | ||
``` | ||
|
||
# Run in docker | ||
#### Docker | ||
```sh | ||
# Check that Docker is running | ||
$ boot2docker up | ||
$ export DOCKER_HOST=$(boot2docker shellinit) | ||
# check that docker is running | ||
boot2docker up | ||
export DOCKER_HOST=$(boot2docker shellinit) | ||
# Build your Docker image | ||
$ docker build -t app . | ||
# build your docker image | ||
docker build -t app . | ||
# start a new docker container | ||
docker run -e DATASET_ID=$PROJECT_ID -p 8080:8080 app | ||
# Start a new Docker container | ||
$ docker run -e DATASET_ID=$PROJECT_ID -p 8080:8080 app | ||
# test the app | ||
curl -X GET http://$(boot2docker ip):8080 | ||
# Test the app | ||
$ curl -X GET http://$(boot2docker ip):8080 | ||
``` | ||
|
||
# Run w/ [Managed VMs](https://developers.google.com/appengine/docs/managed-vms/) | ||
#### [Managed VMs](https://developers.google.com/appengine/docs/managed-vms/) | ||
```sh | ||
# Get gcloud | ||
$ curl https://sdk.cloud.google.com | bash | ||
# get gcloud | ||
curl https://sdk.cloud.google.com | bash | ||
# authorize gcloud and set your default project | ||
gcloud auth login | ||
gcloud config set project $PROJECT_ID | ||
# Authorize gcloud and set your default project | ||
$ gcloud auth login | ||
$ gcloud config set project $PROJECT_ID | ||
# get managed vms component | ||
gcloud components update app-engine-managed-vms | ||
# Get Managed VMs component | ||
$ gcloud components update app-engine-managed-vms | ||
# check that docker is running | ||
boot2docker up | ||
# Check that Docker is running | ||
$ boot2docker up | ||
# run the app locally | ||
gcloud preview app run . | ||
curl -X GET http://localhost:8080 | ||
# Run the app locally | ||
$ gcloud preview app run . | ||
$ curl -X GET http://localhost:8080 | ||
# deploy the app to production | ||
gcloud preview app deploy . | ||
curl -X GET http://$PROJECT_ID.appspot.com | ||
# Deploy the app to production | ||
$ gcloud preview app deploy . | ||
$ curl -X GET http://$PROJECT_ID.appspot.com | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# gcloud-node-todos Examples | ||
|
||
- [Command Line](https://github.com/GoogleCloudPlatform/gcloud-node-todos/apps/cli) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#! /usr/bin/env node | ||
|
||
// | ||
// Copyright 2013 Google Inc. All Rights Reserved. | ||
// | ||
// 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 todos = require('./'); | ||
var inquirer = require('inquirer'); | ||
var actions = { | ||
add: add, | ||
deleteCompleted: deleteCompleted, | ||
exit: process.kill, | ||
displayTodos: displayTodos, | ||
displayTodosAndDelete: displayTodosAndDelete | ||
}; | ||
|
||
// Start the prompts. | ||
init(); | ||
|
||
function init() { | ||
inquirer.prompt([ | ||
{ | ||
message: 'What would you like to do?', | ||
name: 'action', | ||
type: 'list', | ||
choices: [ | ||
{ | ||
name: 'List Todos', | ||
value: 'displayTodos' | ||
}, | ||
{ | ||
name: 'Add a Todo', | ||
value: 'add' | ||
}, | ||
{ | ||
name: 'Delete Todos', | ||
value: 'displayTodosAndDelete' | ||
}, | ||
{ | ||
name: 'Delete Completed Todos', | ||
value: 'deleteCompleted' | ||
}, | ||
new inquirer.Separator(), | ||
{ | ||
name: 'Exit', | ||
value: 'exit' | ||
} | ||
] | ||
} | ||
], function(answers) { | ||
actions[answers.action](answers); | ||
}); | ||
} | ||
|
||
function add() { | ||
inquirer.prompt({ | ||
message: 'What do you need to do?', | ||
name: 'text' | ||
}, function(answers) { | ||
todos.add(answers.text, function(err) { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
init(); | ||
}); | ||
}); | ||
} | ||
|
||
function displayTodos() { | ||
todos.getTodos(function(err, entities) { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
if (entities.length === 0) { | ||
console.log('There are no todos!\n'); | ||
init(); | ||
return; | ||
} | ||
inquirer.prompt({ | ||
message: 'What have you completed?', | ||
name: 'completed', | ||
type: 'checkbox', | ||
choices: entities.map(function(entity) { | ||
return { | ||
name: entity.text, | ||
checked: entity.done, | ||
value: entity | ||
}; | ||
}) | ||
}, function(answers) { | ||
// Update entities model. | ||
entities = entities.map(function(entity) { | ||
if (answers.completed.some(function(completed) { | ||
return completed.id === entity.id; | ||
})) { | ||
entity.done = true; | ||
} else { | ||
entity.done = false; | ||
} | ||
return entity; | ||
}); | ||
|
||
todos.update(entities, function(err) { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
init(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function displayTodosAndDelete() { | ||
todos.getTodos(function(err, entities) { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
inquirer.prompt({ | ||
message: 'What would you like to delete?', | ||
name: 'completed', | ||
type: 'checkbox', | ||
choices: entities.map(function(entity) { | ||
return { | ||
name: entity.text, | ||
checked: false, | ||
value: entity | ||
}; | ||
}) | ||
}, function(answers) { | ||
todos.delete(answers.completed, function(err) { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
init(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function deleteCompleted() { | ||
todos.deleteCompleted(function(err) { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
init(); | ||
}); | ||
} |
Oops, something went wrong.