This repository contains a complete backend solution for an Online Mart, built using an event-driven microservices architecture. The system is designed to be scalable, reliable, and efficient, using independent microservices that communicate asynchronously.
The backend system of the Online Mart manages user authentication, product management, order processing, inventory updates, notifications, and payments. Each service operates independently and communicates asynchronously using Kafka. The project is built with Python, FastAPI, SqlModel, and other modern tools.
The system is designed using microservices architecture with each service focusing on a specific domain:
- User Service: Handles user authentication, registration, and profiles.
- Product Service: Manages product catalog and CRUD operations.
- Order Service: Processes orders and manages their statuses.
- Inventory Service: Tracks and updates product inventory.
- Notification Service: Sends user notifications (e.g., email, SMS).
- Payment Service: Integrates with Stripe for payment processing.
The services are connected via Kafka for asynchronous communication, while Kong serves as the API Gateway for secure access.
- Backend Framework: Python, FastAPI, SqlModel
- Containerization: Docker, Docker Compose
- Database: PostgreSQL
- Event Broker: Kafka
- API Gateway & Load Balancer: Kong
- Payment Integration: Stripe
- Serialization: Protocol Buffers
- Authentication: JWT
To run this project locally, you must have:
- Docker Desktop installed (along with WSL on Windows)
- Or Docker installed on Linux
- Clone the repository:
git clone https://github.com/zaka-urrehman/online_mart.git cd online_mart
- Set up Environment Variables:
- Set the necessary variables like USER_SECRET_KEY, DATABASE_URL, STRIPE_API_KEY, etc
- Start Docker Containers:
- Run the following command to start all services
All Containers must set up automatically. You don't have to do it manually. Once all the services are up, you can run the project by the following steps.docker-compose up --build
- Once the containers are running, Kong's configuration will be set up automatically.
- To verify:
- Open your browser and navigate to
http://localhost:8003
. - You can verify routes and gateway services here for each container.
- Open your browser and navigate to
-
Ensure JWT authentication is properly configured:
- Go to Kong Admin UI (or appropriate port for Kong).
- In the Consumers tab, select the consumer created for the user-service (should be created automatically).
- If not present, create a new consumer with a username (e.g.,
example_user
) and add JWT credentials for that consumer. Your credentials for consumer and your USER_SERCRET_KEY in your .env file in user-service should be same. - Copy the Credential ID of the consumer.
- Update the
.env
file in the user-service with this Credential ID underISS
.
Once updated, restart the user-service to apply the changes:
docker-compose restart user-service
-
To enable payment processing:
- Copy your Stripe API key and paste it in the
.env
file asSTRIPE_API_KEY
. - Once the containers are running, view the logs of the Stripe CLI container to retrieve the webhook secret:
docker logs -f stripe-cli
If you don't find the webhook secret, it might be because the container haven't picked up the env for stripe API key. You can restart the container and you will see your webhook secret.
- Copy the displayed webhook secret and update the
.env
file of the payment-service withWEBHOOK_SECRET
. - Restart the payment-service container to apply the changes:
docker-compose restart payment-service
- Copy your Stripe API key and paste it in the
-
Access the FastAPI documentation: Go to the following URLs in your browser to access the API documentation (Swagger UI) for each service:
- User Service: http://localhost:8000/user-service/docs
- Product Service: http://localhost:8000/product-service/docs
- Order Service: http://localhost:8000/inventory-service/docs
- Inventory Service: http://localhost:8000/order-service/docs
-
Test authentication:
- Under the user-service Use the add-user endpoint to add a user and add address endpoint to add address for user which will later be used for placing order.
- Use the login endpoint of the User Service to generate a JWT token.
- Copy the token and use it as the Authorization header in the subsequent service requests.
-
Add a Product:
- Add a Category, Size and then a product.
- Use the Admin Account for this purpose as only admin can do these operations on products etc. You can create an Admin account by navigating to the add-admin endpoint under the user-service.
-
Place an order:
- Add a product to cart under the order-service.
- Use the Create Order endpoint in the Order Service to place a new order.
- This triggers payment processing via Stripe and updates the order status upon successful payment.
-
Check inventory:
- Use the Inventory Service endpoints to increase, decrease, or set product quantities.
- Use only admin account as user account will be restricted from inventory-service.
-
Review Kafka events:
- Check Kafka UI at http://localhost:9080 to monitor event topics and messages exchanged between services.
This completes the testing of all microservices.
To stop all running containers and services:
- Run the following command:
docker compose down
-
Issue: Services not starting as expected.
- Solution: Ensure Docker Desktop or Docker is running correctly, and try using
docker compose up --build
to rebuild the images.
- Solution: Ensure Docker Desktop or Docker is running correctly, and try using
-
Issue: JWT verification fails even after a token is generated.
- Solution: Verify the JWT token's
iss
claim is set correctly, matching the Kong consumer ID.
- Solution: Verify the JWT token's
-
Issue: Payment Service is not processing payments.
- Solution: Ensure that the Stripe webhook secret is set correctly in the Payment Service’s
.env
file.
- Solution: Ensure that the Stripe webhook secret is set correctly in the Payment Service’s
-
Issue: Kafka messages not being consumed.
- Solution: Restart the services depending on Kafka, especially the consumers, to ensure proper connectivity.
You’ve successfully set up the backend for the Online Mart application. This project showcases an event-driven microservices architecture with secure authentication and payment processing. For any adjustments or extensions, refer to the codebase and documentation. Thank you for trying it out!