# ===========================================================================
# URL Shortener — nginx location blocks
# Drop these into an existing server { } block.
#
# Assumptions:
#   - The Python backend is reachable at  http://urlshort:8080
#     (docker service name; swap for 127.0.0.1:8080 or upstream as needed)
#   - Static frontend files live at       /app/static/
#     (adjust the alias paths to match your deployment layout)
#
# Path mapping (base_url = http://<host>/s):
#   /s/             -> frontend index.html
#   /s/static/...   -> frontend CSS / JS
#   /s/api/...      -> Python backend (proxy)
#   /s/<code>       -> Python backend (redirect, proxy)
# ===========================================================================

# --- Redirect bare /s to /s/ for clean UX ---
location = /s {
    return 301 /s/;
}

# --- Serve frontend index.html at /s/ ---
location = /s/ {
    alias /root/repo/urlshortener/static/;
}

# --- Serve frontend static assets (CSS, JS, etc.) ---
# ^~ ensures this takes priority over the general /s/ proxy below.
location ^~ /s/static/ {
    alias /root/repo/urlshortener/static/;
    expires 7d;
    add_header Cache-Control "public, immutable";
}

# --- Proxy everything else under /s/ to the Python backend ---
# Covers: /s/api/shorten, /s/api/urls, /s/api/lookup, /s/<code> redirects
location /s/ {
    proxy_pass         http://127.0.0.1:18082;
    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;
}

