This project is an API for a mart system that supports product management, order placement, and user authentication and authorization. It utilizes FastAPI for the backend, PostgreSQL for the database, and Docker for containerization. The project is structured with multiple services including an API service, an authentication service, a PostgreSQL database, and an SMTP service for email notifications.
The project consists of the following services:
- API Service: Handles product and order-related operations.
- Auth Service: Manages user authentication and authorization.
- PostgreSQL Database: Stores user, product, and order data.
- SMTP Service: Handles email notifications for user registration and account verification.
- Docker
- Docker Compose
- Poetry (for managing Python dependencies)
-
Clone the repository:
git clone https://github.com/iamshoaibxyz/mart-projects.git cd 7-basic-mart-with-auth
-
Setup Environment Variables:
Create a
.env
file in the root directory and add the following environment variables:DATABASE_URL=postgresql://shoaib:mypassword@db:5432/mydatabase JWT_SECRET=your_jwt_secret JWT_ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30 USER_CONTEXT=your_user_context
-
Build and Run the Services:
Use Docker Compose to build and start the services:
docker-compose up --build
This will start the following services:
- API service on port 8000
- Auth service on port 8002
- PostgreSQL database on port 5432
- SMTP service on ports 8025 (web interface) and 1025 (SMTP server)
- GET /: Root endpoint for the API service.
- POST /order/: Place an order.
- GET /get-all-products: Retrieve all products.
- GET /get-products-by-category/{product_category}: Retrieve products by category.
- GET /get-product/{product_id}: Retrieve a specific product by ID.
- POST /add-product: Add a new product.
- PATCH /increment_product_item/{product_id}: Increment product quantity.
- PATCH /update_product/{product_id}: Update product details.
- GET /get_orders: Retrieve all orders.
- POST /auth/signup: Create a new user account.
- POST /auth/login: User login.
- POST /auth/account-verify: Verify user account.
- GET /auth/users: Retrieve all user accounts (requires authentication).
- GET /auth/tokens: Retrieve all user tokens (requires authentication).
-
User Signup:
curl -X POST "http://127.0.0.1:8002/auth/signup" -H "Content-Type: application/json" -d '{"first_name": "John", "last_name": "Doe", "email": "[email protected]", "password": "StrongPassword123!"}'
-
User Login:
curl -X POST "http://127.0.0.1:8002/auth/login" -H "Content-Type: application/x-www-form-urlencoded" -d "[email protected]&password=StrongPassword123!"
-
Add Product:
curl -X POST "http://127.0.0.1:8000/add-product" -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d '{"name": "Apple", "category": "food", "price": 1, "quantity": 100}'
-
Place Order:
curl -X POST "http://127.0.0.1:8000/order/" -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d '{"product_id": "<product_uuid>", "quantity": 2}'
- Ensure the JWT secret is kept secure and not shared publicly.
- For email functionality, update the SMTP configuration in
app/config/email.py
to match your email server settings.
- Database Connection Issues: Ensure that the PostgreSQL service is running and accessible.
- Authentication Errors: Verify that the JWT token is correctly passed in the
Authorization
header.
This README provides a comprehensive guide to setting up and running the Mart API project, allowing developers to quickly get started and understand the project's functionality.