fastapi-practices / fastapi_best_architecture

The RBAC permission control system built based on FastAPI adopts a unique pseudo-three-layer architecture design, built-in fastapi-admin basic implementation, and is free and open source as a template library.
https://fastapi-practices.github.io/fastapi_best_architecture_docs/
MIT License
575 stars 100 forks source link
fastapi fastapi-admin fastapi-rbac fastapi-sqlalchemy fastapi-template pycasbin pydantic pydantic-v2

FastAPI Best Architecture

GitHub Static Badge Ruff Pydantic v2

[!NOTE] This repository as a template library open to any person or enterprise can be used for free!

English | ็ฎ€ไฝ“ไธญๆ–‡

FastAPI framework based on the front-end and back-end separation of the middle and back-end solutions, follow the pseudo three-tier architecture design, support for python3.10 and above versions

๐Ÿ”ฅContinuously updated and maintained๐Ÿ”ฅ

Pseudo three-tier architecture

The mvc architecture is a common design pattern in python web, but the three-tier architecture is even more fascinating

In python web development, there is no common standard for the concept of three-tier architecture, so we'll call it a pseudo three-tier architecture here

But please note that we don't have a traditional multi-app structure (django, springBoot...) If you don't like this pattern, use templates to transform it to your heart's content!

workflow java fastapi_best_architecture
view controller api
data transmit dto schema
business logic service + impl service
data access dao / mapper crud
model model / entity model

Online Demo

You can view some of the preview screenshots in fastapi_best_architecture_ui

Luckily, we now have a demo site: FBA UI

tester: test / 123456

super: admin / 123456

Features

Built-in features

Project structure

โ”œโ”€๐Ÿ“ backend--------------- # Backend
โ”‚โ€ƒโ”œโ”€๐Ÿ“ alembic------------- # DB migration
โ”‚โ€ƒโ”œโ”€๐Ÿ“ app----------------- # Application
โ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ admin------------- # System admin
โ”‚โ€ƒโ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ api------------- # Interface
โ”‚โ€ƒโ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ crud------------ # CRUD
โ”‚โ€ƒโ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ model----------- # SQLA model
โ”‚โ€ƒโ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ schema---------- # Data transmit
โ”‚โ€ƒโ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ service--------- # Service
โ”‚โ€ƒโ”‚โ€ƒโ”‚โ€ƒโ””โ”€๐Ÿ“ tests----------- # Pytest
โ”‚โ€ƒโ”‚โ€ƒโ”œโ”€๐Ÿ“ generator--------- # Code generate
โ”‚โ€ƒโ”‚โ€ƒโ””โ”€๐Ÿ“ task-------------- # Celery task
โ”‚โ€ƒโ”œโ”€๐Ÿ“ common-------------- # public resources
โ”‚โ€ƒโ”œโ”€๐Ÿ“ core---------------- # Core configuration
โ”‚โ€ƒโ”œโ”€๐Ÿ“ database------------ # Database connection
โ”‚โ€ƒโ”œโ”€๐Ÿ“ log----------------- # Log
โ”‚โ€ƒโ”œโ”€๐Ÿ“ middleware---------- # Middlewares
โ”‚โ€ƒโ”œโ”€๐Ÿ“ scripts------------- # Scripts
โ”‚โ€ƒโ”œโ”€๐Ÿ“ sql----------------- # SQL files
โ”‚โ€ƒโ”œโ”€๐Ÿ“ static-------------- # Static files
โ”‚โ€ƒโ”œโ”€๐Ÿ“ templates----------- # Template files
โ”‚โ€ƒโ””โ”€๐Ÿ“ utils--------------- # Toolkit
โ””โ”€๐Ÿ“ deploy---------------- # Server deployment

Local development / Docker deployment

For more details, please check the official documentation

Test data

Initialize the test data using the backend/sql/init_test_data.sql file.

Development Process

(For reference only)

  1. define the database model (model)
  2. define the data validation model (schema)
  3. define the view (api) and routing (router)
  4. write business (service)
  5. write database operations (crud)

Testing

Execute unit tests through pytest.

  1. create a test database fba_test with utf8mb4 encoding
  2. create database tables using the backend/sql/create_tables.sql file
  3. initialize the test data using the backend/sql/init_pytest_data.sql file
  4. Go to the backend directory and execute the test commands.

    cd backend/
    
    pytest -vs --disable-warnings

Status

Alt

Contributors

Special thanks

Interactivity

WeChat / QQ

Sponsor us

If this program has helped you, you can sponsor us with some coffee beans: :coffee: Sponsor :coffee:

License

This project is licensed by the terms of the MIT license

Stargazers over time