A sample backend application in FastAPI, Python and MongoDB
git clone https://github.com/me-heer/ecom-backend.git
cd ecom-backend
pip3 install -r requirements.txt
uvicorn main:app
After uvicorn main:app, the server runs on http://127.0.0.1:8000
.
Copy and paste the sample curl calls below and test the output.
You don't need to install MongoDB, since this app will connect to a test instance of MongoDB Atlas with sample data. The connection string of MongoDB is purposefully pushed to this repository for ease of access while testing this.
curl -X GET --location "http://127.0.0.1:8000/products?limit=3&min_price=15&max_price=25" \
-H "Accept: application/json"
curl -X POST --location "http://127.0.0.1:8000/orders" \
-H "Content-Type: application/json" \
-d '{
"items": [
{"product_id": "65b77503b064d441b7d4c2b7", "bought_quantity": 1}
],
"user_address": {
"city": "Mumbai",
"country": "India",
"zip_code": "12345"
}
}'
Ideally, I would like to have the following things in a project, but since this was a sample project, I refrained from working on them due to time constraints:
- Unit/Integration/End-to-end tests
- When new orders are created, we should be using locks/mutexes or other techniques to prevent race conditions.
- When the scale of this project grows, use N-tier architecture (Controller -> Service -> Repository)
- Move MongoDB configurations to its own file
- Use Pydantic Model Validations instead of manual code
- Logging
- Remove
.env
file from the repository (I have just kept it so that anyone can run this) - API Documentation (i.e.: Swagger)
- Authentication/Authorisation (of course)
- Rate Limiting
- More Validations