-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sample app for chalice integration (#119)
- Loading branch information
1 parent
03dabbc
commit 9911633
Showing
9 changed files
with
128 additions
and
0 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,9 @@ | ||
{ | ||
"version": "2.0", | ||
"app_name": "chalice-rest-api", | ||
"stages": { | ||
"dev": { | ||
"api_gateway_stage": "api" | ||
} | ||
} | ||
} |
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,2 @@ | ||
.chalice/deployments/ | ||
.chalice/venv/ |
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,41 @@ | ||
# LocalStack Demo: Chalice REST API | ||
|
||
Simple demo application illustrating AWS Chalice integration in LocalStack. The AWS Chalice integration features a REST API which can be tested locally and put to production using the [LocalStack's AWS Chalice client](https://github.com/localstack/chalice-local). | ||
|
||
## Prerequisites | ||
|
||
- LocalStack | ||
- Docker | ||
- `chalice-local` | ||
|
||
## Installing | ||
|
||
To install the dependencies: | ||
|
||
```sh | ||
pip3 install -r requirements-dev.txt | ||
``` | ||
|
||
## Running | ||
|
||
Make sure that LocalStack is started: | ||
|
||
```sh | ||
localstack start -d | ||
``` | ||
|
||
Start the local-server via: | ||
|
||
```sh | ||
chalice-local local | ||
``` | ||
|
||
You will see the following logs on the terminal: | ||
|
||
```sh | ||
Serving on http://127.0.0.1:8000 | ||
``` | ||
|
||
## License | ||
|
||
This code is available under the Apache 2.0 license. |
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,65 @@ | ||
from chalice import Chalice, NotFoundError, Response | ||
|
||
app = Chalice(app_name='todo-app') | ||
app.debug = True | ||
|
||
TODO_ITEMS = { | ||
'1': { | ||
'item': 'Run LocalStack in detached mode' | ||
}, | ||
'2': { | ||
'item': 'Run your Chalice REST API with LocalStack' | ||
} | ||
} | ||
|
||
@app.route('/') | ||
def index(): | ||
return {'localstack': 'chalice integration'} | ||
|
||
@app.route('/health') | ||
def health_check(): | ||
return Response(status_code=200, body="ok\n", headers={'Content-Type': 'text/plain'}) | ||
|
||
@app.route('/todo') | ||
def todos(): | ||
items = [ v for k, v in TODO_ITEMS.items() ] | ||
|
||
params = app.current_request.query_params | ||
if params: | ||
offset = int(params.get('offset', 0)) | ||
size = int(params.get('size', len(TODO_ITEMS))) | ||
|
||
return items[offset:size] | ||
|
||
return items | ||
|
||
@app.route('/todo/{todo_id}') | ||
def get_todo(todo_id): | ||
if todo_id in TODO_ITEMS: | ||
return TODO_ITEMS[todo_id] | ||
raise NotFoundError | ||
|
||
@app.route('/todo/{todo_id}', methods=["DELETE"]) | ||
def delete_todo(todo_id): | ||
item = TODO_ITEMS[todo_id] | ||
del TODO_ITEMS[todo_id] | ||
return item | ||
|
||
@app.route('/todo/{todo_id}', methods=["POST", "PUT"]) | ||
def update_todo(todo_id): | ||
if app.current_request.method == "POST": | ||
TODO_ITEMS[todo_id].update(app.current_request.json_body) | ||
else: | ||
TODO_ITEMS[todo_id] = app.current_request.json_body | ||
return TODO_ITEMS[todo_id] | ||
|
||
@app.route('/introspect') | ||
def introspect(): | ||
return app.current_request.to_dict() | ||
|
||
@app.route('/todo', methods=["POST"]) | ||
def add_todo(): | ||
todo = app.current_request.json_body | ||
new_id = str(len(TODO_ITEMS) + 1) | ||
TODO_ITEMS[new_id] = todo | ||
return todo |
Empty file.
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 @@ | ||
chalice==1.27.1 | ||
pytest | ||
chalice-local |
Empty file.
Empty file.
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,8 @@ | ||
from chalice.test import Client | ||
from app import app | ||
|
||
|
||
def test_index(): | ||
with Client(app) as client: | ||
response = client.http.get('/') | ||
assert response.json_body == {'localstack': 'chalice integration'} |