Python FastAPI Template

September 28, 2023 · View on GitHub

中文文档请查看 README_zh.md

Basic Features

  • Basic framework
  • Switchable environment configurations
  • Automatic code reloading
  • SQLAlchemy-based ORM
  • Swagger API documentation support
  • Logging using the logure framework
  • JWT authentication
  • RUFF code standards
  • One-line command to generate CRUD templates including model, schema, and router
  • Complete CI/CD workflow and production environment Docker-compose automatic deployment template
  • Using RUFF to standardize code

Getting Started

1. Install Poetry

pip install poetry

2. Install Dependencies

poetry install

3. Start Development

poetry run app

4. Switch Environments

poetry run app env=dev  # Development environment (default)
poetry run app env=prod # Production environment

In dev environment, the development server will automatically reload the code

! Note: After automatic appending, if you need to undo it, please manually delete the appended code. Do not use Ctrl+Z in the IDE, as it may cause code formatting issues.

1. Execute the following command

poetry run create_crud name={data_model_name} -a  # Use `_` as a separator for multi-word names, case-insensitive

For example:

poetry run create_crud name=example -a

Generate CRUD Templates (Manually add model creation and router inclusion)

1. Execute the following command

poetry run create_crud name={data_model_name} -a  # Use `_` as a separator for multi-word names, case-insensitive

For example:

poetry run create_crud name=example

2. Add automatic table creation

In src/models/__init__.py, import your associated models under # $table_create$:

# $table_create$ 自动创建表追加锚 *请不要修改此行* (Anchor of the table creation line *Do not modify this line*)
from .{data_model_name} import DB{DataModelClassName}

3. Mount the router

In src/app.py, under Mount the router tables, add the router:

from src.routers.{data_model_name} import router as {data_model_name}_router

app.include_router({data_model_name}_router, prefix="/{router_prefix}", tags=["{router_tag}"])

Please replace {data_model_name}, {DataModelClassName}, {router_prefix}, and {router_tag} with the appropriate values as needed for your project.