# API Deployment Guide for cPanel

## 1. Prepare API for Production

1. Create a production build:
```bash
cd packages/api
npm run build
```

2. Create a `.htaccess` file in your API directory:
```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ public/    [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>
```

3. Create `ecosystem.config.js` for PM2:
```javascript
module.exports = {
  apps: [{
    name: 'lensseek-api',
    script: 'dist/main.js',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'production',
      PORT: 8080  // Change this to match your cPanel Node.js app port
    }
  }]
}
```

## 2. cPanel Setup for API

1. Log in to your cPanel account

2. Navigate to "Setup Node.js App" in cPanel

3. Create a new Node.js application:
   - Application name: lensseek-api
   - Application mode: Production
   - Node.js version: 16.x or higher
   - Application root: /path/to/your/api
   - Application URL: api.yourdomain.com
   - Application startup file: dist/main.js
   - Environment variables (add all required env variables)

4. Upload Files:
   - Use File Manager or FTP to upload the built API files
   - Directory structure should be:
     ```
     public_html/api/
     ├── dist/
     ├── node_modules/
     ├── package.json
     ├── ecosystem.config.js
     └── .env
     ```

5. Install Dependencies:
```bash
npm install --production
```

6. Start the Application:
   - In cPanel Node.js App interface, click "Start" for your application
   - Or via SSH:
     ```bash
     cd ~/public_html/api
     pm2 start ecosystem.config.js
     ```

## 3. Database Setup

1. Create MySQL Database:
   - Go to "MySQL Database Wizard" in cPanel
   - Create a new database and user
   - Grant privileges to the user

2. Update `.env` file with production database credentials:
```env
DATABASE_URL="mysql://username:password@localhost:3306/database_name"
```

3. Run Prisma migrations:
```bash
npx prisma migrate deploy
```

## 4. SSL Setup

1. Install SSL Certificate:
   - Go to "SSL/TLS" in cPanel
   - Install Let's Encrypt SSL or upload your certificate

2. Force HTTPS in `.htaccess`:
```apache
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```

## 5. Testing

1. Test API endpoints:
```bash
curl https://api.yourdomain.com/health
```

2. Monitor logs:
   - Check Node.js app logs in cPanel
   - Or via PM2: `pm2 logs lensseek-api`
