Files
api-btekno/SETUP_LOCAL_API.md

143 lines
3.5 KiB
Markdown
Raw Normal View History

# Setup API Lokal untuk Testing
## Struktur Folder
```
C:\laragon\www\
├── api-btekno\ # Backend API
│ └── public\ # Document root
│ ├── index.php # Entry point
│ └── .htaccess # URL rewrite
└── Retribusi\ # Frontend
└── public\ # Document root frontend
```
## Cara Akses API Lokal
### Opsi 1: Menggunakan Path Langsung (Laragon/XAMPP)
Jika menggunakan Laragon/XAMPP dengan struktur folder di atas:
**Base URL:** `http://localhost/api-btekno/public`
**Contoh endpoint:**
- Health: `http://localhost/api-btekno/public/health`
- Login: `http://localhost/api-btekno/public/auth/v1/login`
- Dashboard Summary: `http://localhost/api-btekno/public/retribusi/v1/dashboard/summary?date=2026-01-01`
### Opsi 2: Setup Virtual Host (Recommended)
Buat virtual host di Laragon untuk akses yang lebih clean:
1. Buka Laragon → Menu → Apache → Sites-enabled
2. Buat file baru: `api-retribusi.test.conf`
3. Isi dengan:
```apache
<VirtualHost *:80>
ServerName api-retribusi.test
DocumentRoot "C:/laragon/www/api-btekno/public"
<Directory "C:/laragon/www/api-btekno/public">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
```
4. Restart Apache di Laragon
5. Edit `C:\Windows\System32\drivers\etc\hosts` (run as Administrator):
```
127.0.0.1 api-retribusi.test
```
6. Akses: `http://api-retribusi.test/health`
**Base URL untuk config.js:** `http://api-retribusi.test`
## Test API Lokal
### 1. Test Health Endpoint
```bash
# Via browser
http://localhost/api-btekno/public/health
# Via curl
curl http://localhost/api-btekno/public/health
# Expected response:
# {"status":"ok","time":1767284697}
```
### 2. Test Login
```bash
curl -X POST http://localhost/api-btekno/public/auth/v1/login \
-H "Content-Type: application/json" \
-H "X-API-KEY: POKOKEIKISEKOYOLO" \
-d '{"username":"admin","password":"password"}'
```
### 3. Test Dashboard Summary
```bash
curl http://localhost/api-btekno/public/retribusi/v1/dashboard/summary?date=2026-01-01 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-API-KEY: POKOKEIKISEKOYOLO"
```
## Troubleshooting
### Error: 404 Not Found
**Kemungkinan penyebab:**
1. `.htaccess` belum ada di folder `public/`
2. `mod_rewrite` belum aktif di Apache
3. Path salah
**Solusi:**
1. Pastikan file `api-btekno/public/.htaccess` ada
2. Aktifkan `mod_rewrite` di Laragon:
- Menu → Apache → Modules → Centang `rewrite_module`
- Restart Apache
3. Cek path: `http://localhost/api-btekno/public/health` (dengan `/public`)
### Error: 500 Internal Server Error
**Kemungkinan penyebab:**
1. Database connection error
2. `.env` file tidak ada atau salah konfigurasi
3. PHP version tidak sesuai
**Solusi:**
1. Cek file `api-btekno/.env` ada dan konfigurasi database benar
2. Cek PHP version: `php -v` (harus >= 8.2.0)
3. Cek error log di Laragon
### Error: CORS
**Kemungkinan penyebab:**
1. CORS middleware belum aktif
2. Origin tidak di-allow
**Solusi:**
1. Pastikan `CorsMiddleware` sudah di-register di `index.php`
2. Cek konfigurasi CORS di `src/Middleware/CorsMiddleware.php`
## Update Frontend Config
Setelah API lokal bisa diakses, update `Retribusi/public/dashboard/js/config.js`:
```javascript
// Force local mode
const FORCE_LOCAL_MODE = true;
// Atau auto-detect (akan otomatis detect localhost)
const FORCE_LOCAL_MODE = false;
```
Base URL akan otomatis: `http://localhost/api-btekno/public`