# =============================================================================
# Apache (443 SSL VirtualHost) — LiveKit'i aynı domain altında prefix ile yayınla
#
# Amaç:
# - Tarayıcı: wss://novazeka.com.tr/novazeka/livekit/rtc/...
# - Origin:  https://novazeka.com.tr/novazeka/...  (aynı site altında; CORS sorunu azalır)
# - Backend: https://novazeka.com.tr/novazeka/livekit/twirp/...  (RoomService/Egress)
#
# Kurulum:
# 1) Modülleri açın:
#    a2enmod proxy proxy_http proxy_wstunnel headers rewrite ssl
# 2) Bu bloğu novazeka.com.tr için aktif olan <VirtualHost *:443> içine ekleyin.
#    (Mümkünse genel rewrite/alias kurallarından ÖNCE)
# 3) apachectl -t && systemctl reload apache2   (veya httpd)
#
# Notlar:
# - LiveKit container'ı 127.0.0.1:7880 dinlemeli.
# - Cloudflare kullanıyorsanız: WebSockets açık; /novazeka/livekit/* için cache/WAF kuralı eklemeyin.
# =============================================================================

ProxyPreserveHost On
ProxyRequests Off

# WebSocket signaling
ProxyPass        "/novazeka/livekit/rtc"  "ws://127.0.0.1:7880/rtc"
ProxyPassReverse "/novazeka/livekit/rtc"  "ws://127.0.0.1:7880/rtc"

# Twirp HTTP API
ProxyPass        "/novazeka/livekit/twirp" "http://127.0.0.1:7880/twirp"
ProxyPassReverse "/novazeka/livekit/twirp" "http://127.0.0.1:7880/twirp"

# StopEgress / uzun Twirp (408 deadline_exceeded önleme)
ProxyTimeout 300

# JWT query uzunluğu için (Flutter web /rtc/validate çok uzun query string üretebilir)
LimitRequestFieldSize 16384
LimitRequestLine 16384

# Preflight (OPTIONS) — tarayıcı CORS
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^/novazeka/livekit/(rtc|twirp)/ - [R=204,L]

<IfModule mod_headers.c>
  Header always set Access-Control-Allow-Credentials "true"
  Header always set Vary "Origin"
  Header always set Access-Control-Expose-Headers "Grpc-Status, Grpc-Message, Grpc-Encoding, Grpc-Accept-Encoding"

  # Localhost Flutter web için (geliştirme)
  SetEnvIf Origin "^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$" ORIGIN_OK=$0
  Header always set Access-Control-Allow-Origin "%{ORIGIN_OK}e" env=ORIGIN_OK
</IfModule>
