# CoderAI single-port reverse proxy (in-container).
# Fronts the main server and the bundled tool web UIs on one published port.
# nginx runs in the foreground under supervisord (daemon off).

# No `user` directive: when the container runs as root, the master stays root and
# spawns workers as nobody; when run with `--user UID`, nginx runs entirely as that
# UID. All writable state below lives under /tmp so non-root runs work unchanged.
worker_processes  auto;
daemon off;
pid /tmp/nginx.pid;
# crit only: nginx's startup/worker [notice] lines and per-request info would
# otherwise stream to the container console (supervisord pipes nginx stderr to
# /dev/fd/1), drowning out coderai's own output on attach / `docker logs`. Real
# failures (crit/alert/emerg) still surface.
error_log /dev/stderr crit;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    server_tokens off;
    # Off: per-request access lines would stream to the container console and
    # bury coderai's output on attach. coderai logs its own requests already.
    access_log off;

    # Writable temp paths under /tmp so the listed user (root or --user UID) can
    # always create them; the defaults under /var/lib/nginx are root-only.
    client_body_temp_path /tmp/nginx-client-body;
    proxy_temp_path       /tmp/nginx-proxy;
    fastcgi_temp_path     /tmp/nginx-fastcgi;
    uwsgi_temp_path       /tmp/nginx-uwsgi;
    scgi_temp_path        /tmp/nginx-scgi;

    # AI workloads: large uploads (images/audio/video) and long generations.
    client_max_body_size 4096m;
    proxy_read_timeout   3600s;
    proxy_send_timeout   3600s;
    proxy_connect_timeout 75s;

    # Shared proxy headers. CoderAI builds public URLs from these
    # (codai/api/urlutils.py); the tools honour X-Forwarded-Prefix for sub-paths.
    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;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host  $host;

    upstream coderai   { server 127.0.0.1:18776; }
    upstream editor    { server 127.0.0.1:8420;  }
    upstream videogen  { server 127.0.0.1:7790;  }
    upstream township  { server 127.0.0.1:7788;  }

    server {
        listen 8776 default_server;
        listen [::]:8776 default_server;
        server_name _;

        # --- Video editor: https://host:8776/editor/ -------------------------
        location /editor/ {
            proxy_pass http://editor/;            # trailing slash strips the prefix
            proxy_set_header X-Forwarded-Prefix /editor;
            proxy_request_buffering off;          # stream large uploads through
            proxy_buffering off;                  # SSE progress
        }

        # --- Videogen studio: https://host:8776/videogen/ -------------------
        location /videogen/ {
            proxy_pass http://videogen/;
            proxy_set_header X-Forwarded-Prefix /videogen;
            proxy_request_buffering off;
            proxy_buffering off;
        }

        # --- Township fighters: https://host:8776/township/ ----------------
        location /township/ {
            proxy_pass http://township/;
            proxy_set_header X-Forwarded-Prefix /township;
            proxy_request_buffering off;
            proxy_buffering off;
        }

        # --- CoderAI server + OpenAI API at the root ------------------------
        location / {
            proxy_pass http://coderai;
            proxy_buffering off;                  # SSE: chat stream + task progress
        }
    }
}
