location ~ ^/api/vnc/(.*) {
    proxy_pass http://lico_18083/$1;
}

location /websockify {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_read_timeout 60s;     # VNC connection timeout
    proxy_buffering off;
    proxy_pass http://lico_18082;
}

location /novnc {
    index vnc_auto.html;
    error_page 403 405 =404 /40x.html;
    alias /usr/share/novnc/;
}

location ~ ^/api/sessions/current/(async) {
    proxy_pass http://lico_18081/$1;
}

location ~ ^/api/nodes/(.*)/shell/sessions/ {
    auth_request /auth/user;
    proxy_pass http://lico_18081/$1/shell/sessions;
}

location ~ ^/api/nodes/(.*)/console/sessions/ {
    auth_request /auth/operator;
    proxy_pass http://lico_18081/$1/console/sessions;
}

location ~ /auth/(user|operator|admin) {
    internal;
    proxy_pass http://lico_18080/api/auth?role=$1;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
    client_max_body_size 5000m;
}

location ~ ^/tensorboard/(?<tens>[-\w]+)/(?<tb_uuid>le-[0-9a-z\-]+)/ {
    mirror /mirror/tensorboard;
    mirror_request_body off;
    set $origin_path $request_uri;
    perl '
            use MIME::Base64;
            sub {
                my $r = shift;
                my $tens_url = MIME::Base64::decode_base64url($r->variable("tens"));
                $r->internal_redirect("/tensorboard-open/$tens_url/");
            }
        ';
}
location ~ ^/tensorboard-open/([:\.\d]+)/ {
    proxy_pass http://$1$origin_path;
}

location /mirror/tensorboard {
    internal;
    proxy_pass http://lico_18080/api/tensorboard/$tb_uuid/;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
    proxy_method POST;
}

location /api/file/ {
    client_max_body_size 5000m;
    proxy_pass http://lico_18085;
}

location ~ ^/dev/jupyter/(?<jup>[-\w]+)/ {
    set $origin_path $request_uri;
    perl '
            use MIME::Base64;
            sub {
                my $r = shift;
                my $jup = MIME::Base64::decode_base64url($r->variable("jup"));
                $r->internal_redirect("/dev/jupyter-open/$jup/");
            }
        ';
}

location ~ ^/dev/jupyter-open/([:\.\d]+)/ {
    internal;
    proxy_pass http://$1$origin_path;
    proxy_set_header Host $host;

    error_page  502 =203 /50x.html;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

location /api {
    proxy_pass http://lico_18080;
    client_max_body_size 5000m;
}

location /openapi {
    proxy_pass http://lico_18080;
}

location /download {
    more_set_headers "Content-Type: application/octet-stream";
    proxy_pass http://lico_18080;
}

location ~ ^/docs/(.*) {
    default_type text/plain;
    charset utf8;
    alias /usr/share/lico-docs/$1;
}

location /config {
    default_type application/json;
    alias /etc/lico/portal.conf;
}

location / {
    error_page 403 405 =404 /40x.html;
    root /usr/share/lico-portal;
}