Backend Architecture
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The backend architecture of the Fikak application is built as a containerized Python-based service utilizing the FastAPI framework. It serves as the core orchestration layer, managing data persistence via a PostgreSQL database and providing a RESTful API for the frontend application.
The system is designed for modularity, separating the API routing logic, database connection management, and data modeling into distinct components. This structure ensures that the application can handle concurrent requests efficiently while maintaining strict data integrity through an Object-Relational Mapping (ORM) layer.
Sources: backend/main.py, docker-compose.yml:5-15, backend/requirements.txt
System Component Overview
The backend is composed of several interacting layers that handle incoming HTTP requests, process business logic, and interface with the storage engine.
Core Technology Stack
| Component | Technology | Description |
|---|---|---|
| Web Framework | FastAPI | Asynchronous web framework for building APIs. |
| Database | PostgreSQL | Relational database for persistent storage. |
| ORM | SQLAlchemy | SQL toolkit and Object-Relational Mapper. |
| Containerization | Docker | Deployment and environment isolation. |
| Language | Python | Primary programming language. |
Sources: backend/requirements.txt, docker-compose.yml:17-27, backend/main.py
High-Level Architecture Flow
The following diagram illustrates the interaction between the client, the FastAPI application server, and the database.
The architecture leverages Docker to bridge the application service with the database service using a shared network.
Sources: docker-compose.yml:1-30, backend/main.py
Database Design and Persistence
The persistence layer is managed through SQLAlchemy, which abstracts the underlying PostgreSQL database. The configuration is centralized to manage sessions and engine initialization.
Data Modeling
Models are defined as Python classes inheriting from a declarative base. These models map directly to database tables, defining schemas and relationships.
The database initialization sequence ensures that tables are created or verified upon application startup.
Sources: backend/models.py, backend/database.py
Session Management
The application uses a dependency injection pattern to provide database sessions to API endpoints, ensuring that connections are properly closed after each request.
# Dependency to get DB session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
Sources: backend/database.py:10-15, backend/main.py:20-25
API Implementation
The main.py file serves as the entry point, configuring middleware, including routers, and defining global exception handlers.
Request Lifecycle
- Middleware Processing: Handles CORS (Cross-Origin Resource Sharing) and logging.
- Routing: Matches the incoming URL to a specific function handler.
- Dependency Injection: Resolves required resources like database sessions or authentication tokens.
- Logic Execution: Interacts with models and processes data.
- Serialization: Converts Python objects into JSON responses.
Sources: backend/main.py, backend/database.py
Deployment Configuration
The backend environment is defined in docker-compose.yml, specifying environment variables for database connectivity and volume mapping for persistent data storage.
backend:
build: ./backend
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/fikak
depends_on:
- db
Sources: docker-compose.yml:5-15
Conclusion
The backend architecture of the Fikak app provides a robust foundation using modern Python tools. By separating concerns between the API layer (FastAPI), the data layer (SQLAlchemy/PostgreSQL), and the infrastructure layer (Docker), the system achieves a scalable and maintainable environment for application development.