#!/bin/sh # Substitute Hugging Face Spaces runtime env vars into the built index.html # before nginx starts. # # On `sdk: docker` Spaces, HF only exposes its OAuth + Space helper variables # server-side (as env vars), never client-side like it does for static/Gradio # Spaces. We bridge the gap by patching the placeholders we emit in index.html # with the real values here. set -eu INDEX_FILE="${INDEX_FILE:-/usr/share/nginx/html/index.html}" if [ ! -f "$INDEX_FILE" ]; then echo "[inject-hf-vars] $INDEX_FILE not found, skipping" >&2 exit 0 fi replace() { placeholder="$1" value="$2" escaped=$(printf '%s' "$value" | sed -e 's/[\\/&]/\\&/g') sed -i "s/${placeholder}/${escaped}/g" "$INDEX_FILE" } replace "__OAUTH_CLIENT_ID__" "${OAUTH_CLIENT_ID:-}" replace "__OAUTH_SCOPES__" "${OAUTH_SCOPES:-openid profile}" replace "__SPACE_HOST__" "${SPACE_HOST:-}" replace "__SPACE_ID__" "${SPACE_ID:-}" if [ -n "${OAUTH_CLIENT_ID:-}" ]; then echo "[inject-hf-vars] OAUTH_CLIENT_ID injected (${OAUTH_CLIENT_ID%${OAUTH_CLIENT_ID#????????}}…)" else echo "[inject-hf-vars] OAUTH_CLIENT_ID is empty (hf_oauth: true missing?)" >&2 fi