worker_processes  1;
error_log /var/log/nginx/error.log warn;

events {
    worker_connections  1024;
}

rtmp {
    
    server {
        listen 1935;  # RTMP listen port

        application live {
            live on;
            # No RTMP playback
            deny play all;
            push rtmp://127.0.0.1:1935/hls-live;

            on_publish http://web_server:5000/init_stream;  # if the stream is detected from OBS
            on_publish_done http://web_server:5000/end_stream;  # if the stream is ended on OBS

        }

        application hls-live {
            live on;

            hls on;
            hls_path /user_data/;

            allow publish 127.0.0.1;
            deny publish all;

            hls_nested on;
            hls_fragment 1s;
            hls_playlist_length 4s;
            hls_fragment_naming system;
            hls_cleanup off;
        }
    }
}

http {
    access_log off;
    # Enable HLS
    server {
        listen 8080;
        root /var/www;

        location /api/ {
            rewrite ^/api/(.*)$ /$1 break;
            proxy_pass http://web_server:5000;  # web_server is the name of the Flask container in docker-compose
            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;
        }
        
        location /socket.io/ {
            proxy_pass http://web_server:5000/socket.io/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            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_cache_bypass $http_upgrade;
            proxy_buffers 8 32k;
            proxy_buffer_size 64k;
            proxy_read_timeout 86400;
        }

        location /hmr/ {
            proxy_pass http://frontend:5173;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        ## Non cacheable HLS files, m3u8 and png
        location ~ ^/stream/(.+)/(.+\.(m3u8|png))$ {
            alias /user_data/$1/stream/$2;

            # Let the MPEG-TS video chunks be cacheable
            expires -1d;
        }

        ## Cacheable HLS files, ts
        location ~ ^/stream/(.+)/(.+\.ts)$ {
            # Call backend to check permission
            alias /user_data/$1/stream/$2;

            # Let the MPEG-TS video chunks be cacheable
            expires max;
        }

        ## VoDs Location and thumbnails
        ## Contains MP4 files and PNG thumbnails
        location ~ ^/vods/(.+)/(.+\.(mp4|png))$ {
            alias /user_data/$1/vods/$2;
            # where $1 is the user's username and $2 is the thumbnail_name

            # The thumbnails should not be cacheable
            expires -1d;
        }

        ## Profile pictures location
        location ~ ^/user/(.+)/profile_picture$ {
            alias /user_data/$1/index.png;
            # where $1 is the user's username

            # The profile pictures should not be cacheable
            expires -1d;
        }
        

        location ~ ^/\?token=.*$ {
            proxy_pass http://frontend:5173;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        location / {
            proxy_pass http://frontend:5173;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}
