Skip to content
Open

Go #3

Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ba55aa0
feat: add go versions of the micro services
slayerjain Jan 9, 2026
59095cd
fix: go services
officialasishkumar Jan 9, 2026
cc64e12
fix: docker-compose file
officialasishkumar Jan 9, 2026
8b1e84f
fix: docker-compose file
officialasishkumar Jan 9, 2026
eddf4f1
fix: keploy yaml
officialasishkumar Jan 9, 2026
f33b76e
fix: add script files
officialasishkumar Jan 9, 2026
25f1dab
fix: add freezetime build in order service
officialasishkumar Jan 9, 2026
f34f8c2
Fix: update dockerfile for order service for dedup
officialasishkumar Jan 9, 2026
391eba5
fix: add global and test set level noise
officialasishkumar Jan 9, 2026
e89041d
fix: add noise endpoints
officialasishkumar Jan 9, 2026
d914549
fix: k8s setup
officialasishkumar Jan 10, 2026
4ae340e
fix: udpate docker compose for coverage
officialasishkumar Jan 10, 2026
42f493a
fix: tiem freeze setup
officialasishkumar Jan 10, 2026
dabe281
feat: add guide.md
officialasishkumar Jan 12, 2026
aac791b
feat: add guide.md
officialasishkumar Jan 12, 2026
1701fc1
feat: add guide.md
officialasishkumar Jan 12, 2026
5bdf417
feat: add guide.md
officialasishkumar Jan 12, 2026
9a63e27
feat: add guide.md
officialasishkumar Jan 12, 2026
d755dc2
feat: add keploy installation in guide
officialasishkumar Jan 12, 2026
9bc5f05
feat: add Kafka integration
Yogeshjindal Feb 9, 2026
6b8bc71
feat: Add Kafka safe wrappers and improve order service configuration
Yogeshjindal Feb 19, 2026
d7dd7a9
Add noise config to ignore dynamic order IDs in Keploy tests
Yogeshjindal Feb 26, 2026
80c8b69
push all
Yogeshjindal Feb 28, 2026
436c019
Remove keploy-enterprise binary and add to gitignore
Yogeshjindal Feb 28, 2026
87033ea
proper initialisation and skipping as per mode
Yogeshjindal Feb 28, 2026
07a9968
using mock matching
Yogeshjindal Feb 28, 2026
d54e1ac
feat: enhance database initialization and connection logic; add run s…
slayerjain Mar 10, 2026
09f25a5
Fix Go order service startup path
officialasishkumar Mar 14, 2026
174c473
Ignore dynamic order ids in Keploy config
officialasishkumar Mar 14, 2026
5968ccc
Merge pull request #13 from keploy/codex/fix-go-order-service-entrypoint
slayerjain Mar 14, 2026
33c5d2f
fix: replace apk ca-certificates install with COPY from builder
Sarthak160 Mar 26, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go-services/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script/
322 changes: 322 additions & 0 deletions go-services/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
# Go E-commerce Microservices

A microservices-based e-commerce application built with Go, featuring Kafka for event-driven architecture.

## Architecture

| Service | Port | Description |
|---------|------|-------------|
| User Service | 8082 | User authentication and management |
| Product Service | 8081 | Product catalog management |
| Order Service | 8080 | Order processing with Kafka events |
| API Gateway | 8083 | Unified API entry point |
| Kafka | 29092 | Message broker for events |
| Zookeeper | 2181 | Kafka coordination |

## Prerequisites

- Docker and Docker Compose installed
- `curl` command available (for testing)

## Quick Start

### 1. Start All Services

```bash
cd go-services
docker compose up -d --build
```

Wait about 30 seconds for all services to be ready.

### 2. Verify Services Are Running

```bash
docker compose ps
```

All services should show "Up" status.

---

## Testing the Complete Flow (Copy-Paste Ready)

### Step 1: Login and Get Token

```bash
# Login as admin user
curl -s -X POST http://localhost:8082/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}'
```

**Expected Response:**
```json
{"email":"admin@example.com","id":"...","token":"eyJ...","username":"admin"}
```

Copy the `token` value for the next steps.

### Step 2: Set Token as Environment Variable

```bash
# Replace <YOUR_TOKEN> with the token from Step 1
export TOKEN="<YOUR_TOKEN>"
```

Or use this one-liner to automatically set the token:

```bash
export TOKEN=$(curl -s -X POST http://localhost:8082/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' | \
grep -o '"token":"[^"]*"' | cut -d'"' -f4)
echo "Token set: $TOKEN"
```

### Step 3: Create a Product

```bash
curl -s -X POST http://localhost:8081/api/v1/products \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name":"Laptop","description":"Gaming Laptop","price":999.99,"stock":50}'
```

**Expected Response:**
```json
{"id":"<product-id>"}
```

Save the product ID:
```bash
export PRODUCT_ID="<product-id>"
```

Or use this one-liner:
```bash
export PRODUCT_ID=$(curl -s -X POST http://localhost:8081/api/v1/products \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name":"Laptop","description":"Gaming Laptop","price":999.99,"stock":50}' | \
grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "Product ID: $PRODUCT_ID"
```

### Step 4: Get User ID

The user ID is returned in the login response. You can extract it from Step 1, or use this one-liner:

```bash
export USER_ID=$(curl -s -X POST http://localhost:8082/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' | \
grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
echo "User ID: $USER_ID"
```

Or if you saved the login response, copy the `id` field:
```bash
export USER_ID="<user-id-from-login-response>"
```

### Step 5: Create an Order (Triggers `order_created` Kafka Event)

```bash
curl -s -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"userId\":\"$USER_ID\",\"items\":[{\"productId\":\"$PRODUCT_ID\",\"quantity\":2}]}"
```

**Expected Response:**
```json
{"id":"<order-id>","status":"PENDING"}
```

Save the order ID:
```bash
export ORDER_ID="<order-id>"
```

Or use this one-liner:
```bash
export ORDER_ID=$(curl -s -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"userId\":\"$USER_ID\",\"items\":[{\"productId\":\"$PRODUCT_ID\",\"quantity\":2}]}" | \
grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "Order ID: $ORDER_ID"
```

### Step 6: Pay the Order (Triggers `order_paid` Kafka Event)

```bash
curl -s -X POST "http://localhost:8080/api/v1/orders/$ORDER_ID/pay" \
-H "Authorization: Bearer $TOKEN"
```

**Expected Response:**
```json
{"id":"<order-id>","status":"PAID"}
```

### Step 7: Create and Cancel Another Order (Triggers `order_cancelled` Event)

```bash
# Create a new order
NEW_ORDER=$(curl -s -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"userId\":\"$USER_ID\",\"items\":[{\"productId\":\"$PRODUCT_ID\",\"quantity\":1}]}" | \
grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "New Order ID: $NEW_ORDER"

# Cancel it
curl -s -X POST "http://localhost:8080/api/v1/orders/$NEW_ORDER/cancel" \
-H "Authorization: Bearer $TOKEN"
```

**Expected Response:**
```json
{"id":"<order-id>","status":"CANCELLED"}
```

---

## Verify Kafka Events

### Check Order Service Logs

```bash
docker logs order_service 2>&1 | grep -E "KAFKA|order_"
```

**Expected Output:**
```
Kafka producer initialized for topic: order-events
Kafka consumer started for topic: order-events
Kafka: message sent to topic order-events with key: order_created
>>> KAFKA EVENT RECEIVED: [order_created] -> ...
Kafka: message sent to topic order-events with key: order_paid
>>> KAFKA EVENT RECEIVED: [order_paid] -> ...
Kafka: message sent to topic order-events with key: order_cancelled
>>> KAFKA EVENT RECEIVED: [order_cancelled] -> ...
```

### Read Messages Directly from Kafka Topic

```bash
docker exec kafka kafka-console-consumer \
--bootstrap-server localhost:9092 \
--topic order-events \
--from-beginning \
--max-messages 10
```

### List Kafka Topics

```bash
docker exec kafka kafka-topics --bootstrap-server localhost:9092 --list
```

---

## Complete One-Liner Test Script

Run this entire block to test everything at once:

```bash
# Login and set token + user ID
LOGIN_RESP=$(curl -s -X POST http://localhost:8082/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}')
export TOKEN=$(echo $LOGIN_RESP | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
export USER_ID=$(echo $LOGIN_RESP | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
echo "✓ Logged in (User ID: $USER_ID)"

# Create product
export PRODUCT_ID=$(curl -s -X POST http://localhost:8081/api/v1/products \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name":"Test Product","description":"Test","price":49.99,"stock":100}' | \
grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "✓ Created product: $PRODUCT_ID"

# Create order
export ORDER_ID=$(curl -s -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"userId\":\"$USER_ID\",\"items\":[{\"productId\":\"$PRODUCT_ID\",\"quantity\":2}]}" | \
grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "✓ Created order: $ORDER_ID"

# Pay order
curl -s -X POST "http://localhost:8080/api/v1/orders/$ORDER_ID/pay" \
-H "Authorization: Bearer $TOKEN" > /dev/null
echo "✓ Paid order: $ORDER_ID"

# Create and cancel another order
export ORDER_ID2=$(curl -s -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"userId\":\"$USER_ID\",\"items\":[{\"productId\":\"$PRODUCT_ID\",\"quantity\":1}]}" | \
grep -o '"id":"[^"]*"' | cut -d'"' -f4)
curl -s -X POST "http://localhost:8080/api/v1/orders/$ORDER_ID2/cancel" \
-H "Authorization: Bearer $TOKEN" > /dev/null
echo "✓ Created and cancelled order: $ORDER_ID2"

echo ""
echo "=== Kafka Events ==="
docker logs order_service 2>&1 | grep ">>> KAFKA EVENT" | tail -5
```

---

## Kafka Event System

### Events Published

| Event Type | Trigger | Payload |
|------------|---------|---------|
| `order_created` | New order placed | `orderId`, `userId`, `totalAmount`, `items` |
| `order_paid` | Order payment confirmed | `orderId`, `userId`, `totalAmount` |
| `order_cancelled` | Order cancelled | `orderId` |

### Configuration

| Environment Variable | Default | Description |
|---------------------|---------|-------------|
| `KAFKA_BROKERS` | `kafka:9092` | Kafka broker addresses |
| `KAFKA_TOPIC` | `order-events` | Topic for order events |
| `KAFKA_GROUP_ID` | `order-service-group` | Consumer group ID |

---

## Stop Services

```bash
docker compose down -v
```

---

## Keploy Recording

To run Keploy record mode for order service:

```bash
keploy record -c "docker compose up" --container-name="order_service" --build-delay 40 --path="./order_service" --config-path="./order_service"
```

Wait for services to start, then run the test script:

```bash
python3 -m venv venv
source venv/bin/activate
pip install requests
python3 test_api_script.py
```

This will record all test cases in `order_service/keploy` folder.

26 changes: 26 additions & 0 deletions go-services/apigateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM golang:1.21-alpine AS builder

WORKDIR /app

# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download

# Copy source
COPY . .

# Build
RUN GOOS=linux go build -o /apigateway ./apigateway

# Runtime
FROM alpine:3.19

RUN apk --no-cache add ca-certificates

WORKDIR /app
COPY --from=builder /apigateway .

EXPOSE 8083

CMD ["./apigateway"]

Loading
Loading