This is a robust, scalable, and secure Django-based API for a social media platform, featuring Firebase Authentication, PostgreSQL database, and Docker containerization.
Live API: https://codeleap-api.upafiliado.com.br/
- Firebase Authentication: Integrated with Google Login. The backend validates tokens and automatically provisions users.
- Social Core: Complete CRUD for Posts and Comments.
- Interactions: Atomic "Like" system with high-concurrency safety.
- Performance: Denormalized counters (
likes_count,comments_count) usingF()expressions to ensure database efficiency. - Soft Delete: Posts and comments are never physically deleted from the database.
- Reasoning: This ensures data integrity and preserves historical records for auditing, analytics, and legal compliance (e.g., investigating reported content or fulfilling legal requests). Items are simply filtered out from the API results via the
deleted=Trueflag.
- Reasoning: This ensures data integrity and preserves historical records for auditing, analytics, and legal compliance (e.g., investigating reported content or fulfilling legal requests). Items are simply filtered out from the API results via the
- Ownership Security: Strict permissions ensuring only authors can modify their content.
- Scalable Pagination: Global pagination set to 10 items per page.
- Dockerized: Ready for development and production with Docker Compose.
- Full Test Suite: Comprehensive unit tests with Firebase mocking.
To ensure high performance and scalability under heavy load, several optimization techniques were applied:
- Denormalized Counters:
likes_countandcomments_countare stored directly on thePostmodel. This eliminates the need for expensiveCOUNT(*)queries during feed listing, resulting inO(1)read performance for these metrics. - Atomic F() Expressions: All counter updates use Django's
F()expressions. This ensures that increments and decrements happen at the database level (SQL), preventing race conditions and ensuring data consistency even with multiple concurrent requests. - Composite Indexing:
- A composite index was added for
[post_id, created_datetime]in theCommentmodel to ensure that fetching paginated comments for a specific post is near-instant. - A unique index for
[user_id, post_id]in theLikemodel ensures database-level integrity and fast lookup for like states.
- A composite index was added for
- Soft Delete Filtering: Optimized queries ensure that deleted items are filtered out efficiently at the database level.
- Backend: Django 6.0+, Django REST Framework (DRF)
- Database: PostgreSQL 16
- Auth: Firebase Admin SDK
- DevOps: Docker, Docker Compose
- Testing: Django APITestCase (Unit Testing)
- Docker & Docker Compose
- Python 3.12 (if running locally)
Copy the template and fill in your credentials:
cp .env.example .env- Go to your Firebase Console -> Project Settings -> Service Accounts.
- Click Generate new private key and download the JSON file.
- Rename it to
firebase-service-account.jsonand place it in the root directory of this project.
Build and start the containers:
docker-compose up --buildThe API will be available at http://localhost:8000/api/v1/careers/.
All requests (except Admin) require a Firebase ID Token in the header:
Authorization: Bearer <YOUR_FIREBASE_ID_TOKEN>
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/careers/ |
List all posts (paginated). |
| POST | /api/v1/careers/ |
Create a new post. |
| PATCH | /api/v1/careers/{id}/ |
Update your own post. |
| DELETE | /api/v1/careers/{id}/ |
Soft delete your own post. |
| POST | /api/v1/careers/{id}/like/ |
Toggle like/unlike on a post. |
| GET | /api/v1/careers/{id}/comments/ |
List comments for a post. |
| POST | /api/v1/careers/{id}/comments/ |
Add a comment to a post. |
| PATCH | /api/v1/comments/{id}/ |
Edit your own comment. |
| DELETE | /api/v1/comments/{id}/ |
Soft delete your own comment. |
To run the full test suite (including Firebase auth mocks):
# Inside Docker
docker-compose exec web python manage.py test
# Locally
python manage.py testDEBUGshould be set toFalsein production.CORS_ALLOW_ALL_ORIGINSis enabled for development but should be restricted in production.- The
firebase-service-account.jsonand.envfiles are ignored by Git for your safety.