Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Student Service CI/CD
name: User Service CI/CD

on:
push:
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ RUN go install github.com/swaggo/swag/cmd/swag@latest && \
swag init

# build static binary
RUN go build -ldflags="-s -w" -o /app/student-service .
RUN go build -ldflags="-s -w" -o /app/user-service .

FROM alpine:3.18
RUN apk add --no-cache ca-certificates

WORKDIR /app
COPY --from=builder /app/student-service /app/student-service
COPY --from=builder /app/user-service /app/user-service
# Include API docs and optional .env from the build context so the runtime image can serve /docs
COPY --from=builder /src/docs /app/docs

Expand All @@ -34,4 +34,4 @@ USER 1000:1000
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:${SERVER_PORT}/health || exit 1

CMD ["/app/student-service"]
CMD ["/app/user-service"]
6 changes: 3 additions & 3 deletions INTEGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

## Overview

The Student Service demonstrates microservice integration by calling the Enrollment Service to fetch student enrollment data. This demonstrates how microservices communicate and share data across service boundaries in a distributed architecture.
The User Service demonstrates microservice integration by calling the Enrollment Service to fetch student enrollment data. This demonstrates how microservices communicate and share data across service boundaries in a distributed architecture.

## Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│ API Gateway (Port 8080) │
│ Routes requests to Student Service │
│ Routes requests to User Service
└────────────────────────────┬────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
Student Service (Port 5001) │
User Service (Port 5001)
├─────────────────────────────────────────────────────────────────┤
│ Storage: MongoDB (student data) │
│ Endpoints: │
Expand Down
6 changes: 3 additions & 3 deletions MONGODB_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ brew services start mongodb-community
Create a `.env` file in the project root:
```env
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=student_service
MONGODB_DB=userdb
SERVER_PORT=8080
SERVER_ENV=development
```

**For MongoDB Atlas (Cloud):**
```env
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority
MONGODB_DB=student_service
MONGODB_DB=userdb
```

### 3. Run the Server
Expand Down Expand Up @@ -99,7 +99,7 @@ Each student document in the `students` collection contains:
### Using MongoDB Shell
```bash
mongosh
use student_service
use user-service
db.students.find()
```

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: help install run swagger clean

help:
@echo "Student Service API - Available Commands"
@echo "User Service API - Available Commands"
@echo ""
@echo " make install - Download Go dependencies"
@echo " make run - Run the server"
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Student Service 🎓
# User Service 🎓

A production-ready Go microservice for student management with comprehensive security features, JWT authentication, and comprehensive testing.

Expand Down Expand Up @@ -42,7 +42,7 @@ A production-ready Go microservice for student management with comprehensive sec
┌─────────────────────────────────────────────────┐
Student Service (Port 5001) │
User Service (Port 5001)
├─────────────────────────────────────────────────┤
│ Routes: │
│ POST /auth/register → registerH │
Expand All @@ -61,7 +61,7 @@ A production-ready Go microservice for student management with comprehensive sec
└────────────────┘ └────────────────────┘
```

**Microservice Integration:** The Student Service calls the Enrollment Service via HTTP to fetch enrollments for the `/students/{id}/enrollments` endpoint.
**Microservice Integration:** The User Service calls the Enrollment Service via HTTP to fetch enrollments for the `/students/{id}/enrollments` endpoint.


## 📋 Requirements
Expand Down Expand Up @@ -290,7 +290,7 @@ Response: 200 OK

## 🔗 Microservice Integration

The Student Service integrates with the **Enrollment Service** to provide comprehensive student information including course enrollments.
The User Service integrates with the **Enrollment Service** to provide comprehensive student information including course enrollments.

**Key Features:**
- ✅ Synchronous HTTP/REST calls to Enrollment Service
Expand Down Expand Up @@ -443,9 +443,9 @@ Client
API Gateway (Port 8080)
├─ Validates JWT
├─ Adds X-User-ID header
├─ Routes to Student Service
├─ Routes to User Service
Student Service (Port 5001)
User Service (Port 5001)
├─ Receives X-User-ID
├─ Processes request
├─ Returns response
Expand All @@ -455,7 +455,7 @@ API Gateway (returns to client)

**Environment:**
```bash
STUDENT_SERVICE_URL=http://localhost:5001
USER_SERVICE_URL=http://localhost:5001
```


Expand Down
2 changes: 1 addition & 1 deletion README_API.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Student Service API
# User Service API

A simple student management API built with Go.

Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"golang.org/x/time/rate"
)

// @title Student Service API
// @title User Service API
// @version 1.0
// @description A simple student management API with MongoDB
// @BasePath /
Expand Down Expand Up @@ -973,8 +973,8 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
}

// loginHandler godoc
// @Summary Login a student
// @Description Authenticate a student with email and password, return JWT token
// @Summary Login
// @Description Authenticate a USER with email and password, return JWT token
// @Tags Auth
// @Accept json
// @Produce json
Expand Down
6 changes: 3 additions & 3 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rate Limiter Test Suite

This folder contains comprehensive tests for the rate limiting functionality of the Student Service.
This folder contains comprehensive tests for the rate limiting functionality of the User Service.

## Test Files

Expand Down Expand Up @@ -163,7 +163,7 @@ chmod +x test/test_rate_limiter.sh

## Rate Limiter Configuration

Current configuration in the Student Service:
Current configuration in the User Service:

- **Rate:** 5 requests per minute per IP
- **Burst:** 1 (allows 1 immediate request)
Expand All @@ -179,7 +179,7 @@ This means:
## Troubleshooting

### Problem: "Connection refused"
**Solution:** Make sure the Student Service is running
**Solution:** Make sure the User Service is running
```bash
cd /workspaces/user-service
go run main.go
Expand Down
4 changes: 2 additions & 2 deletions test_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ echo "----------------------------------------------"

# Check if service is running
if ! curl -s -f "$BASE_URL/" > /dev/null 2>&1; then
echo -e "${RED}❌ Student Service is not running on $BASE_URL${NC}"
echo -e "${RED}❌ User Service is not running on $BASE_URL${NC}"
echo "Start with: go run main.go"
exit 1
fi

echo -e "${GREEN}✅ Student Service is running${NC}"
echo -e "${GREEN}✅ User Service is running${NC}"
echo ""

# Test data
Expand Down
30 changes: 15 additions & 15 deletions test_enrollment_connection.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
# test_enrollment_connection.sh - Test enrollment service connection from student service
# test_enrollment_connection.sh - Test enrollment service connection from user service

echo "=============================================="
echo "Enrollment Service Connection Test"
echo "Student Service ↔ Enrollment Service (Cloud)"
echo "User Service ↔ Enrollment Service (Cloud)"
echo "=============================================="
echo ""

Expand All @@ -13,7 +13,7 @@ if [ -f .env ]; then
fi

# Configuration
STUDENT_SERVICE="http://localhost:${SERVER_PORT:-5001}"
USER_SERVICE="http://localhost:${SERVER_PORT:-5001}"
ENROLLMENT_SERVICE_URL="${ENROLLMENT_SERVICE_URL}"

# Check if ENROLLMENT_SERVICE_URL is set
Expand Down Expand Up @@ -52,14 +52,14 @@ info() {
echo "📋 Step 1: Check Service Connectivity"
echo "----------------------------------------------"

# Check Student Service
echo -n "Checking Student Service (localhost:5001)... "
if curl -s -f "$STUDENT_SERVICE/" > /dev/null 2>&1; then
# Check User Service
echo -n "Checking User Service (localhost:5001)... "
if curl -s -f "$USER_SERVICE/" > /dev/null 2>&1; then
echo -e "${GREEN}✅${NC}"
pass "Student Service is running"
pass "User Service is running"
else
echo -e "${RED}❌${NC}"
fail "Student Service is not running"
fail "User Service is not running"
exit 1
fi

Expand All @@ -85,7 +85,7 @@ TEST_PHONE="555-$(printf '%04d' $((RANDOM % 10000)))"

info "Registering student: $TEST_EMAIL"

REGISTER_RESPONSE=$(curl -s -X POST "$STUDENT_SERVICE/auth/register" \
REGISTER_RESPONSE=$(curl -s -X POST "$USER_SERVICE/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$TEST_EMAIL\",
Expand All @@ -111,7 +111,7 @@ echo "----------------------------------------------"

info "Logging in with email: $TEST_EMAIL"

LOGIN_RESPONSE=$(curl -s -X POST "$STUDENT_SERVICE/auth/login" \
LOGIN_RESPONSE=$(curl -s -X POST "$USER_SERVICE/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$TEST_EMAIL\",
Expand All @@ -138,7 +138,7 @@ echo ""

ENROLLMENT_RESPONSE=$(curl -s -w "\n%{http_code}" \
-H "Authorization: Bearer $TOKEN" \
"$STUDENT_SERVICE/students/$STUDENT_ID/enrollments")
"$USER_SERVICE/students/$STUDENT_ID/enrollments")

HTTP_CODE=$(echo "$ENROLLMENT_RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$ENROLLMENT_RESPONSE" | sed '$d')
Expand Down Expand Up @@ -206,8 +206,8 @@ echo ""

echo "🔍 Step 6: Connection Details"
echo "----------------------------------------------"
info "Student Service Configuration:"
echo " - Base URL: $STUDENT_SERVICE"
info "User Service Configuration:"
echo " - Base URL: $USER_SERVICE"
echo " - Port: 5001"
echo " - Status: Running"
echo ""
Expand Down Expand Up @@ -237,11 +237,11 @@ if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}✅ All tests passed!${NC}"
echo ""
echo "🎯 Results:"
echo " - Student Service ✅ Working"
echo " - User Service ✅ Working"
echo " - Enrollment Service ✅ Reachable"
echo " - Microservice Integration ✅ Successful"
echo ""
echo "The Student Service successfully connects to the Enrollment Service"
echo "The User Service successfully connects to the Enrollment Service"
echo "at $ENROLLMENT_SERVICE_URL"
exit 0
else
Expand Down
22 changes: 11 additions & 11 deletions test_integration.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash
# test_integration.sh - Test script for Student-Enrollment microservice integration
# This script demonstrates how the Student Service calls the Enrollment Service
# This script demonstrates how the User Service calls the Enrollment Service

set -e # Exit on errors

echo "=============================================="
echo "Microservice Integration Test"
echo "Student Service ↔ Enrollment Service"
echo "User Service ↔ Enrollment Service"
echo "=============================================="
echo ""

Expand All @@ -16,16 +16,16 @@ if [ -f .env ]; then
fi

# Configuration (defaults for local development)
STUDENT_SERVICE="http://localhost:${SERVER_PORT:-5001}"
USER_SERVICE="http://localhost:${SERVER_PORT:-5001}"
ENROLLMENT_SERVICE="${ENROLLMENT_SERVICE_URL:-http://localhost:5003}"
API_GATEWAY="http://localhost:8080"

echo "📋 Prerequisites Check"
echo "----------------------------------------------"

# Check if Student Service is running
echo -n "Checking Student Service (port 5001)... "
if curl -s -f "$STUDENT_SERVICE/" > /dev/null 2>&1; then
# Check if User Service is running
echo -n "Checking User Service (port 5001)... "
if curl -s -f "$USER_SERVICE/" > /dev/null 2>&1; then
echo "✅ Running"
else
echo "❌ Not running"
Expand All @@ -46,7 +46,7 @@ echo ""
echo "🔐 Step 1: Register Test Student"
echo "----------------------------------------------"

REGISTER_RESPONSE=$(curl -s -X POST "$STUDENT_SERVICE/auth/register" \
REGISTER_RESPONSE=$(curl -s -X POST "$USER_SERVICE/auth/register" \
-H "Content-Type: application/json" \
-d '{
"email": "testuser_'$(date +%s)'@university.edu",
Expand All @@ -71,7 +71,7 @@ echo ""
echo "🔑 Step 2: Login to Get JWT Token"
echo "----------------------------------------------"

LOGIN_RESPONSE=$(curl -s -X POST "$STUDENT_SERVICE/auth/login" \
LOGIN_RESPONSE=$(curl -s -X POST "$USER_SERVICE/auth/login" \
-H "Content-Type: application/json" \
-d '{
"email": "'$(echo "$REGISTER_RESPONSE" | jq -r '.email')'",
Expand Down Expand Up @@ -137,7 +137,7 @@ echo "GET /students/$STUDENT_ID/enrollments"
echo ""

INTEGRATION_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" \
"$STUDENT_SERVICE/students/$STUDENT_ID/enrollments")
"$USER_SERVICE/students/$STUDENT_ID/enrollments")

echo "$INTEGRATION_RESPONSE" | jq '.'

Expand Down Expand Up @@ -180,13 +180,13 @@ echo "✅ Integration Test Complete!"
echo "=============================================="
echo ""
echo "📋 Summary:"
echo " - Student Service: Working"
echo " - User Service: Working"
echo " - Enrollment Service: $([ -z "$ENROLLMENT_DOWN" ] && echo "Working" || echo "Down (graceful degradation tested)")"
echo " - Microservice Integration: ✅ Successful"
echo " - Student ID: $STUDENT_ID"
echo " - Enrollments: $ENROLLMENT_COUNT"
echo ""
echo "🔗 Integration demonstrated:"
echo " Student Service → HTTP Call → Enrollment Service"
echo " User Service → HTTP Call → Enrollment Service"
echo " Response combines data from both services"
echo ""
Loading