Hi, it's Hai

Software Engineering, Game Development, Storytelling, and more!


Journal


Youtube


Twitter


GitHub


GitLab


Discord


© 2023. All rights reserved.

Hi, it's Hai

Software Engineering, Game Development, Storytelling, and more!

Architecture for a large FastAPI project: Applying the MVC pattern

Posted on August 7, 2023

Introduction

Building a large-scale FastAPI project requires careful consideration of the project's architecture to ensure scalability, maintainability, and performance. FastAPI, a modern and high-performance web framework, provides an excellent foundation for developing robust and efficient APIs. However, as the project grows in size and complexity, it becomes crucial to design a well-structured architecture that can handle the demands of a large-scale application. In this blog post, we will explore the key considerations and best practices for architecting a large FastAPI project. From organizing project structure to managing dependencies and implementing modular design patterns, we will delve into the essential elements that contribute to a scalable and maintainable architecture for large FastAPI projects. Whether you're starting a new project or looking to improve an existing one, this guide will provide valuable insights to help you design an architecture that can support your project's growth and success.

The MVC (Model-View-Controller) pattern is a software architectural design pattern that separates an application into three components: the Model, responsible for managing data and business logic; the View, responsible for presenting data to the user; and the Controller, which handles user input, processes it, and updates the Model and View accordingly. By separating concerns, the MVC pattern promotes modularity, maintainability, and reusability, allowing for efficient development and management of applications.

The structure

Here is the folder structure for your project:

project_root
├── __init__.py
├── main.py
├── core
│   ├── models
│   │   ├── database.py
│   │   └── __init__.py
│   ├── schemas
│   │   ├── __init__.py
│   │   └── schema.py
│   └── settings.py
├── tests
│   ├── __init__.py
│   └── v1
│       ├── __init__.py
│       └── test_v1.py
└── v1
    ├── api.py
    ├── endpoints
    │   ├── endpoint.py
    │   └── __init__.py
    └── __init__.py 

The 'models' folder is designated for storing your database models, enabling you to import the shared database session or object seamlessly across both v1 and v2 versions.

The 'schemas' are essentially your Pydantic models. We refer to them as schemas because they are used to create OpenAPI schemas, which are essential for FastAPI as it is based on the OpenAPI specification. Schemas play a crucial role throughout the entire project, from generating Swagger documentation to defining the expected request body for each endpoint.

Having 'views' is not mandatory unless if you are going to render your frontend with some template engine like Jinja.

Do your testing inside 'tests'.

Instead of defining all your APIs inside a single file, you can create them independently by APIRouter.