# AGENTS_SYNC.md — Server Setup & Hybrid Workflow

## Server Info

| Item | Detail |
|---|---|
| **IP** | 188.166.231.167 |
| **Hostname** | JuniorDept |
| **OS** | Ubuntu 24.04.3 LTS |
| **RAM** | 3.8GB |
| **CPU** | 2 cores |
| **Node.js** | v20.20.2 (`/usr/bin/node` — system, bukan nvm) |
| **Yarn** | via corepack |
| **PostgreSQL** | v16.14 |
| **Web Server** | Apache2 (reverse proxy) |
| **Process Manager** | PM2 (v2.10.3) |

## Domain & Port Mapping

| Environment | Domain | Backend Port | Storefront Port | Project Dir |
|---|---|---|---|---|
| **Production** | `totti-new.dpanell.com` | 9000 | 8000 | `/var/www/totti-commerce` |
| **Staging** | `staging.dpanell.com` | 9001 | 8001 | `/var/www/staging` |

## Database

| Environment | DB Name | User | Password |
|---|---|---|---|
| **Production** | `totti_medusa` | `totti` | `totti2024secure` |
| **Staging** | `totti_staging` | `staging` | `staging2026secure` |

### Koneksi
```
postgres://totti:totti2024secure@localhost:5432/totti_medusa        # production
postgres://staging:staging2026secure@localhost:5432/totti_staging    # staging
```

## Apache Reverse Proxy

### Production (`totti-new.conf`)
- `/app`, `/admin`, `/auth`, `/store`, `/hooks`, `/health` → `localhost:9000` (MedusaJS)
- Semua path lain (`/`) → `localhost:8000` (Next.js storefront)

### Staging (`staging.dpanell.com`)
- Config di `/etc/apache2/sites-enabled/web-staging.conf`
- **Belum ada reverse proxy** — masih serve file statis
- Perlu diupdate seperti config production (port 9001/8001)

### Config Staging (yang perlu di-set)
```apache
<VirtualHost *:80>
    ServerName staging.dpanell.com

    ProxyPreserveHost On
    ProxyRequests Off

    # Medusa admin UI
    ProxyPass /app http://localhost:9001/app
    ProxyPassReverse /app http://localhost:9001/app

    # Medusa API paths
    ProxyPass /admin http://localhost:9001/admin
    ProxyPassReverse /admin http://localhost:9001/admin

    ProxyPass /auth http://localhost:9001/auth
    ProxyPassReverse /auth http://localhost:9001/auth

    ProxyPass /store http://localhost:9001/store
    ProxyPassReverse /store http://localhost:9001/store

    ProxyPass /hooks http://localhost:9001/hooks
    ProxyPassReverse /hooks http://localhost:9001/hooks

    ProxyPass /health http://localhost:9001/health
    ProxyPassReverse /health http://localhost:9001/health

    # Next.js storefront (fallback)
    ProxyPass / http://localhost:8001/
    ProxyPassReverse / http://localhost:8001/

    ErrorLog ${APACHE_LOG_DIR}/staging.error.log
    CustomLog ${APACHE_LOG_DIR}/staging.access.log combined
</VirtualHost>
```

## PM2 Ecosystem

### Production (`/var/www/totti-commerce/ecosystem.config.js`)
```javascript
apps: [
  { name: 'totti-backend',    cwd: '/var/www/totti-commerce/apps/backend',   script: 'yarn', args: 'start', interpreter: 'none' },
  { name: 'totti-storefront', cwd: '/var/www/totti-commerce/apps/storefront', script: 'yarn', args: 'start', interpreter: 'none' },
]
```

### Staging (`/var/www/staging/ecosystem.config.js`)
```javascript
apps: [
  { name: 'staging-backend',    cwd: '/var/www/staging/apps/backend',   script: 'yarn', args: 'start', interpreter: 'none' },
  { name: 'staging-storefront', cwd: '/var/www/staging/apps/storefront', script: 'yarn', args: 'start', interpreter: 'none' },
]
```

## Hybrid Workflow

### Konsep
```
Local (Windows) → Edit kode → rsync ke server → Build → PM2 restart → Test di staging.dpanell.com
```

### Sync Local → Server
```bash
cd /path/to/totti-commerce
rsync -avz \
  --exclude='node_modules' \
  --exclude='.next' \
  --exclude='.medusa' \
  --exclude='.turbo' \
  ./ root@188.166.231.167:/var/www/staging/
```

### Build & Deploy di Server (SSH)
```bash
# Install deps (jika ada package baru)
cd /var/www/staging && yarn install
cd apps/storefront && yarn install && cd ../..

# Build backend
cd apps/backend && yarn build && cd ../..

# Build storefront
cd apps/storefront && yarn build && cd ../..

# Migrate database (jika ada migration baru)
cd apps/backend && yarn medusa db:migrate && cd ../..

# Restart services
pm2 restart staging-backend staging-storefront
```

### Test
- **Storefront**: https://staging.dpanell.com
- **Admin**: https://staging.dpanell.com/app
- **API**: https://staging.dpanell.com/store

## Important Commands di Server

```bash
# Cek status services
pm2 list
pm2 logs staging-backend
pm2 logs staging-storefront

# Restart
pm2 restart staging-backend
pm2 restart staging-storefront

# Apache
sudo apache2ctl configtest
sudo systemctl restart apache2

# Database
sudo -u postgres psql -c "\l"
```

## SSH Access
```bash
ssh root@188.166.231.167
# Key: D:/A1 PROJECT/dPanel/SSH/sshprivate_vscode
```

## Environment Variables Staging (`apps/backend/.env`)
```
MEDUSA_ADMIN_ONBOARDING_TYPE=default
STORE_CORS=https://staging.dpanell.com
ADMIN_CORS=https://staging.dpanell.com,http://staging.dpanell.com,http://localhost:9001
AUTH_CORS=https://staging.dpanell.com,http://staging.dpanell.com,http://localhost:9001,http://localhost:8001
REDIS_URL=redis://localhost:6379
JWT_SECRET=<generate_baru>
COOKIE_SECRET=<generate_baru>
DATABASE_URL=postgres://staging:staging2026secure@localhost:5432/totti_staging
MEDUSA_BACKEND_URL=https://staging.dpanell.com
RAJAONGKIR_API_KEY=640f099bca5a28d5dc4e8c296bc61505
RAJAONGKIR_ORIGIN_CITY=17596
XENDIT_API_KEY=<sandbox_atau_production>
XENDIT_WEBHOOK_SECRET=<sama_dengan_api_key_terkait>
STORE_URL=https://staging.dpanell.com
REVALIDATE_SECRET=<generate_baru>
```

## Notes
- Redis terinstall di server (`REDIS_URL=redis://localhost:6379` di .env production)
- Xendit API key production sudah terpakai — untuk staging gunakan sandbox
- Server spec rendah (2 core, 3.8GB RAM) — untuk 2 environment (prod+staging) perlu dijaga
- `.vscode/sftp.json` sudah terkonfigurasi untuk sync ke `/var/www/staging`
