Skip to content

Commit

Permalink
add sample app for chalice integration (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshCasper authored Jun 16, 2022
1 parent 03dabbc commit 9911633
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 0 deletions.
9 changes: 9 additions & 0 deletions chalice-rest-api/.chalice/config.json
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"
}
}
}
2 changes: 2 additions & 0 deletions chalice-rest-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.chalice/deployments/
.chalice/venv/
41 changes: 41 additions & 0 deletions chalice-rest-api/README.md
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.
65 changes: 65 additions & 0 deletions chalice-rest-api/app.py
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.
3 changes: 3 additions & 0 deletions chalice-rest-api/requirements-dev.txt
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.
8 changes: 8 additions & 0 deletions chalice-rest-api/tests/test_app.py
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'}

0 comments on commit 9911633

Please sign in to comment.