This is a FastAPI server for managing orders with real-time order status updates via WebSocket. The project is built with SQLModel and PostgreSQL (SQLite for testing). It is containerized with Docker and ready for further deployment enhancements.
- Local Development:
http://localhost:80 - Production:
http://your-ec2-ip:80
- REST API Endpoints
POST /orders: Create a new order.GET /orders: Retrieve a list of orders.
- WebSocket Endpoint
/ws/orders: Receive real-time order status updates.
- Testing
- Tests are located under the
tests/folder using pytest.
- Tests are located under the
- Containerization
- Dockerized application for consistent deployment.
- DevOps
- CI/CD with GitHub Actions in progress.
- Install dependencies:
pip install -r requirements.txt
- Ensure that you have PostgreSQL installed and running.
- Run the server:
uvicorn app.main:app --reload
- Build the Docker image:
docker build --platform=linux/amd64 -t blockhouse-app . - Run the application container (with .env file):
docker run --env-file .env -p 80:80 blockhouse-app
- Optionally, run the PostgreSQL container:
docker run --name blockhouse-db -e POSTGRES_PASSWORD=${} -d postgres
Tests are located under the tests/ folder. Run the tests with the following command:
pytest tests-
URL:
/orders -
Method:
POST -
Description: Create a new order. When an order is created, a background task notifies all connected WebSocket clients about the new order.
-
Headers:
Content-Type: application/json -
Request Body Example:
{ "symbol": "dress", "price": 500.0, "quantity": 5, "orderType": "online" } -
Success Response:
- Code: 201 Created
- Content: JSON representation of the created order. For example:
{ "id": 1, "symbol": "dress", "price": 500.0, "quantity": 5, "orderType": "online", }
-
URL:
/orders -
Method:
GET -
Description: Retrieve a list of orders.
-
Query Parameters:
offset(optional, default: 0): The starting point (index) from which orders are returned.orderType(optional, default: 100, maximum: 100): The number of orders to return.
-
Success Response:
- Code: 200 OK
- Content: A JSON list of order objects. For example:
[ { "id": 1, "symbol": "dress", "price": 500.0, "quantity": 5, "orderType": "online", }, { "id": 2, "symbol": "food", "price": 150.0, "quantity": 10, "orderType": "offline", } ]
- URL:
/ws/orders - Method:
WebSocket - Description: Enables clients to connect via WebSocket to receive real-time updates about order statuses. Notifications are sent when:
- A new order is created.
- A client sends an update message.
- A client disconnects.
-
Client Usage Example (JavaScript):
const ws = new WebSocket('ws://localhost:80/ws/orders'); ws.onopen = () => { console.log('WebSocket connection established.'); }; ws.onmessage = (event) => { const data = JSON.parse(event.data); console.log('Received message:', data); }; ws.onclose = () => { console.log('WebSocket connection closed.'); };
-
Behavior:
- When a client connects, it is added to the connection pool.
- When a client sends a message, it is broadcasted to all connected clients.
- On disconnect, a broadcast informs remaining clients that a client has disconnected.
The database schema consists of a single table, orders, with the following columns:
id: The primary key of the order.symbol: The name of the product.price: The price of the product.quantity: The quantity of the product.orderType: The type of order (online or offline).
- Background Tasks:
The
POST /ordersendpoint uses FastAPI's BackgroundTasks to handle broadcasting so that the client receives a quick response without waiting for the broadcast to complete. - Database:
The API uses SQLModel with PostgreSQL (SQLite for testing). Database connection settings are loaded from environment variables via a
.envfile.
- AWS ECR used to store built Docker images
- GitHub repository environment secrets used to store IAM role ARN (used for pushing to ECR) and SSH credentials
- Run "Docker build and deploy" step only after tests have succeeded on pushes to main
- Deployments to EC2 can be secured via either of the following to remove using SSH and exposing SSH port to the internet (GitHub Actions uses a wide range of dnyamic IP addresses, making it hard to configure a robust Security Group rule)
- a webhook (can use AWS System Manager or ubuntu's webhook), or
- via AWS CodeDeploy
- Deploy to multiple environments with a deployment pipeline
- Load balancer + auto scaling group can be configured in EC2 to improve service reliability and scalability