Skip to content

Commit fb69f47

Browse files
committed
feat: Added Makefile for Mac/Linux users and update README
1 parent 4aca408 commit fb69f47

2 files changed

Lines changed: 256 additions & 6 deletions

File tree

Makefile

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
.PHONY: help build up down restart logs logs-gateway logs-processor test seed seed-item health metrics metrics-gateway metrics-processor inventory order-status clean rebuild ps test-order test-idempotency
2+
3+
# Default target
4+
help:
5+
@echo "Flash Sale Engine - Makefile Commands"
6+
@echo ""
7+
@echo "Available commands:"
8+
@echo " make build - Build Docker images"
9+
@echo " make up - Start all services"
10+
@echo " make down - Stop all services"
11+
@echo " make restart - Restart all services"
12+
@echo " make logs - View logs from all services"
13+
@echo " make logs-gateway - View gateway logs"
14+
@echo " make logs-processor - View processor logs"
15+
@echo " make test - Run comprehensive test suite"
16+
@echo " make seed - Seed inventory (100 items for item_id '101')"
17+
@echo " make seed-item - Seed inventory for specific item (usage: make seed-item ITEM=102 QTY=50)"
18+
@echo " make health - Check service health"
19+
@echo " make metrics - View Prometheus metrics"
20+
@echo " make metrics-gateway - View gateway metrics"
21+
@echo " make metrics-processor - View processor metrics"
22+
@echo " make inventory - Check inventory for item (usage: make inventory ITEM=101)"
23+
@echo " make order-status - Check order status (usage: make order-status REQ_ID=test-123)"
24+
@echo " make clean - Stop services and remove containers"
25+
@echo " make rebuild - Rebuild and restart services"
26+
@echo " make ps - Show running containers"
27+
@echo ""
28+
29+
# Build Docker images
30+
build:
31+
@echo "Building Docker images..."
32+
docker-compose build
33+
34+
# Start all services
35+
up:
36+
@echo "Starting services..."
37+
docker-compose up -d
38+
@echo "Waiting for services to be ready..."
39+
@sleep 5
40+
@echo "Services started. Use 'make health' to check status."
41+
42+
# Stop all services
43+
down:
44+
@echo "Stopping services..."
45+
docker-compose down
46+
47+
# Restart all services
48+
restart:
49+
@echo "Restarting services..."
50+
docker-compose restart
51+
52+
# View logs from all services
53+
logs:
54+
docker-compose logs -f
55+
56+
# View gateway logs
57+
logs-gateway:
58+
docker-compose logs -f gateway
59+
60+
# View processor logs
61+
logs-processor:
62+
docker-compose logs -f processor
63+
64+
# Run comprehensive test suite (requires PowerShell on Windows, or bash script on Linux)
65+
test:
66+
@echo "Running test suite..."
67+
@if command -v pwsh >/dev/null 2>&1; then \
68+
pwsh -File test-all-features.ps1; \
69+
elif [ -f test-all-features.sh ]; then \
70+
./test-all-features.sh; \
71+
else \
72+
echo "Error: test-all-features.ps1 requires PowerShell or test-all-features.sh"; \
73+
echo "For manual testing, use: make seed && curl -X POST http://localhost:8080/buy ..."; \
74+
fi
75+
76+
# Seed inventory (default: 100 items for item_id '101')
77+
seed:
78+
@echo "Seeding inventory: 100 items for item_id '101'..."
79+
@docker exec flash-sale-engine-redis-1 redis-cli SET inventory:101 100 || \
80+
docker exec $$(docker ps -q -f name=redis) redis-cli SET inventory:101 100
81+
@echo "Inventory seeded successfully"
82+
83+
# Seed inventory for specific item
84+
seed-item:
85+
@if [ -z "$(ITEM)" ] || [ -z "$(QTY)" ]; then \
86+
echo "Usage: make seed-item ITEM=102 QTY=50"; \
87+
exit 1; \
88+
fi
89+
@echo "Seeding inventory: $(QTY) items for item_id '$(ITEM)'..."
90+
@docker exec flash-sale-engine-redis-1 redis-cli SET inventory:$(ITEM) $(QTY) || \
91+
docker exec $$(docker ps -q -f name=redis) redis-cli SET inventory:$(ITEM) $(QTY)
92+
@echo "Inventory seeded successfully"
93+
94+
# Check service health
95+
health:
96+
@echo "Checking service health..."
97+
@curl -s http://localhost:8080/health | python3 -m json.tool 2>/dev/null || \
98+
curl -s http://localhost:8080/health | python -m json.tool 2>/dev/null || \
99+
curl -s http://localhost:8080/health || \
100+
echo "Error: Could not check health. Is the gateway running?"
101+
102+
# View Prometheus metrics (both services)
103+
metrics:
104+
@echo "=== Gateway Metrics (port 8080) ==="
105+
@curl -s http://localhost:8080/metrics | head -20
106+
@echo ""
107+
@echo "=== Processor Metrics (port 9090) ==="
108+
@curl -s http://localhost:9090/metrics | head -20
109+
110+
# View gateway metrics
111+
metrics-gateway:
112+
@echo "Gateway Metrics:"
113+
@curl -s http://localhost:8080/metrics | grep -E "^gateway_" | head -20
114+
115+
# View processor metrics
116+
metrics-processor:
117+
@echo "Processor Metrics:"
118+
@curl -s http://localhost:9090/metrics | grep -E "^processor_" | head -20
119+
120+
# Check inventory for specific item
121+
inventory:
122+
@if [ -z "$(ITEM)" ]; then \
123+
echo "Usage: make inventory ITEM=101"; \
124+
exit 1; \
125+
fi
126+
@echo "Checking inventory for item_id '$(ITEM)'..."
127+
@docker exec flash-sale-engine-redis-1 redis-cli GET inventory:$(ITEM) || \
128+
docker exec $$(docker ps -q -f name=redis) redis-cli GET inventory:$(ITEM)
129+
130+
# Check order status
131+
order-status:
132+
@if [ -z "$(REQ_ID)" ]; then \
133+
echo "Usage: make order-status REQ_ID=test-123"; \
134+
exit 1; \
135+
fi
136+
@echo "Checking order status for request_id '$(REQ_ID)'..."
137+
@docker exec flash-sale-engine-redis-1 redis-cli GET "order_status:$(REQ_ID)" || \
138+
docker exec $$(docker ps -q -f name=redis) redis-cli GET "order_status:$(REQ_ID)"
139+
140+
# Clean up: stop services and remove containers
141+
clean:
142+
@echo "Cleaning up..."
143+
docker-compose down -v
144+
@echo "Cleanup complete"
145+
146+
# Rebuild and restart services
147+
rebuild:
148+
@echo "Rebuilding and restarting services..."
149+
docker-compose up -d --build
150+
@echo "Waiting for services to be ready..."
151+
@sleep 5
152+
@echo "Services rebuilt and restarted. Use 'make health' to check status."
153+
154+
# Show running containers
155+
ps:
156+
@docker-compose ps
157+
158+
# Quick test: send a test order
159+
test-order:
160+
@echo "Sending test order..."
161+
@curl -X POST http://localhost:8080/buy \
162+
-H "Content-Type: application/json" \
163+
-d '{"user_id":"test-user","item_id":"101","amount":1,"request_id":"make-test-'"$$(date +%s)"'"}' \
164+
-w "\nHTTP Status: %{http_code}\n" \
165+
-s || echo "Error: Could not send order. Is the gateway running?"
166+
167+
# Test idempotency: send same request twice
168+
test-idempotency:
169+
@echo "Testing idempotency..."
170+
@REQ_ID="idempotency-test-$$(date +%s)"; \
171+
echo "First request (should succeed):"; \
172+
curl -X POST http://localhost:8080/buy \
173+
-H "Content-Type: application/json" \
174+
-d "{\"user_id\":\"test-user\",\"item_id\":\"101\",\"amount\":1,\"request_id\":\"$$REQ_ID\"}" \
175+
-w "\nHTTP Status: %{http_code}\n" \
176+
-s; \
177+
echo ""; \
178+
echo "Second request (should return 409):"; \
179+
curl -X POST http://localhost:8080/buy \
180+
-H "Content-Type: application/json" \
181+
-d "{\"user_id\":\"test-user\",\"item_id\":\"101\",\"amount\":1,\"request_id\":\"$$REQ_ID\"}" \
182+
-w "\nHTTP Status: %{http_code}\n" \
183+
-s || echo "Error: Could not test idempotency. Is the gateway running?"
184+

README.md

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,45 @@ A **production-ready** high-concurrency distributed system for handling flash sa
5858
### Prerequisites
5959

6060
- Docker and Docker Compose
61-
- Go 1.22+ (optional, for local development)
61+
- Go 1.23+ (optional, for local development)
62+
- Make (optional, for Mac/Linux users - see Makefile commands)
6263

6364
### 1. Clone and Start
6465

66+
**Option A: Using Make (Mac/Linux)**
67+
```bash
68+
git clone <your-repo-url>
69+
cd flash-sale-engine
70+
make build # Build Docker images
71+
make up # Start all services
72+
make seed # Seed inventory (100 items for item_id '101')
73+
```
74+
75+
**Option B: Using Docker Compose**
6576
```bash
6677
git clone <your-repo-url>
6778
cd flash-sale-engine
6879
docker-compose up -d --build
80+
docker exec flash-sale-engine-redis-1 redis-cli SET inventory:101 100
81+
```
82+
83+
**Option C: Using PowerShell (Windows)**
84+
```powershell
85+
git clone <your-repo-url>
86+
cd flash-sale-engine
87+
docker-compose up -d --build
88+
docker exec flash-sale-engine-redis-1 redis-cli SET inventory:101 100
6989
```
7090

7191
### 2. Seed Inventory
7292

93+
**Using Make:**
94+
```bash
95+
make seed # Default: 100 items for item_id '101'
96+
make seed-item ITEM=102 QTY=50 # Custom item and quantity
97+
```
98+
99+
**Using Docker:**
73100
```bash
74101
docker exec flash-sale-engine-redis-1 redis-cli SET inventory:101 100
75102
```
@@ -595,23 +622,62 @@ flash-sale-engine/
595622
│ └── apps.yaml # Gateway, Processor
596623
├── Dockerfile # Multi-stage build for both services
597624
├── docker-compose.yml # Local development setup
625+
├── Makefile # Make commands for Mac/Linux users
598626
├── go.mod # Go module dependencies
599-
├── test-all-features.ps1 # Comprehensive test suite
627+
├── test-all-features.ps1 # Comprehensive test suite (PowerShell)
600628
├── README.md # This file
601629
├── TESTING.md # Detailed testing guide
602630
└── OPERATIONS.md # Operations runbook
603631
```
604632

605633
## 🔧 Development
606634

607-
**Build Locally:**
635+
### Using Makefile (Mac/Linux)
636+
637+
The project includes a `Makefile` with convenient commands:
638+
639+
```bash
640+
make help # Show all available commands
641+
make build # Build Docker images
642+
make up # Start all services
643+
make down # Stop all services
644+
make logs # View logs from all services
645+
make logs-gateway # View gateway logs
646+
make logs-processor # View processor logs
647+
make test # Run comprehensive test suite
648+
make seed # Seed inventory (100 items for item_id '101')
649+
make health # Check service health
650+
make metrics # View Prometheus metrics
651+
make inventory ITEM=101 # Check inventory for specific item
652+
make order-status REQ_ID=test-123 # Check order status
653+
make clean # Stop services and remove containers
654+
make rebuild # Rebuild and restart services
655+
make test-order # Send a test order
656+
make test-idempotency # Test idempotency (send same request twice)
657+
```
658+
659+
### Build Locally
660+
661+
**Using Make:**
662+
```bash
663+
make build
664+
```
665+
666+
**Manual Build:**
608667
```bash
609668
go mod download
610-
go build -o gateway-bin ./gateway/main.go
611-
go build -o processor-bin ./processor/main.go
669+
go build -o gateway-bin ./gateway
670+
go build -o processor-bin ./processor
671+
```
672+
673+
### Run Locally (requires Redis and Kafka)
674+
675+
**Using Docker Compose:**
676+
```bash
677+
make up # or: docker-compose up -d
612678
```
613679

614-
**Run Locally (requires Redis and Kafka):**
680+
**Manual Run:**
615681
```bash
616682
# Terminal 1: Gateway
617683
REDIS_ADDR=localhost:6379 KAFKA_ADDR=localhost:9092 ./gateway-bin

0 commit comments

Comments
 (0)