From 19733455ac27d5df1d01f44a7d2c4f96b1043716 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 12 Dec 2020 15:54:32 -0500 Subject: [PATCH] Adding invoke, and refactoring app structure --- docker-compose.yml | 2 +- services/api/app/api/__init__.py | 0 services/api/app/api/ping.py | 10 ++++++++++ services/api/app/main.py | 8 +++----- tasks.py | 4 ++-- 5 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 services/api/app/api/__init__.py create mode 100644 services/api/app/api/ping.py diff --git a/docker-compose.yml b/docker-compose.yml index ca86535..ffad242 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3.7' services: api: build: ./services/api - command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000 + command: uvicorn api.main:api --reload --workers 1 --host 0.0.0.0 --port 8000 volumes: - ./services/api/:/opt/fast_api/ ports: diff --git a/services/api/app/api/__init__.py b/services/api/app/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/services/api/app/api/ping.py b/services/api/app/api/ping.py new file mode 100644 index 0000000..dd0d384 --- /dev/null +++ b/services/api/app/api/ping.py @@ -0,0 +1,10 @@ +import datetime as dt +from fastapi import APIRouter + +router = APIRouter() + + +@router.get('/ping') +async def pong(): + now = dt.datetime.now(dt.timezone.utc) + return {'ping': "pong!", 'now': now} diff --git a/services/api/app/main.py b/services/api/app/main.py index 93407c7..7b60eea 100644 --- a/services/api/app/main.py +++ b/services/api/app/main.py @@ -1,10 +1,8 @@ import datetime as dt from fastapi import FastAPI -app = FastAPI() +from app.api import ping +app = FastAPI() -@app.get('/ping') -async def pong(): - now = dt.datetime.now(dt.timezone.utc) - return {'ping': "pong!", 'now': now} +app.include_router(ping.router) diff --git a/tasks.py b/tasks.py index e21ccae..01979b2 100644 --- a/tasks.py +++ b/tasks.py @@ -1,9 +1,9 @@ from invoke import task -UNVICORN_CMD = "uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000" +UNVICORN_CMD = "uvicorn api.main:api --reload --workers 1 --host 0.0.0.0 --port 8000" @task def serve(c): - """Serves the fast_api app for local development""" + """Serves the fast_api api for local development""" c.run(f"pushd services/api && {UNVICORN_CMD}")