Skip to content

zobayer1/FastAPI-Hexagonal-Architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Hexagonal Architecture Demo

This is a FastAPI demo application implementing the Hexagonal Architecture.

Breakdown

The services module holds our domain implementations. We expose various interfaces (implemented using abstract classes) as ports that can be used by the adapters from the driving and driven modules.

In this example, the path operation endpoint function save_data acts as the driving adapter, and the concrete subclass MemoryDataPort of port abstract class MyDataPort acts as the driven adapter.

Path operation method save_data invokes the save method from the concrete implementation class MyServiceImpl. A dependency on the driven class MemoryDataport is injected from the path operation function to the domain business logic implementation using FastAPI's Depends mechanism.

The difference between a N-tier and hexagonal architecture is that the adapter MemoryDataPort implements interfaces from the domain's port instead of the domain logic implementing an interface from a lower level.

Installation

Create a Python 3.11 virtual environment and install dependencies from the requirements file.

python3.11 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Run Server

Run Uvicorn server

uvicorn app.main:app --reload

Test using Postman or cURL by tweaking the data query parameter:

curl --location --request PUT 'http://localhost:8000?data=true'

Run Tests

To run the tests, simply run pytest.

pytest -s

References

About

This is a FastAPI demo application implementing the Hexagonal Architecture

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages