Skip to content
Draft
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
191 changes: 191 additions & 0 deletions infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# PeerPrep Infrastructure

Shared infrastructure for all PeerPrep microservices.

## Services

- **PostgreSQL**: `localhost:5432` (user: `peerprep`, password: `peerprep`)
- **Redis**: `localhost:6379`
- **pgAdmin**: http://localhost:5050 (admin@peerprep.dev / admin)
- **Redis Commander**: http://localhost:8081

## Quick Start

```powershell
# One command setup - starts infrastructure, creates databases,
# generates .env files, and seeds databases for services that have them
cd infrastructure
py scripts/setup-infrastructure.py

# That's it! All services are ready to run
```

### Manual Steps (if needed)

```powershell
# Generate service .env files only
py scripts/generate-env-files.py

# Seed individual service database (if setup didn't work and service has a database)
cd ../services/{service-name}
py scripts/seed_data.py
```

## What the Setup Script Does

The automated setup (`py scripts/setup-infrastructure.py`) performs:

1. **Infrastructure Startup**: Starts PostgreSQL, Redis, pgAdmin, Redis Commander
2. **Database Creation**: Creates all databases defined in `init-all-databases.sql`
3. **Environment Configuration**: Generates `.env.dev` files for all services in `SERVICES`
4. **Database Seeding**: Automatically runs `scripts/seed_data.py` for each service that has a database and seed script
5. **Health Checks**: Verifies all services are running properly
6. **Service Information**: Shows connection details and next steps

**Current Services Configured:**
- **question-service**: Fully configured with database and seeding

**To Add More Services:**
1. Add service configuration to `generate-env-files.py`
2. For services with databases: Uncomment relevant database creation in `init-all-databases.sql`
3. For services with databases: Create `services/{service}/scripts/seed_data.py`
4. Run setup script - it will automatically handle the new service

## Database Setup

### Current Services
- **question-service**: `peerprep_questions_dev` / `peerprep_questions_test`

### Adding More Services

To add more microservices with automatic setup, edit `infrastructure/scripts/generate-env-files.py`:

```python
SERVICES = {
"question-service": {
"port": 8003,
"db_name": "peerprep_questions"
},
# Examples of different service types:
#
# Service with database:
# "user-service": {
# "port": 8001,
# "db_name": "peerprep_users"
# },
#
# Service without database (Redis-only or stateless):
# "notification-service": {
# "port": 8005
# # No db_name = no database configuration
# }
}
```

### Auto-Seeding Requirements (Optional)

For services with databases that need sample data, create a `scripts/seed_data.py` file:

```python
# services/{service-name}/scripts/seed_data.py
import sys
import os

# Add the parent directory to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.core.database import SessionLocal, engine, Base
from app.models.your_model import YourModel

def seed_database():
Base.metadata.create_all(bind=engine)
db = SessionLocal()
try:
# Check if data already exists
existing_count = db.query(YourModel).count()
if existing_count > 0:
print(f"Database already contains {existing_count} records. Skipping seed.")
return

# Add your seed data here
# ... seeding logic ...

db.commit()
print(f"Successfully seeded database with sample data.")
except Exception as e:
print(f"Error seeding database: {e}")
db.rollback()
finally:
db.close()

if __name__ == "__main__":
seed_database()
```

### Database Creation Steps (For Services That Need Databases)

1. **Update database initialization**:
```sql
-- Add to infrastructure/development/scripts/init-all-databases.sql
CREATE DATABASE peerprep_newservice_dev;
CREATE DATABASE peerprep_newservice_dev_test;
GRANT ALL PRIVILEGES ON DATABASE peerprep_newservice_dev TO peerprep;
GRANT ALL PRIVILEGES ON DATABASE peerprep_newservice_dev_test TO peerprep;
```

2. **Add service configuration with `db_name`** (see above)

3. **Create seed script** (optional, see above)

## Running Services

All services are started the same way regardless of their dependencies:

```powershell
cd services/{service-name}
py run.py
```

**Service Types:**
- **With Database**: Services like `question-service` that store data in PostgreSQL
- **Redis Only**: Services that only use Redis for caching/sessions
- **Stateless**: Services that don't need persistent storage

## Management

```powershell
# Start infrastructure
cd infrastructure/development
docker-compose up -d

# Stop (preserve data)
docker-compose down

# Reset (delete all data)
docker-compose down -v
docker-compose up -d

# View logs
docker-compose logs postgres
docker-compose logs redis
```

## Troubleshooting

```powershell
# Check infrastructure status
cd infrastructure
py scripts/setup-infrastructure.py

# Manually create databases if missing
cd infrastructure/development
docker-compose exec postgres psql -U peerprep -d postgres -f /docker-entrypoint-initdb.d/init-all-databases.sql

# Test service configuration
cd services/{service-name}
py -c "from app.core.config import settings; print(settings.get_database_url())"

# Manual database seeding (for services with databases)
cd services/{service-name}
py scripts/seed_data.py
```
72 changes: 72 additions & 0 deletions infrastructure/development/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# PeerPrep Development Environment Configuration Template
# Copy this to each microservice and customize service-specific values

# =============================================================================
# SHARED INFRASTRUCTURE SETTINGS
# =============================================================================

# Environment
ENV=dev

# Shared Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=peerprep
DB_PASSWORD=peerprep

# Shared Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

# =============================================================================
# MICROSERVICE-SPECIFIC SETTINGS (customize per service)
# =============================================================================

# App Settings (CUSTOMIZE)
APP_NAME=peerprep-{SERVICE_NAME}-service
HOST=localhost
PORT={SERVICE_PORT} # 8001: user, 8002: matching, 8003: question, 8004: collaboration

# Database Names (ONLY FOR SERVICES THAT NEED DATABASES)
DB_NAME=peerprep_{SERVICE_NAME} # results in peerprep_{SERVICE_NAME}_dev

# Database URLs (ONLY FOR SERVICES THAT NEED DATABASES)
# These will be commented out automatically for stateless services
DATABASE_URL=postgresql://peerprep:peerprep@localhost:5432/peerprep_{SERVICE_NAME}_dev
TEST_DATABASE_URL=postgresql://peerprep:peerprep@localhost:5432/peerprep_{SERVICE_NAME}_dev_test

# =============================================================================
# SHARED AUTHENTICATION SETTINGS
# =============================================================================

# JWT Configuration (shared across all services)
JWT_SECRET=dev-secret-key-change-in-production-123
JWT_ALG=HS256

# =============================================================================
# SHARED CORS SETTINGS
# =============================================================================

# Frontend URLs
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001

# =============================================================================
# OTHER SHARED SERVICES
# =============================================================================

# AWS
AWS_REGION=us-east-1
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=

# Logging
LOG_LEVEL=INFO
LOG_FORMAT=json

# =============================================================================
# SERVICE-SPECIFIC OVERRIDES
# =============================================================================

# Add service-specific environment variables below:
# ...
77 changes: 77 additions & 0 deletions infrastructure/development/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# PeerPrep Development Infrastructure

services:
postgres:
image: postgres:13
container_name: peerprep-postgres-dev
environment:
POSTGRES_USER: peerprep
POSTGRES_PASSWORD: peerprep
POSTGRES_DB: postgres # Default database
ports:
- "5432:5432"
volumes:
- postgres_dev_data:/var/lib/postgresql/data
- ./scripts/init-all-databases.sql:/docker-entrypoint-initdb.d/init-all-databases.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U peerprep"]
interval: 5s
timeout: 5s
retries: 5
networks:
- peerprep-dev

# Redis - Shared caching and session store
redis:
image: redis:7-alpine
container_name: peerprep-redis-dev
ports:
- "6379:6379"
volumes:
- redis_dev_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
networks:
- peerprep-dev

# pgAdmin - Database administration interface
pgadmin:
image: dpage/pgadmin4
container_name: peerprep-pgadmin-dev
environment:
PGADMIN_DEFAULT_EMAIL: admin@peerprep.dev
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5050:80"
volumes:
- pgadmin_dev_data:/var/lib/pgadmin
depends_on:
- postgres
networks:
- peerprep-dev

# Redis Commander - Redis administration interface
redis-commander:
image: rediscommander/redis-commander:latest
container_name: peerprep-redis-commander-dev
environment:
REDIS_HOSTS: local:redis:6379
ports:
- "8081:8081"
depends_on:
- redis
networks:
- peerprep-dev

volumes:
postgres_dev_data:
redis_dev_data:
pgadmin_dev_data:

networks:
peerprep-dev:
name: peerprep-dev
driver: bridge
27 changes: 27 additions & 0 deletions infrastructure/development/scripts/init-all-databases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- PeerPrep Database Initialization Script
-- Creates all databases needed for the microservices architecture

-- Question Service Databases
CREATE DATABASE peerprep_questions_dev;
CREATE DATABASE peerprep_questions_dev_test;
GRANT ALL PRIVILEGES ON DATABASE peerprep_questions_dev TO peerprep;
GRANT ALL PRIVILEGES ON DATABASE peerprep_questions_dev_test TO peerprep;

-- Uncomment when services are added
-- User Service Databases
-- CREATE DATABASE peerprep_users_dev;
-- CREATE DATABASE peerprep_users_dev_test;
-- GRANT ALL PRIVILEGES ON DATABASE peerprep_users_dev TO peerprep;
-- GRANT ALL PRIVILEGES ON DATABASE peerprep_users_dev_test TO peerprep;

-- Matching Service Databases
-- CREATE DATABASE peerprep_matching_dev;
-- CREATE DATABASE peerprep_matching_dev_test;
-- GRANT ALL PRIVILEGES ON DATABASE peerprep_matching_dev TO peerprep;
-- GRANT ALL PRIVILEGES ON DATABASE peerprep_matching_dev_test TO peerprep;

-- Collaboration Service Databases
-- CREATE DATABASE peerprep_collaboration_dev;
-- CREATE DATABASE peerprep_collaboration_dev_test;
-- GRANT ALL PRIVILEGES ON DATABASE peerprep_collaboration_dev TO peerprep;
-- GRANT ALL PRIVILEGES ON DATABASE peerprep_collaboration_dev_test TO peerprep;
Loading