# Panduan Deploy Webhook Accurate ke VPS (Dari Awal Sampai systemctl)

Panduan ini dari **komputer Anda (Windows)** sampai **layanan jalan di VPS** dengan `systemctl`.

---

## Yang Anda Butuhkan

- **Windows**: project `lims` (folder `golang-webhook-service` ada di dalamnya)
- **VPS**: akses SSH (user + IP/hostname)
- **Di Windows**: Go terpasang (untuk build binary Linux)

---

# BAGIAN 1: Di Komputer Anda (Windows)

## 1.1 Buka folder project

Di **PowerShell** atau **Command Prompt**:

```powershell
cd c:\Users\MSahrul\Documents\PROJECT\lims\golang-webhook-service
```

## 1.2 Build binary untuk Linux (VPS)

VPS pakai Linux, jadi kita build untuk Linux:

```powershell
$env:GOOS = "linux"
$env:GOARCH = "amd64"
go build -o webhook-service .
```

Selesai akan ada file **`webhook-service`** (tanpa .exe) untuk Linux.

*(Setelah ini, kalau Anda build lagi untuk Windows, jalankan: `$env:GOOS = "windows"; $env:GOARCH = "amd64"; go build -o webhook-service.exe .`)*

## 1.3 Siapkan file yang akan dikirim ke VPS

Yang **wajib** dikirim:

| File/Folder | Keterangan |
|-------------|------------|
| `webhook-service` | Binary Linux hasil build (langkah 1.2) |
| `.env` | Konfigurasi (PORT, DB_PATH, SHARED_HOSTING_URL, secret, dll) |
| `deploy/webhook-accurate.service` | Unit systemd |

**Opsional** (boleh dikirim kalau mau simpan di server):

- `deploy/README-SYSTEMD.md`, `deploy/PANDUAN-DEPLOY-VPS.md` (dokumentasi)

**Tidak perlu** dikirim ke VPS untuk jalan:

- `*.exe`, folder `config/`, `handlers/`, `worker/`, `main.go`, `go.mod`, dll (kode sumber). Cukup binary + .env + unit systemd.

---

# BAGIAN 2: Kirim File ke VPS

Pilih salah satu cara.

## Opsi A: Pakai SCP (dari Windows)

Ganti `USER` dan `VPS_IP` dengan user dan IP VPS Anda (mis. `root@103.xxx.xxx.xxx` atau `ubuntu@server.example.com`).

```powershell
# Masih di folder golang-webhook-service
cd c:\Users\MSahrul\Documents\PROJECT\lims\golang-webhook-service

scp webhook-service USER@VPS_IP:/tmp/
scp .env USER@VPS_IP:/tmp/
scp deploy/webhook-accurate.service USER@VPS_IP:/tmp/
```

File sementara di VPS ada di `/tmp/`.

## Opsi B: Pakai Git (jika repo sudah di Git)

**Di Windows:** commit + push (termasuk binary jika mau, atau tidak usah—lihat di bawah).

**Di VPS:** clone/pull lalu build di server:

```bash
cd /home/USER
git clone https://..../lims.git
cd lims/golang-webhook-service
GOOS=linux go build -o webhook-service .
# .env tetap harus dibuat/copy di VPS (jangan commit .env ke Git)
```

Lalu lanjut ke Bagian 3 (path di VPS sesuaikan, mis. `/home/USER/lims/golang-webhook-service`).

## Opsi C: Upload manual (FileZilla / WinSCP)

1. Buka FileZilla/WinSCP, connect ke VPS.
2. Upload ke folder sementara (mis. `/tmp/` atau `/home/USER/`):
   - `webhook-service`
   - `.env`
   - `deploy/webhook-accurate.service`

Lalu lanjut ke Bagian 3; di server pindahkan file ke lokasi akhir.

---

# BAGIAN 3: Di VPS (SSH)

Login dulu:

```bash
ssh USER@VPS_IP
```

Ganti **path** dan **user** di bawah sesuai VPS Anda. Contoh pakai `/opt/webhook-accurate` dan user `www-data`. Kalau Anda pakai user lain (mis. `ubuntu`, `deploy`), ganti saja.

## 3.1 Buat folder layanan

```bash
sudo mkdir -p /opt/webhook-accurate/data
```

`data/` dipakai untuk SQLite (sesuai `DB_PATH` di .env).

## 3.2 Pindahkan file dari /tmp ke folder layanan

Jika tadi Anda kirim ke `/tmp/`:

```bash
sudo mv /tmp/webhook-service /opt/webhook-accurate/
sudo mv /tmp/.env /opt/webhook-accurate/
sudo chmod +x /opt/webhook-accurate/webhook-service
```

Jika Anda pakai path lain (mis. repo di `/home/ubuntu/lims/golang-webhook-service`), ganti sumber/tujuan path-nya.

## 3.3 Sesuaikan .env (path database)

Edit .env di VPS:

```bash
sudo nano /opt/webhook-accurate/.env
```

Pastikan **DB_PATH** mengarah ke dalam folder layanan, misalnya:

```env
PORT=9000
DB_PATH=/opt/webhook-accurate/data/webhook.db
```

Simpan (Ctrl+O, Enter, Ctrl+X).

## 3.4 Atur kepemilikan file

Agar systemd jalan sebagai `www-data` (atau user yang Anda pilih):

```bash
sudo chown -R www-data:www-data /opt/webhook-accurate
```

Kalau Anda ingin service jalan sebagai user Anda (mis. `ubuntu`):

```bash
sudo chown -R ubuntu:ubuntu /opt/webhook-accurate
```

(Nanti di unit file, `User` dan `Group` juga harus `ubuntu`.)

## 3.5 Pasang unit systemd

```bash
sudo cp /tmp/webhook-accurate.service /etc/systemd/system/
```

Kalau file .service tidak di `/tmp/` (mis. dari clone repo):

```bash
sudo cp /path/ke/webhook-accurate.service /etc/systemd/system/
```

Edit unit agar path dan user cocok dengan VPS:

```bash
sudo nano /etc/systemd/system/webhook-accurate.service
```

Pastikan:

- **WorkingDirectory** = folder yang berisi `webhook-service` dan `.env` (mis. `/opt/webhook-accurate`)
- **ExecStart** = path penuh ke binary (mis. `/opt/webhook-accurate/webhook-service`)
- **User** dan **Group** = user yang punya file (mis. `www-data` atau `ubuntu`)

Contoh isi:

```ini
[Unit]
Description=Accurate Webhook Relay (Golang)
After=network.target

[Service]
Type=simple
User=www-data
Group=www-data

WorkingDirectory=/opt/webhook-accurate
ExecStart=/opt/webhook-accurate/webhook-service

Restart=always
RestartSec=5

StandardOutput=journal
StandardError=journal
SyslogIdentifier=webhook-accurate

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

Simpan (Ctrl+O, Enter, Ctrl+X).

## 3.6 Muat ulang systemd, aktifkan, dan jalankan

```bash
sudo systemctl daemon-reload
sudo systemctl enable webhook-accurate
sudo systemctl start webhook-accurate
sudo systemctl status webhook-accurate
```

Status harus **active (running)** (warna hijau).

## 3.7 Cek log (kalau ada error)

```bash
sudo journalctl -u webhook-accurate -f
```

Keluar dengan Ctrl+C.

## 3.8 Tes endpoint

Di VPS atau dari komputer Anda:

```bash
curl http://VPS_IP:9000/health
```

Harus dapat respons: **OK**.

Port `9000` sesuai **PORT** di `.env`. Jika pakai port lain, ganti di curl.

---

# BAGIAN 4: Ringkasan Perintah (Copy-Paste)

**Di Windows (PowerShell) – build & kirim:**

```powershell
cd c:\Users\MSahrul\Documents\PROJECT\lims\golang-webhook-service
$env:GOOS = "linux"; $env:GOARCH = "amd64"; go build -o webhook-service .
scp webhook-service USER@VPS_IP:/tmp/
scp .env USER@VPS_IP:/tmp/
scp deploy/webhook-accurate.service USER@VPS_IP:/tmp/
```

**Di VPS (SSH) – pasang & jalankan:**

```bash
sudo mkdir -p /opt/webhook-accurate/data
sudo mv /tmp/webhook-service /opt/webhook-accurate/
sudo mv /tmp/.env /opt/webhook-accurate/
sudo chmod +x /opt/webhook-accurate/webhook-service
sudo nano /opt/webhook-accurate/.env
# Pastikan DB_PATH=/opt/webhook-accurate/data/webhook.db dan PORT=9000
sudo chown -R www-data:www-data /opt/webhook-accurate
sudo cp /tmp/webhook-accurate.service /etc/systemd/system/
sudo nano /etc/systemd/system/webhook-accurate.service
# Sesuaikan WorkingDirectory, ExecStart, User, Group
sudo systemctl daemon-reload
sudo systemctl enable webhook-accurate
sudo systemctl start webhook-accurate
sudo systemctl status webhook-accurate
curl http://localhost:9000/health
```

---

# BAGIAN 5: Setelah Ini (Pemeliharaan)

| Tujuan | Perintah |
|--------|----------|
| Lihat status | `sudo systemctl status webhook-accurate` |
| Lihat log live | `sudo journalctl -u webhook-accurate -f` |
| Restart (setelah ganti .env atau binary) | `sudo systemctl restart webhook-accurate` |
| Stop | `sudo systemctl stop webhook-accurate` |
| Start | `sudo systemctl start webhook-accurate` |

**Update binary (setelah Anda build ulang di Windows dan kirim lagi):**

```bash
# Di VPS
sudo systemctl stop webhook-accurate
sudo cp /tmp/webhook-service /opt/webhook-accurate/
sudo chown www-data:www-data /opt/webhook-accurate/webhook-service
sudo systemctl start webhook-accurate
sudo systemctl status webhook-accurate
```

---

# BAGIAN 6: Nginx (Opsional)

Kalau Anda ingin akses lewat **domain** atau **port 80/443** (bukan langsung `VPS_IP:9000`), pakai Nginx sebagai reverse proxy.

Contoh konfigurasi Nginx (ganti `webhook.domain.com` dan path SSL jika pakai HTTPS):

```nginx
server {
    listen 80;
    server_name webhook.domain.com;

    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_http_version 1.1;
        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;
    }
}
```

Simpan di `/etc/nginx/sites-available/webhook-accurate`, aktifkan, tes, reload:

```bash
sudo ln -s /etc/nginx/sites-available/webhook-accurate /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

Lalu di **Accurate**, URL webhook bisa diisi: `http://webhook.domain.com/webhook/accurate` (atau pakai HTTPS jika sudah ada SSL).

---

Dengan ini, alur dari **perubahan file di Windows** sampai **layanan jalan di VPS dengan systemctl** sudah tercakup dari awal hingga akhir.
