
# Log errors to stdout - Tested?
error_log /dev/stdout warn;

events {}

http {
    log_format my_upstream '$remote_addr [$time_local] "$request" $status'
    ' "$upstream_addr" $upstream_response_time $upstream_http_etag $remaining_part';

    # if SERVICE_SSL_ENABLED flag is set, following lines are commented in and the path to the certs is set
    #ssl_certificate
    #ssl_certificate_key

    client_max_body_size 10G;
    client_body_timeout 300s;
    client_header_timeout 120s;

    proxy_connect_timeout 600;
    proxy_send_timeout 600;
    proxy_read_timeout 600;
    send_timeout 600;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

     map $upstream_http_location $new_location {
        default .$upstream_http_location;
    }

    server {
        listen 8080;

        #auth_basic "Restricted Access";
        #auth_basic_user_file /etc/nginx/.htpasswd;

        # only do relative redirects
        absolute_redirect off;

        error_page 502 504 /5xx.html;
        error_page 497 https://$http_host$request_uri;

        location = /5xx.html {
            auth_basic off;
            root /resources/;
            internal;
        }

        location = / {
            # relative redirect
            return 302 demos/playground/;
        }

        location = /demos/ {
            # relative redirect
            return 302 playground/;
        }

        location = /ping {
            auth_basic off;
            return 200;
        }

        # if url is called without trailing slash, add a trailing slash, otherwise it cannot be routed correctly.
        location ~* "^{BASE_URL_DECODED}/demos/(?<last_part>[^/]+)$" {
            # uri is only the path whereas request_uri also contains args
            # Use last part to have a relative redirect
            return 302 $last_part/$is_args$args;
        }

        location ~* "^{BASE_URL_DECODED}/demos/(?<demo>[a-zA-Z_]+)/(?<remaining_part>.*)" {
            access_log /var/log/nginx/upstream.log my_upstream;

            # Allow CORS requests
            if ($request_method = OPTIONS) {
                add_header Access-Control-Allow-Origin "$http_origin";
                add_header Access-Control-Allow-Credentials "true";
                add_header Access-Control-Allow-Methods "GET, OPTIONS, DELETE, POST, PUT";
                add_header Access-Control-Allow-Headers "Authorization, Content-Type";
                add_header Content-Length 0;
                add_header Content-Type text/plain;
                return 200;
            }

            if ($demo = playground) {
                set $demo_port 7080;
            }

            if ($demo = hello_world_ui) {
                set $demo_port 7081;
            }

            if ($demo = hello_world_api) {
                set $demo_port 6081;
            }

            if ($demo = train_word_vectors_ui) {
                set $demo_port 7082;
            }

            if ($demo = train_word_vectors_api) {
                set $demo_port 6082;
            }

            if ($demo = seperate_audio_ui) {
                set $demo_port 7083;
            }

            if ($demo = seperate_audio_api) {
                set $demo_port 6083;
            }

            if ($demo = question_answering_ui) {
                set $demo_port 7084;
            }

            if ($demo = question_answering_api) {
                set $demo_port 6084;
            }

            if ($demo = showcase_components_ui) {
                set $demo_port 7085;
            }

            if ($demo = showcase_components_api) {
                set $demo_port 6085;
            }

            if ($demo = preprocess_text_ui) {
                set $demo_port 7086;
            }

            if ($demo = preprocess_text_api) {
                set $demo_port 6086;
            }

            if ($demo = image_super_resolution_ui) {
                set $demo_port 7087;
            }

            if ($demo = image_super_resolution_api) {
                set $demo_port 6087;
            }

            if ($demo = detect_language_ui) {
                set $demo_port 7088;
            }

            if ($demo = detect_language_api) {
                set $demo_port 6088;
            }

            if ($demo = generate_text_ui) {
                set $demo_port 7089;
            }

            if ($demo = generate_text_api) {
                set $demo_port 6089;
            }

            if ($demo = named_entity_recognition_ui) {
                set $demo_port 7090;
            }

            if ($demo = named_entity_recognition_api) {
                set $demo_port 6090;
            }

            if ($remaining_part !~ ^/(.*)$) {
                # add slash to remaining part if it wasn't already added
                # required since base path always starts with slash
                set $remaining_part /$remaining_part;
            }

            # proxy_buffering off;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass_request_headers on;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_store off;

            proxy_pass http://127.0.0.1:$demo_port$remaining_part$is_args$args;
            gzip on;
            gzip_proxied any;
            gzip_types *;
        }
    }
}
