# Deployment

Panduan deploy aplikasi ke production.

## Development Server

```bash
source .venv/bin/activate
python run.py
```

Server berjalan di `http://0.0.0.0:8888` dengan debug mode.

## Production (Gunicorn)

### Install

```bash
pip install gunicorn
```

### Jalankan

```bash
gunicorn "app:create_app()" \
    --bind 0.0.0.0:8888 \
    --workers 3 \
    --timeout 120 \
    --access-logfile logs/access.log \
    --error-logfile logs/error.log
```

### Systemd Service

Buat file `/etc/systemd/system/koinkamu.service`:

```ini
[Unit]
Description=Koinkamu Investment Platform
After=network.target mysql.service

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/investasi.kamu.co.id
Environment="PATH=/var/www/investasi.kamu.co.id/.venv/bin"
ExecStart=/var/www/investasi.kamu.co.id/.venv/bin/gunicorn "app:create_app()" --bind 127.0.0.1:8888 --workers 3 --timeout 120
Restart=always

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl enable koinkamu
sudo systemctl start koinkamu
sudo systemctl status koinkamu
```

## Database (MySQL/MariaDB)

### XAMPP (Development)

```bash
# Start
/Applications/XAMPP/xamppfiles/bin/mysql.server start

# Stop
/Applications/XAMPP/xamppfiles/bin/mysql.server stop

# MySQL CLI
/Applications/XAMPP/xamppfiles/bin/mysql -u root
```

### Production

Pastikan MySQL/MariaDB sudah running dan database sudah dibuat:

```bash
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS cornbyt1_investasikamu CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
```

### Backup

```bash
# Full backup
mysqldump -u root cornbyt1_investasikamu > backup_$(date +%Y%m%d).sql

# Restore
mysql -u root cornbyt1_investasikamu < backup_20260228.sql
```

## Nginx Reverse Proxy

```nginx
server {
    listen 80;
    server_name investasi.kamu.co.id;

    location / {
        proxy_pass http://127.0.0.1:8888;
        proxy_set_header Host $host;
        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;
    }

    location /static/ {
        alias /var/www/investasi.kamu.co.id/app/static/;
        expires 30d;
    }
}
```

## Cron Jobs

Lihat [CLI Scripts > Cron Jobs](cli-scripts) untuk contoh lengkap cron setup.

## Environment Variables

Pastikan `.env` production memiliki:

```bash
FLASK_ENV=production
FLASK_DEBUG=0
SECRET_KEY=<random-64-char-string>
```

> **Penting:** Jangan gunakan `FLASK_DEBUG=1` di production.

## Checklist Deploy

1. Clone repository ke server
2. Setup virtual environment + install dependencies
3. Copy `.env.example` ke `.env`, sesuaikan credentials
4. Start MySQL, buat database
5. Jalankan seed scripts (`seed_data.py`, `seed_admin.py`)
6. Setup Gunicorn (systemd service)
7. Setup Nginx reverse proxy
8. Setup SSL (Let's Encrypt)
9. Setup cron jobs untuk sync
10. Test semua endpoint
