/fastapi_best_architecture

基于 FastAPI 框架的前后端分离中后台解决方案,遵循伪三层架构设计, 支持 Python 3.10 及以上版本

Primary LanguagePythonMIT LicenseMIT

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

  • Design with FastAPI PEP 593 Annotated Parameters
  • Global asynchronous design with async/await + asgiref
  • Follows Restful API specification
  • Global SQLAlchemy 2.0 syntax
  • Pydantic v1 and v2 (different branches)
  • Casbin RBAC access control model
  • Role menu RBAC access control model
  • Celery asynchronous tasks
  • JWT middleware whitelist authentication
  • Global customizable time zone time
  • Docker / Docker-compose deployment
  • Pytest Unit Testing

Built-in features

  • User management: management of system user roles, assignment of permissions
  • Departmental management: Configuration of the system organization (company, department, group, ...)
  • Menu management: Configuration of system menus, user menus, button permission labels
  • Role management: assignment of role menu privileges, assignment of role routing privileges
  • Dictionary management: maintenance of commonly used fixed data or parameters within the system
  • Code generation: back-end code is automatically generated, supporting preview, write and download.
  • Operation log: logging and querying of normal and abnormal system operations.
  • Login authentication: graphical captcha backend authentication login
  • Logging: logging and querying of normal and abnormal user logins
  • Service monitoring: server hardware device information and status
  • Timed tasks: automated tasks, asynchronous tasks, support for function calls
  • Interface Documentation: Automatically generate online interactive API interface documentation.

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

TG / Discord

Sponsor us

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

License

This project is licensed by the terms of the MIT license

Stargazers over time