# Apache Virtual Host Configuration for Vision Videoke
# Subdirectory setup at /var/www/html/music
# This configuration allows Vision Videoke to run alongside existing sites

# Add this to your existing Apache virtual host configuration
# or include it in your main site's .htaccess file

# Vision Videoke subdirectory configuration
<Directory "/var/www/html/music">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    
    # Enable rewrite engine for React Router
    RewriteEngine On
    
    # Handle React Router (SPA) routing
    # Redirect all requests to index.html except for actual files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/music/api/
    RewriteRule ^(.*)$ /music/index.html [L,QSA]
    
    # Security headers
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
    
    # CORS headers for API requests
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
    
    # Handle preflight OPTIONS requests
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$ $1 [R=200,L]
</Directory>

# Proxy configuration for Node.js backend API
# This forwards API requests to the Node.js application running on port 3001
<Location "/music/api">
    ProxyPreserveHost On
    ProxyPass http://127.0.0.1:3001/api
    ProxyPassReverse http://127.0.0.1:3001/api
    
    # WebSocket support (if needed)
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteCond %{HTTP:Connection} upgrade [NC]
    RewriteRule ^/music/api/(.*) "ws://127.0.0.1:3001/api/$1" [P,L]
</Location>

# File upload handling with increased limits
<Location "/music/api/upload">
    # Increase upload limits
    LimitRequestBody 104857600  # 100MB
    
    # Extended timeouts for file uploads
    ProxyTimeout 300
    ProxyPass http://127.0.0.1:3001/api/upload
    ProxyPassReverse http://127.0.0.1:3001/api/upload
</Location>

# Static file caching for performance
<LocationMatch "/music/\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
    ExpiresActive On
    ExpiresDefault "access plus 1 year"
    Header append Cache-Control "public, immutable"
</LocationMatch>

# HTML files with short cache
<LocationMatch "/music/.*\.html$">
    ExpiresActive On
    ExpiresDefault "access plus 1 hour"
    Header append Cache-Control "public, must-revalidate"
</LocationMatch>

# Gzip compression for better performance
<Location "/music">
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \
        \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
</Location>

# Security: Block access to sensitive files
<LocationMatch "/music/(\.env|package\.json|ecosystem\.config\.js|\.git)">
    Require all denied
</LocationMatch>

# Health check endpoint (accessible without authentication)
<Location "/music/health">
    ProxyPass http://127.0.0.1:3001/health
    ProxyPassReverse http://127.0.0.1:3001/health
</Location>

# Rate limiting (if mod_evasive is available)
<IfModule mod_evasive24.c>
    <Location "/music/api">
        DOSHashTableSize    2048
        DOSPageCount        10
        DOSPageInterval     1
        DOSSiteCount        50
        DOSSiteInterval     1
        DOSBlockingPeriod   600
    </Location>
</IfModule>

# Optional: Basic authentication for admin areas
# <Location "/music/admin">
#     AuthType Basic
#     AuthName "Vision Videoke Admin"
#     AuthUserFile /etc/apache2/.htpasswd
#     Require valid-user
# </Location>

