# Copyright (C) 2024 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

server {
    gzip on;
    gzip_types      text/plain text/x-log;
    gzip_proxied    no-cache no-store private expired auth;
    gzip_min_length 1000;

    listen 2080 default_server;
    listen [::]:2080 default_server;
    
    # SSL configuration
    #
    listen 2443 ssl default_server;
    listen [::]:2443 ssl default_server;

    ssl_certificate /etc/cuttlefish-orchestration/ssl/cert/cert.pem;
    ssl_certificate_key /etc/cuttlefish-orchestration/ssl/cert/key.pem;

   location = /hostlogs/cuttlefish-operator.service {
       alias /run/cuttlefish/operator.log;
       default_type "text/plain";
   }
   location = /hostlogs/cuttlefish-host_orchestrator.service {
       alias /run/cuttlefish/host_orchestrator.log;
       default_type "text/plain";
   }
   location /hostlogs/kernel {
       rewrite /hostlogs/kernel /entries?_TRANSPORT=kernel break;
       proxy_pass http://0.0.0.0:19531;
   }

    # journal-gatewayd
    # [DEPRECATED]
    # TODO(b/425706660): Remove this rule after relevant clients have migrated to /hostlogs.
    location /_journal/entries {
        set $pass 0;
        if ($arg__systemd_unit ~ '(cuttlefish-operator|cuttlefish-host_orchestrator).service') { set $pass 1; }
        if ($arg__transport = 'kernel') { set $pass 1; }
        if ($arg_container_name != '') { set $pass 1; }
        if ($pass = 0) {
            return 404;
        }
        proxy_pass http://0.0.0.0:19531/entries;
    }

    # Host Orchestrator
    location / {
        # ADB websocket backend
        location ~* ^/.*/adb$ {
            proxy_pass http://127.0.0.1:2081;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
        }

        proxy_pass http://127.0.0.1:2081;

        # This is required for uploading huge artifacts.
        client_max_body_size 20G;
    }
}
