From f0e73900c149f26ffa66646d45683ca048c2d83c Mon Sep 17 00:00:00 2001 From: Jon Whittlestone Date: Mon, 26 Jun 2023 10:28:11 +0100 Subject: [PATCH 1/4] Config --- .github/workflows/main.yml | 2 -- Makefile | 4 ++++ README.md | 8 +++++++- backend/config/__init__.py | 25 +++++++++++++++++++++++++ backend/config/settings.toml | 14 +++++++------- backend/db.py | 4 ++-- backend/main.py | 10 ++++++++-- snippets-resources.md | 9 ++++++++- 8 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 backend/config/__init__.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c7d212b..f7f86f3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,8 +13,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - with: - ref: main - name: Add environment variables to .env run: | echo "DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/starter_db_dev" >> .env diff --git a/Makefile b/Makefile index 0fef8c7..6ba9cef 100644 --- a/Makefile +++ b/Makefile @@ -27,3 +27,7 @@ rebuild: rebuild-d: docker-compose down -v --remove-orphans && sudo rm -rf postgres-data; docker-compose up --build -d; + +d-test: + # Rebuilds container and runs docker test + docker-compose build web-test && docker-compose run web-test pytest tests -x -o log_cli=true diff --git a/README.md b/README.md index 5dfa6c8..db5f83f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,12 @@ make up-build Run the command above and then visit: http://127.0.0.1:8004 +### Run the tests: + +```bash +make d-test +``` + > See [snippets-resources.md](snippets-resources.md) for handy snippets and resources. @@ -35,7 +41,7 @@ Run the command above and then visit: http://127.0.0.1:8004 - [x] Poetry, Dynaconf - [x] Containerised tests - [x] Github Action to run tests -- [ ] FastAPI repositories, schemas / logging / cleanup and mocked tests +- [ ] FastAPI config, routers, middleware, model, auth, repositories, schemas, logging, cleanup and mocked tests. - [ ] Production deployment - [ ] Pre-commit / manage.py / migrations - [ ] Tag this repo and release diff --git a/backend/config/__init__.py b/backend/config/__init__.py new file mode 100644 index 0000000..68cd5b5 --- /dev/null +++ b/backend/config/__init__.py @@ -0,0 +1,25 @@ + +import os +from pydantic import BaseSettings + +from .dyna import settings + + +class CoreSettings(BaseSettings): + DEBUG: bool = False + SERVICE_NAME: str = "starter-api" + SERVICE_LABEL: str = f"starter-api {settings.current_env}" + SERVICE_VERSION = "1.0.0" + SERVICE_DESCRIPTION = "A no-brainer starter for a dockerised FastAPI app" + DATABASE_URL: str = os.environ.get("DATABASE_URL", "") + APP_HOST: str = "0.0.0.0" # nosec + APP_PORT: int = 8000 + +class ModuleSettings(BaseSettings): + ... + +class Sett(CoreSettings, ModuleSettings): + pass + + +setts = Sett() \ No newline at end of file diff --git a/backend/config/settings.toml b/backend/config/settings.toml index c0a4b09..334ff0f 100644 --- a/backend/config/settings.toml +++ b/backend/config/settings.toml @@ -1,17 +1,17 @@ [default] -APP_NAME="Starter API" - +APP_LABEL="Starter API" + [development] -APP_NAME="Starter API DEV" +APP_LABEL="Starter API DEV" [ci] -APP_NAME="Starter API CI" +APP_LABEL="Starter API CI" [testing] -APP_NAME="Starter API TEST" +APP_LABEL="Starter API TEST" [staging] -APP_NAME="Starter API ST" +APP_LABEL="Starter API ST" [production] -APP_NAME="Starter API PR" \ No newline at end of file +APP_LABEL="Starter API PR" \ No newline at end of file diff --git a/backend/db.py b/backend/db.py index 48f9665..dc0af2c 100644 --- a/backend/db.py +++ b/backend/db.py @@ -5,10 +5,10 @@ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker +from .config import setts -DATABASE_URL = os.environ.get("DATABASE_URL") -engine = create_async_engine(DATABASE_URL, echo=True, future=True) +engine = create_async_engine(setts.DATABASE_URL, echo=True, future=True) async def init_db(): diff --git a/backend/main.py b/backend/main.py index 342e303..60b853f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -5,6 +5,7 @@ from .db import init_db, get_session from .models import Album, AlbumCreate from .config.dyna import settings +from .config import setts from sqlalchemy.future import select from sqlalchemy.ext.asyncio import AsyncSession @@ -14,7 +15,13 @@ from fastapi.templating import Jinja2Templates -app = FastAPI(title=settings.APP_NAME,) +app = FastAPI( + title=setts.SERVICE_LABEL, + description=setts.SERVICE_DESCRIPTION, + version=setts.SERVICE_VERSION, + debug=setts.DEBUG, +) + app.mount("/static", StaticFiles(directory="backend/static"), name="static") templates = Jinja2Templates(directory="backend/templates") @@ -27,7 +34,6 @@ async def on_startup(): def health(): return JSONResponse(status_code=200, content={ - "app_name": settings.APP_NAME, "current_env": settings.current_env, }) diff --git a/snippets-resources.md b/snippets-resources.md index aaaed21..b600ca1 100644 --- a/snippets-resources.md +++ b/snippets-resources.md @@ -11,7 +11,14 @@ Warning: The `make rebuild-d` command will remove your postgres volume. ```bash make rebuild-d; \ -docker-compose exec web-test tests -x -o log_cli=true +docker-compose run web-test pytest tests -x -o log_cli=true +``` + +### Docker Test workflow + +```bash +docker-compose build web-test && docker-compose run web-test pytest tests -x -o log_cli=true + ``` ### Check DB Provisioning From 60ff1e2b293f90526870d4e67cb2749e7438417f Mon Sep 17 00:00:00 2001 From: Jon Whittlestone Date: Mon, 26 Jun 2023 10:55:33 +0100 Subject: [PATCH 2/4] Routers --- README.md | 19 ++++++++------- backend/api/__init__.py | 0 backend/api/album.py | 28 +++++++++++++++++++++ backend/api/endpoints.py | 9 +++++++ backend/api/health.py | 19 +++++++++++++++ backend/main.py | 47 ++++++------------------------------ backend/tests/test_albums.py | 2 +- 7 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 backend/api/__init__.py create mode 100644 backend/api/album.py create mode 100644 backend/api/endpoints.py create mode 100644 backend/api/health.py diff --git a/README.md b/README.md index db5f83f..af8aff5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # starter-api + ![Continuous Integration](https://github.com/jonwhittlestone/starter-api/workflows/Continuous%20Integration/badge.svg) ## Usage + ```bash make up-build ``` -Run the command above and then visit: http://127.0.0.1:8004 +Run the command above and then visit: http://127.0.0.1:8004 ### Run the tests: @@ -14,22 +16,21 @@ Run the command above and then visit: http://127.0.0.1:8004 make d-test ``` - > See [snippets-resources.md](snippets-resources.md) for handy snippets and resources. - ## Debug FastAPI using VSCode 1. Run this: - ```bash - make up-build - ``` + + ```bash + make up-build + ``` 2. In vscode add a breakpoint 3. Run the debugger (F5) -4. Visit: http://127.0.0.1:8004 +4. Visit: http://127.0.0.1:8004 5. The vscode debugger will pause execution at your breakpoint. @@ -41,13 +42,13 @@ make d-test - [x] Poetry, Dynaconf - [x] Containerised tests - [x] Github Action to run tests -- [ ] FastAPI config, routers, middleware, model, auth, repositories, schemas, logging, cleanup and mocked tests. +- [ ] FastAPI config, routers, user model, auth, repositories, schemas, logging, cleanup and mocked tests. - [ ] Production deployment - [ ] Pre-commit / manage.py / migrations - [ ] Tag this repo and release ``` -