Giter Site home page Giter Site logo

wu-clan / fastapi_best_architecture Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fastapi-practices/fastapi_best_architecture

11.0 1.0 2.0 4.85 MB

基于 FastAPI 构建的前后端分离 RBAC 权限控制系统,采用独特的伪三层架构模型设计,内置 fastapi-admin 基本实现,并作为模板库免费开源

License: MIT License

Shell 0.15% Python 99.21% Mako 0.15% Dockerfile 0.50%

fastapi_best_architecture's Introduction

FastAPI Best Architecture

GitHub Static Badge Ruff Pydantic v2

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

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------------- # Database Migrations
│ ├─📁 app----------------- # Application
│ │ ├─📁 admin------------- # Admin Panel
│ │ │ ├─📁 api------------- # API
│ │ │ │ └─📁 v1------------ # API Version
│ │ │ ├─📁 crud------------ # CRUD Operations
│ │ │ ├─📁 model----------- # Models
│ │ │ ├─📁 schema---------- # Data Transfers
│ │ │ ├─📁 service--------- # Services
│ │ │ └─📁 tests----------- # Tests
│ │ ├─📁 generator--------- # Code Generator
│ │ └─📁 task-------------- # Task Application
│ │   └─📁 celery_task----- # Task Layers
│ ├─📁 common-------------- # Common Resources
│ │ ├─📁 exception--------- # Exceptions
│ │ ├─📁 response---------- # Responses
│ │ └─📁 security---------- # Security
│ ├─📁 core---------------- # Core Configuration
│ ├─📁 database------------ # Database Connection
│ ├─📁 log----------------- # Log Storage
│ ├─📁 middleware---------- # Middleware
│ ├─📁 scripts------------- # Scripts
│ ├─📁 sql----------------- # SQL Files
│ ├─📁 static-------------- # Static Files
│ ├─📁 templates----------- # Templates
│ │ └─📁 py---------------- # Python Code Generation
│ └─📁 utils--------------- # Utilities
└─📁 deploy---------------- # Deployment
  └─📁 backend------------- # Backend Deployment
    └─📁 docker-compose---- # Docker Deployment

Local development

  • Python: 3.10+
  • Mysql: 8.0+
  • Redis: The latest stable version is recommended
  • Nodejs: 14.0+

Backend

  1. Enter the backend directory

    cd backend
  2. Install the dependencies

    pip install -r requirements.txt
  3. Create a database fba with utf8mb4 encoding.

  4. Install and start Redis

  5. Create a .env file in the backend directory.

    touch .env
    
    cp .env.example .env
  6. Modify the configuration files core/conf.py and .env as needed.

  7. database migration alembic

    # Generate the migration file
    alembic revision --autogenerate
    
    # Execute the migration
    alembic upgrade head
  8. 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
  9. Initialize test data (Optional)

  10. Start fastapi service

# Help
fastapi --help

# Dev mode
fastapi dev main.py
  1. 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.

  1. Go to the deploy/backend/docker-compose directory, and create the environment variable file .env.

    cd deploy/backend/docker-compose
    
    touch .env.server ../../../backend/.env
    
    cp .env.server ../../../backend/.env
  2. Modify the configuration files backend/core/conf.py and .env as needed.

  3. Execute the one-click startup command

    docker-compose up -d --build
  4. Wait for the command to complete.

  5. 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)

  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: ☕ Sponsor ☕

License

This project is licensed by the terms of the MIT license

Stargazers over time

fastapi_best_architecture's People

Contributors

dependabot[bot] avatar downdawn avatar downdawn66 avatar obrua avatar wu-clan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.