# Deployment Guide

This Astro application is configured for SSR (Server-Side Rendering) deployment using the Node.js adapter.

> **📋 Domain Migration**: For domain migration from workroot.com to workroot.in, see [`DOMAIN-MIGRATION-CHECKLIST.md`](./DOMAIN-MIGRATION-CHECKLIST.md) and [`MIGRATION-SUMMARY.md`](./MIGRATION-SUMMARY.md)

## Server Configuration

The application is configured to run with the following settings:

- **Host**: `0.0.0.0` (accepts connections from all network interfaces)
- **Port**: `10000` (configurable via `PORT` environment variable)

## Quick Start

### 1. Install Dependencies

```bash
npm install
```

### 2. Build the Application

```bash
npm run build
```

This will generate the production build in the `dist/` directory with:
- `dist/server/` - Server-side code and SSR entry point
- `dist/client/` - Static assets and client-side JavaScript

### 3. Start the Production Server

```bash
npm start
```

The server will start on `http://0.0.0.0:10000`

## Environment Variables

Create a `.env` file in the root directory to customize the server configuration:

```env
# Server host (default: 0.0.0.0)
HOST=0.0.0.0

# Server port (default: 10000)
PORT=10000

# Node environment
NODE_ENV=production
```

## Production Deployment Options

### Option 1: Docker Deployment

Create a `Dockerfile`:

```dockerfile
FROM node:20-alpine

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy built application
COPY dist ./dist
COPY server.mjs ./

# Expose port
EXPOSE 10000

# Set environment
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=10000

# Start server
CMD ["node", "server.mjs"]
```

Build and run:

```bash
docker build -t workroot-website .
docker run -p 10000:10000 workroot-website
```

### Option 2: PM2 Process Manager

Install PM2 globally:

```bash
npm install -g pm2
```

Create `ecosystem.config.cjs`:

```javascript
module.exports = {
  apps: [{
    name: 'workroot-website',
    script: './server.mjs',
    instances: 'max',
    exec_mode: 'cluster',
    env: {
      NODE_ENV: 'production',
      HOST: '0.0.0.0',
      PORT: 10000
    }
  }]
};
```

Start with PM2:

```bash
pm2 start ecosystem.config.cjs
pm2 save
pm2 startup  # Enable auto-start on system boot
```

### Option 3: Direct Node.js

```bash
NODE_ENV=production node server.mjs
```

## Platform-Specific Deployments

### Render.com

1. Connect your repository
2. Set build command: `npm run build`
3. Set start command: `npm start`
4. Set environment variable: `PORT=10000`

### Railway.app

1. Connect your repository
2. Railway will auto-detect the build and start commands
3. The app will automatically use the `PORT` environment variable

### DigitalOcean App Platform

1. Create a new app from your repository
2. Set build command: `npm run build`
3. Set run command: `npm start`
4. Configure port: `10000`

### VPS (Ubuntu/Debian)

```bash
# Install Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

# Clone repository
git clone <your-repo-url>
cd workroot-website

# Install dependencies and build
npm ci --only=production
npm run build

# Install PM2
sudo npm install -g pm2

# Start with PM2
pm2 start server.mjs --name workroot-website
pm2 save
pm2 startup

# Setup nginx reverse proxy (optional)
sudo apt install nginx
```

Nginx configuration (`/etc/nginx/sites-available/workroot`):

```nginx
server {
    listen 80;
    server_name workroot.in www.workroot.in;
    return 301 https://workroot.in$request_uri;
}

server {
    listen 443 ssl http2;
    server_name workroot.in;

    ssl_certificate /etc/letsencrypt/live/workroot.in/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/workroot.in/privkey.pem;

    # Security headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;

    location / {
        proxy_pass http://localhost:10000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

## Health Check

The server includes graceful shutdown handlers for `SIGTERM` and `SIGINT` signals.

You can verify the server is running by accessing:
- `http://localhost:10000` (local)
- `http://0.0.0.0:10000` (all interfaces)

## Performance Optimization

The application includes:
- ✅ Image optimization with Sharp (WebP conversion)
- ✅ HTML compression
- ✅ CSS code splitting
- ✅ Manual chunk splitting for better caching
- ✅ Lazy loading for images
- ✅ Prefetch with viewport strategy

## Security Considerations

1. Always use HTTPS in production (use a reverse proxy like nginx)
2. Set proper CORS headers if serving APIs
3. Keep dependencies updated: `npm audit fix`
4. Use environment variables for sensitive configuration
5. Enable rate limiting for API endpoints
6. Set proper CSP headers

## Monitoring

Consider adding monitoring tools:
- **PM2 Plus**: For production monitoring
- **New Relic**: Application performance monitoring
- **Sentry**: Error tracking
- **LogDNA/Datadog**: Log aggregation

## Troubleshooting

### Port already in use
```bash
# Find process using port 10000
lsof -i :10000  # macOS/Linux
netstat -ano | findstr :10000  # Windows

# Kill the process
kill -9 <PID>
```

### Server not accessible
- Verify firewall rules allow port 10000
- Check that HOST is set to `0.0.0.0` for external access
- Ensure the build completed successfully

### Static assets not loading
- Verify `dist/client/` directory exists
- Check that the server.mjs is in the project root
- Ensure build command ran successfully
