# =============================================================
# Portal Wali Kelas – .htaccess for LiteSpeed / Apache on cPanel
# =============================================================

# Enable mod_rewrite
Options -MultiViews
RewriteEngine On

# ===================================================
# OPTIMASI JUMLAH PROSES (PENTING UNTUK LIMIT 40/40)
# ===================================================
<IfModule mod_passenger.c>
    # 1. Batasi maksimal 1 proses per aplikasi (Node.js sangat kuat dengan 1 proses)
    PassengerMaxInstances 1
    # 2. Jangan ada proses yang "standby" menganggur
    PassengerMinInstances 0
    # 3. Matikan aplikasi secara otomatis jika tidak ada pengunjung selama 60 detik
    PassengerPoolIdleTime 60
</IfModule>

# ===================================================
# CRITICAL: Prevent LiteSpeed from caching ANY /api/ request
# LiteSpeed by default caches PUT/POST responses — this breaks data saves!
# ===================================================
<IfModule mod_headers.c>
    # Strip any cache from API routes completely
    <LocationMatch "^/api/">
        Header always set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
        Header always set Pragma "no-cache"
        Header always set Expires "0"
        Header always set Surrogate-Control "no-store"
        Header always set X-Accel-Expires "0"
    </LocationMatch>
</IfModule>

# LiteSpeed specific: disable caching for PUT, POST, PATCH, DELETE
# These must NOT be cached by any reverse proxy
<IfModule LiteSpeed>
    RewriteCond %{REQUEST_METHOD} ^(PUT|POST|PATCH|DELETE)$
    RewriteRule .* - [E=no-cache:1]
    CacheLookup off
</IfModule>

# Disable ESI (Edge Side Includes) caching
<IfModule mod_cache.c>
    CacheDisable /api/
</IfModule>

# ===================================================
# Node.js App – Forward all /api/ requests to Node.js
# (Phusion Passenger handles this, but .htaccess must not intercept it)
# ===================================================

# Let Node.js API handle all /api/* routes – do NOT rewrite them
RewriteCond %{REQUEST_URI} ^/api/
RewriteRule ^ - [L]

# ===================================================
# SPA Fallback – serve index.html for all other routes
# ===================================================
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
