# Deployment

Guide for operating the Collab Server in various environments.

## System Requirements

| Component | Minimum | Recommended |
|-----------|---------|-------------|
| Node.js   | 20.x    | 22.x LTS    |
| RAM       | 512 MB  | 2 GB        |
| Storage   | 1 GB    | 10 GB       |
| CPU       | 1 Core  | 2+ Cores    |

## Environment Variables

| Variable       | Default       | Description      |
|----------------|---------------|------------------|
| `PORT`         | `4000`        | Server port      |
| `NODE_ENV`     | `development` | Environment      |
| `CONTENT_PATH` | `./content`   | Path to content  |
| `LOG_LEVEL`    | `info`        | Log level        |

## Development

```bash
# With npm
npm run dev

# With custom content
CONTENT_PATH=../my-content npm run dev
```

## Production Build

```bash
# Compile TypeScript
npm run build

# Start
NODE_ENV=production npm start
```

## Docker

### Dockerfile

```dockerfile
FROM node:22-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY dist/ ./dist/
COPY content/ ./content/

EXPOSE 4000

ENV NODE_ENV=production
ENV PORT=4000

CMD ["node", "dist/server.js"]
```

### Docker Compose

```yaml
version: '3.8'

services:
  collab-server:
    build: .
    ports:
      - "4000:4000"
    volumes:
      - ./content:/app/content
    environment:
      - NODE_ENV=production
      - PORT=4000
    restart: unless-stopped
```

### Commands

```bash
# Build
docker build -t quiet-frames-collab-server .

# Run
docker run -d -p 4000:4000 \
  -v $(pwd)/content:/app/content \
  quiet-frames-collab-server

# With Docker Compose
docker-compose up -d
```

## Reverse Proxy

### Nginx

```nginx
server {
    listen 80;
    server_name quiet-frames.de;

    location / {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

### Caddy

```caddyfile
quiet-frames.de {
    reverse_proxy localhost:4000
}
```

## SSL/TLS

### With Certbot (Let's Encrypt)

```bash
certbot --nginx -d quiet-frames.de
```

### With Caddy (automatic)

Caddy automatically obtains certificates.

## PM2 (Process Manager)

```bash
# Installation
npm install -g pm2

# Start
pm2 start dist/server.js --name collab-server

# Cluster mode (Multi-Core)
pm2 start dist/server.js -i max --name collab-server

# Autostart
pm2 startup
pm2 save
```

### ecosystem.config.js

```javascript
module.exports = {
  apps: [{
    name: 'collab-server',
    script: 'dist/server.js',
    instances: 'max',
    exec_mode: 'cluster',
    env_production: {
      NODE_ENV: 'production',
      PORT: 4000
    }
  }]
};
```

## Monitoring

### Health Check

```bash
curl http://localhost:4000/health
```

### PM2 Monitoring

```bash
pm2 monit
pm2 logs collab-server
```

### External Services

- **Uptime Robot** – Availability
- **Sentry** – Error tracking
- **Grafana** – Metrics

## Backup

### Content Directory

```bash
# Daily at 2:00
0 2 * * * tar -czf /backups/content-$(date +\%Y\%m\%d).tar.gz /app/content
```

### Snapshots

```bash
# Weekly
0 3 * * 0 tar -czf /backups/snapshots-$(date +\%Y\%m\%d).tar.gz /app/data/snapshots
```

## Logs

### Log Paths

| Environment | Path                      |
|-------------|---------------------------|
| Development | Console                   |
| Production  | `/var/log/collab-server/` |
| Docker      | `docker logs`             |

### Log Rotation

```
/var/log/collab-server/*.log {
    daily
    rotate 14
    compress
    missingok
    notifempty
}
```

## Troubleshooting

### Port Already in Use

```bash
# Find process
lsof -i :4000

# Terminate
kill -9 <PID>
```

### Socket Timeouts

Increase Nginx timeout:

```nginx
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
```

### Memory Leaks

```bash
# Node with increased memory limit
NODE_OPTIONS="--max-old-space-size=4096" npm start
```