# CodeTutor Deployment

Production deployment configuration using Docker Compose and Cloudflare Tunnel.

## Architecture

```
Internet -> Cloudflare Tunnel -> nginx -> frontend (Next.js)
                                      \-> backend (FastAPI)
                                               \-> PostgreSQL
```

## Prerequisites

- Docker and Docker Compose
- Cloudflare account with Zero Trust access
- External Docker network: `public-network`

Create the external network if it doesn't exist:

```bash
docker network create public-network
```

## Setup

### 1. Configure Environment

```bash
cp .env.example .env
```

Edit `.env` with:

- `POSTGRES_PASSWORD`: Strong password for the database
- `CLOUDFLARE_TUNNEL_TOKEN`: Token from Cloudflare Zero Trust dashboard

### 2. Create Cloudflare Tunnel

1. Go to [Cloudflare Zero Trust Dashboard](https://one.dash.cloudflare.com/)
2. Navigate to Networks > Tunnels
3. Create a new tunnel named `codetutor-demo`
4. Add public hostname:
   - Subdomain: `codetutor-demo`
   - Domain: `kschappell.com`
   - Service: `http://nginx:80`
5. Copy the tunnel token to your `.env` file

### 3. Deploy

```bash
docker compose up -d --build
```

### 4. Verify

- Check service health: `docker compose ps`
- View logs: `docker compose logs -f`
- Test endpoint: `curl https://codetutor-demo.kschappell.com/health`

## Database Management

### Initial Setup

The database is automatically created on first run. To seed with initial data:

```bash
docker compose exec backend python -m alembic upgrade head
docker compose exec backend python -m scripts.seed_db
```

### Backups

```bash
docker compose exec db pg_dump -U codetutor codetutor > backup.sql
```

### Restore

```bash
docker compose exec -T db psql -U codetutor codetutor < backup.sql
```

## Updating

```bash
git pull
docker compose up -d --build
```

## Troubleshooting

### Services not starting

Check logs for specific service:

```bash
docker compose logs backend
docker compose logs frontend
docker compose logs nginx
```

### Database connection issues

Ensure the database is healthy:

```bash
docker compose ps db
docker compose logs db
```

### Tunnel not connecting

Verify the tunnel token is correct and the tunnel is active in the Cloudflare dashboard.
