From 99116332740db6a6dd36f6be95c3443f7c9f2a6d Mon Sep 17 00:00:00 2001 From: Harsh Mishra Date: Fri, 17 Jun 2022 01:10:35 +0530 Subject: [PATCH] add sample app for chalice integration (#119) --- chalice-rest-api/.chalice/config.json | 9 ++++ chalice-rest-api/.gitignore | 2 + chalice-rest-api/README.md | 41 ++++++++++++++++ chalice-rest-api/app.py | 65 +++++++++++++++++++++++++ chalice-rest-api/chalicelib/__init__.py | 0 chalice-rest-api/requirements-dev.txt | 3 ++ chalice-rest-api/requirements.txt | 0 chalice-rest-api/tests/__init__.py | 0 chalice-rest-api/tests/test_app.py | 8 +++ 9 files changed, 128 insertions(+) create mode 100644 chalice-rest-api/.chalice/config.json create mode 100644 chalice-rest-api/.gitignore create mode 100644 chalice-rest-api/README.md create mode 100644 chalice-rest-api/app.py create mode 100644 chalice-rest-api/chalicelib/__init__.py create mode 100644 chalice-rest-api/requirements-dev.txt create mode 100644 chalice-rest-api/requirements.txt create mode 100644 chalice-rest-api/tests/__init__.py create mode 100644 chalice-rest-api/tests/test_app.py diff --git a/chalice-rest-api/.chalice/config.json b/chalice-rest-api/.chalice/config.json new file mode 100644 index 0000000..924f64e --- /dev/null +++ b/chalice-rest-api/.chalice/config.json @@ -0,0 +1,9 @@ +{ + "version": "2.0", + "app_name": "chalice-rest-api", + "stages": { + "dev": { + "api_gateway_stage": "api" + } + } +} diff --git a/chalice-rest-api/.gitignore b/chalice-rest-api/.gitignore new file mode 100644 index 0000000..3dd60a9 --- /dev/null +++ b/chalice-rest-api/.gitignore @@ -0,0 +1,2 @@ +.chalice/deployments/ +.chalice/venv/ diff --git a/chalice-rest-api/README.md b/chalice-rest-api/README.md new file mode 100644 index 0000000..6b8ed27 --- /dev/null +++ b/chalice-rest-api/README.md @@ -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. diff --git a/chalice-rest-api/app.py b/chalice-rest-api/app.py new file mode 100644 index 0000000..39e9db9 --- /dev/null +++ b/chalice-rest-api/app.py @@ -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 diff --git a/chalice-rest-api/chalicelib/__init__.py b/chalice-rest-api/chalicelib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chalice-rest-api/requirements-dev.txt b/chalice-rest-api/requirements-dev.txt new file mode 100644 index 0000000..76b9591 --- /dev/null +++ b/chalice-rest-api/requirements-dev.txt @@ -0,0 +1,3 @@ +chalice==1.27.1 +pytest +chalice-local diff --git a/chalice-rest-api/requirements.txt b/chalice-rest-api/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/chalice-rest-api/tests/__init__.py b/chalice-rest-api/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chalice-rest-api/tests/test_app.py b/chalice-rest-api/tests/test_app.py new file mode 100644 index 0000000..0325b5d --- /dev/null +++ b/chalice-rest-api/tests/test_app.py @@ -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'}