upstream backend {
    server panda-wiki-api:8000;
}

server {
    charset utf-8;
    listen 8080 ssl http2;

    ssl_certificate /etc/nginx/ssl/panda-wiki.crt;
    ssl_certificate_key /etc/nginx/ssl/panda-wiki.key;

    location /503 {
        return 503;
    }

    location  ~ ^/(share/v1/chat/message|api/v1/creation/text)$ {
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;

        proxy_read_timeout 24h;
        proxy_send_timeout 24h;

        # Forward client information
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;

        proxy_pass http://backend;
    }

    location = /api/v1/file/upload {
        proxy_pass http://backend;

        client_max_body_size 1000m;

        # Forward client information
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
    }

    location ~ ^/api {
        proxy_pass http://backend;

        client_max_body_size 1000m;

        # Forward client information
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
    }

    location ~ ^/share {
        proxy_pass http://backend;

        # Forward client information
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
    }

    location ~ ^/static-file/ {
        proxy_pass http://panda-wiki-minio:9000;

        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        send_timeout 300;

        client_max_body_size 1000m;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;

        proxy_cache off;
        proxy_buffering off;
    }

    location / {
        root /opt/frontend/dist;
        index index.html index.htm;
        try_files $uri $uri/ $uri.html /index.html;
        if ($request_filename ~* .*\.(htm|html)$) {
            add_header Cache-Control "no-cache";
        }
    }
}