From e975936d520dbe48c0fc4064257976ab90d2df0d Mon Sep 17 00:00:00 2001 From: Vlad Nedelcu Date: Wed, 28 Aug 2024 15:43:28 +0300 Subject: [PATCH] update readme and ci --- .github/workflows/ci.yaml | 4 +- README.md | 96 +++++++++++++++++++++++++++++++++++++-- poetry.lock | 7 ++- pyproject.toml | 2 +- 4 files changed, 101 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5ead5b2..c29f7fe 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,7 +12,7 @@ jobs: contents: 'read' strategy: matrix: - python-version: [ '3.9' ] + python-version: [ '3.8', '3.9' ] steps: - name: Check out code uses: actions/checkout@v2 @@ -38,7 +38,7 @@ jobs: contents: 'read' strategy: matrix: - python-version: [ '3.9' ] + python-version: [ '3.8', '3.9' ] steps: - name: Check out code uses: actions/checkout@v2 diff --git a/README.md b/README.md index c1fcf03..ac5ff74 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,96 @@ -# FastAPI Endpoints +# FastAPI Endpoints -This is a file-based router for FastAPI that automatically discovers and registers route files based on their filenames. -This tool simplifies the organization and scaling of your FastAPI projects by allowing you to structure your endpoints in a modular way. +[![CI checks](https://github.com/vladNed/fastapi-endpoints/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/vladNed/fastapi-endpoints/actions/workflows/ci.yaml) + +This is a file-based router for FastAPI that automatically discovers and registers route files based on their filenames. +This tool simplifies the organization and scaling of your FastAPI projects by allowing you to structure your endpoints in a modular way. With `fastapi-endpoints`, you can easily manage complex applications by keeping your routes clean, intuitive, and maintainable. +## Installation + +```bash +pip install fastapi-endpoints +``` + +## Usage + +### Auto discovery + +The auto discovery feature allows you to organize your routes in separate files and directories and will be automatically registered by the router. +All routers should be under the `routers` directory within your FastAPI application. + +```python +# src/app.py + +from fastapi import FastAPI +from fastapi_endpoints import auto_include_routers + +from . import routers + +app = FastAPI() +auto_include_routers(app, routers) +``` + +```python +# src/routers/users.py +from fastapi import APIRouter + +users_router = APIRouter() + +# Define your routes here +``` + +The routes under `src/routers/users.py` will be automatically registered by the router. The prefix to the routes will be the path to the file relative to the `routers` directory. +For the example above, the routes will be available under `/users`. + +The auto discovery feature also supports nested directories. For example, if you have the following directory structure: + +``` +routers +|── __init__.py +├── api_v1 +│ ├── __init__.py +│ ├── users.py +│ └── posts.py +└── api_v2 + ├── __init__.py + ├── users.py + └── posts.py +app.py +``` + +The routes under `src/routers/api_v1/users.py` will be available under `/api/v1/users`. +The same applies to the other files. The routes under `src/routers/api_v2/users.py` will be available under `/api/v2/users`. + + +## Development + +You are required to have [Poetry](https://python-poetry.org/) installed in your system to manage the dependencies. + +### Setup + +```bash +poetry install +``` + +### Running tests + +```bash +pytest +``` + +### Linting & Formatting + +To check the format: +```bash +ruff format --check src/ +``` + +To check the lint: +```bash +ruff check src/ +``` + +After running both commands, the format errors can be applied by running the formatter without the `--check` flag. + + diff --git a/poetry.lock b/poetry.lock index 83077d7..9d8a2b3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -11,6 +11,9 @@ files = [ {file = "annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89"}, ] +[package.dependencies] +typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.9\""} + [[package]] name = "anyio" version = "4.4.0" @@ -470,5 +473,5 @@ files = [ [metadata] lock-version = "2.0" -python-versions = "^3.9.1" -content-hash = "73552432cdaa511431e8e0042809daeb36510c0b1573fc1ebcc6fe7b79fc9a1c" +python-versions = "^3.8" +content-hash = "1313c22961e59f1e964a8f4b2a462aabdc729cd397a706b5e3612e33f464d167" diff --git a/pyproject.toml b/pyproject.toml index 55f7233..296f689 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ packages = [ ] [tool.poetry.dependencies] -python = "^3.9.1" +python = "^3.8" fastapi = "^0.112.2"