fastapi_best_architecture
                                
                                 fastapi_best_architecture copied to clipboard
                                
                                    fastapi_best_architecture copied to clipboard
                            
                            
                            
                        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...
FastAPI Best Architecture
[!CAUTION] For 2024-3-22 (announcement)
The master branch has completed the app architecture refactoring, please pay extra attention to sync fork operations to avoid irreparable damage!
We have kept and locked the original branch (legacy-single-app-pydantic-v2), which you can get in the branch selector
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
Its purpose is to allow you to use it directly as the infrastructure of your new project, this repository as a template library open to any person or enterprise can be used for free!
🔥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
username / password: admin / 123456
Features
- [x] Design with FastAPI PEP 593 Annotated Parameters
- [x] Global asynchronous design with async/await + asgiref
- [x] Follows Restful API specification
- [x] Global SQLAlchemy 2.0 syntax
- [x] Pydantic v1 and v2 (different branches)
- [x] Casbin RBAC access control model
- [x] Role menu RBAC access control model
- [x] Celery asynchronous tasks
- [x] JWT middleware whitelist authentication
- [x] Global customizable time zone time
- [x] Docker / Docker-compose deployment
- [x] Pytest Unit Testing
Built-in features
- [x] User management: system user role management, permission assignment
- [x] Department Management: Configure the system organization (company, department, group...)
- [x] Menu Management: Configuration of system menus, user menus, button privilege identification
- [x] Role Management: Assign role menu privileges, assign role routing privileges
- [x] Dictionary Management: Maintain common fixed data or parameters within the system.
- [x] Operation Logs: logging and querying of normal and abnormal system operations.
- [x] Login Authentication: graphical authentication code background authentication login
- [x] Login Logs: Logging and querying of normal and abnormal user logins
- [x] Service Monitoring: server hardware device information and status
- [x] Scheduled tasks: automated tasks, asynchronous tasks, and function invocation are supported
- [x] Interface Documentation: Automatically generate online interactive API interface documentation.
Local development
- Python: 3.10+
- Mysql: 8.0+
- Redis: The latest stable version is recommended
- Nodejs: 14.0+
Backend
- 
Enter the backenddirectorycd backend
- 
Install the dependencies pip install -r requirements.txt
- 
Create a database fbawith utf8mb4 encoding.
- 
Install and start Redis 
- 
Create a .envfile in thebackenddirectory.touch .env cp .env.example .env
- 
Modify the configuration files core/conf.pyand.envas needed.
- 
database migration alembic # Generate the migration file alembic revision --autogenerate # Execute the migration alembic upgrade head
- 
Start celery worker, beat and flower celery -A app.task.celery worker -l info # Scheduled tasks (optional) celery -A app.task.celery beat -l info # Web monitor (optional) celery -A app.task.celery flower --port=8555 --basic-auth=admin:123456
- 
Initialize test data (Optional) 
- 
Execute the main.pyfile to start the service
- 
Open a browser and visit: http://127.0.0.1:8000/api/v1/docs 
Front end
Jump to fastapi_best_architecture_ui View details
Docker Deployment
[!WARNING]
Default port conflicts: 8000, 3306, 6379, 5672.
It is recommended to shut down local services: mysql, redis, rabbitmq... before deployment.
- 
Go to the deploy/backend/docker-composedirectory, and create the environment variable file.env.cd deploy/backend/docker-compose touch .env.server ../../../backend/.env cp .env.server ../../../backend/.env
- 
Modify the configuration files backend/core/conf.pyand.envas needed.
- 
Execute the one-click startup command docker-compose up -d --build
- 
Wait for the command to complete. 
- 
Open a browser and visit: http://127.0.0.1:8000/api/v1/docs 
Test data
Initialize the test data using the backend/sql/init_test_data.sql file.
Development Process
(For reference only)
- define the database model (model)
- define the data validation model (schema)
- define the view (api) and routing (router)
- write business (service)
- write database operations (crud)
Testing
Execute unit tests through pytest.
- 
create a test database fba_testwith utf8mb4 encoding
- 
create database tables using the backend/sql/create_tables.sqlfile
- 
initialize the test data using the backend/sql/init_pytest_data.sqlfile
- 
Go to the backenddirectory and execute the test commands.cd backend/ pytest -vs --disable-warnings
Status
Contributors
Special thanks
Interactivity
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