Skip to content
Python

CI en .github, script del token

neuregexd5322cde3359
diff --git a/.env.example b/.env.example
index 4064fb2..bf63bde 100644
--- a/.env.example
+++ b/.env.example
@@ -1,7 +1,7 @@
# Gateway in front of comfy-api-proxy (the Modal URL printed by `modal deploy infra/modal_app.py`,
# or http://127.0.0.1:8000 when running the gateway locally).
-COMFY_BASE_URL=https://YOUR-WORKSPACE--freefield-backend-serve.modal.run
-# Same value as the FREEFIELD_TOKEN in the Modal secret "freefield".
+COMFY_BASE_URL=https://YOUR-WORKSPACE--freefield-serve.modal.run
+# Written by `python tools/setup_token.py`, which stores the same value in the Modal secret "freefield".
FREEFIELD_TOKEN=
# Let the web UI and the CLI run draft apps too (their workflows have not been run on a GPU yet).
# FREEFIELD_DRAFTS=1
diff --git a/ci/validate.yml b/.github/workflows/validate.yml
similarity index 100%
rename from ci/validate.yml
rename to .github/workflows/validate.yml
diff --git a/README.md b/README.md
index 7d7ae85..a4987a6 100644
--- a/README.md
+++ b/README.md
@@ -65,7 +65,7 @@ You need Node 22.6+, Python 3.10+ and a [Modal](https://modal.com) account.
python tools/doctor.py
# 1. secrets (once)
-modal secret create freefield FREEFIELD_TOKEN=$(python -c "import secrets;print(secrets.token_urlsafe(32))")
+python tools/setup_token.py # gateway token: Modal secret "freefield" and .env get the same value
modal secret create huggingface HF_TOKEN=hf_... # accept the LTX-2.5 and FLUX 3 Klein licenses on the Hub first
# 2. models into the "freefield-models" volume, per pack (see packs/index.json)
@@ -77,11 +77,13 @@ modal deploy infra/modal_app.py # FREEFIELD_GPU=A10G to try th
# 4. web app
npm install
-cp .env.example .env # COMFY_BASE_URL = the URL from step 3, FREEFIELD_TOKEN = same token
+# in .env (made by step 1): COMFY_BASE_URL = the URL printed by step 3
npm run serve # http://127.0.0.1:3000
npm run app -- z-image --set prompt="a red bicycle against a white wall" # or from the terminal
```
+On Windows PowerShell, environment variables go before the command as `$env:FREEFIELD_GPU = "A10G"; modal deploy ...`.
+
Any ComfyUI works as the backend: run `comfy-api-proxy` next to it and `python infra/gateway.py` in front, and point
`COMFY_BASE_URL` at the gateway. Pointing it straight at a `comfy-api-proxy` (or another Comfy API v2 endpoint) works
too; only the unload-between-apps step is skipped. Draft apps stay locked in the UI and the CLI until you set
diff --git a/infra/modal_app.py b/infra/modal_app.py
index 8d86cec..5c4a013 100644
--- a/infra/modal_app.py
+++ b/infra/modal_app.py
@@ -1,7 +1,7 @@
"""Freefield backend on Modal: ComfyUI + comfy-api-proxy behind a token gateway.
One-time setup (in your terminal, from the repo root):
- modal secret create freefield FREEFIELD_TOKEN=$(python -c "import secrets;print(secrets.token_urlsafe(32))")
+ python tools/setup_token.py # gateway token, same value in the secret and in .env
modal secret create huggingface HF_TOKEN=hf_... # accept the LTX-2.5 and FLUX 3 Klein licenses on the Hub first
python tools/catalog.py packs
diff --git a/server/runner.ts b/server/runner.ts
index 6a55bb7..c950f8f 100644
--- a/server/runner.ts
+++ b/server/runner.ts
@@ -105,7 +105,7 @@ export function applyInputs(
}
export type RunnerOptions = {
- /** Gateway in front of comfy-api-proxy, e.g. https://you--freefield-backend.modal.run */
+ /** Gateway in front of comfy-api-proxy, e.g. https://you--freefield-serve.modal.run */
baseUrl?: string;
/** Token the gateway expects; sent by the SDK as a bearer token. */
token?: string;
diff --git a/tools/doctor.py b/tools/doctor.py
index 68c6e2e..0e047b4 100644
--- a/tools/doctor.py
+++ b/tools/doctor.py
@@ -69,7 +69,7 @@ def check_modal() -> None:
except json.JSONDecodeError:
names = set(re.findall(r"\b(freefield|huggingface)\b", out))
fixes = {
- "freefield": 'modal secret create freefield FREEFIELD_TOKEN=$(python -c "import secrets;print(secrets.token_urlsafe(32))")',
+ "freefield": "python tools/setup_token.py (same token in the secret and in .env)",
"huggingface": "modal secret create huggingface HF_TOKEN=hf_...",
}
for name, fix in fixes.items():
@@ -140,7 +140,8 @@ def check_hf(packs: list[str], with_lab: bool) -> None:
def check_env() -> None:
env = ROOT / ".env"
if not env.exists():
- report(BAD, ".env missing (the web app reads COMFY_BASE_URL and FREEFIELD_TOKEN from it)", "cp .env.example .env")
+ report(BAD, ".env missing (the web app reads COMFY_BASE_URL and FREEFIELD_TOKEN from it)",
+ "python tools/setup_token.py (creates .env with the same token as the Modal secret)")
return
values = dict(re.findall(r"^\s*([A-Z_]+)\s*=\s*(.*?)\s*$", env.read_text(encoding="utf-8"), flags=re.M))
base, token = values.get("COMFY_BASE_URL", ""), values.get("FREEFIELD_TOKEN", "")
@@ -148,7 +149,7 @@ def check_env() -> None:
report(BAD, "COMFY_BASE_URL not set in .env", "paste the URL printed by: modal deploy infra/modal_app.py")
return
if not token:
- report(BAD, "FREEFIELD_TOKEN empty in .env", "use the same value as the Modal secret \"freefield\"")
+ report(BAD, "FREEFIELD_TOKEN empty in .env", "python tools/setup_token.py (writes one token to .env and the Modal secret)")
try:
with urllib.request.urlopen(f"{base.rstrip('/')}/health", timeout=20) as r:
report(OK, f"backend answers at {base} (HTTP {r.status})")
@@ -162,7 +163,8 @@ def check_env() -> None:
with urllib.request.urlopen(req, timeout=20) as r:
report(OK, f"gateway accepts the token (HTTP {r.status})")
except urllib.error.HTTPError as e:
- report(BAD, f"gateway refused the token (HTTP {e.code})", "FREEFIELD_TOKEN must match the Modal secret \"freefield\"")
+ report(BAD, f"gateway refused the token (HTTP {e.code})",
+ "python tools/setup_token.py, then modal deploy infra/modal_app.py so the backend gets the new token")
except Exception as e:
report(WARN, f"could not reach /freefield/status ({type(e).__name__})")
diff --git a/tools/setup_token.py b/tools/setup_token.py
new file mode 100644
index 0000000..87325b8
--- /dev/null
+++ b/tools/setup_token.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python3
+"""One gateway token, written to the two places that need it.
+
+ python tools/setup_token.py # new token → Modal secret "freefield" and .env
+ python tools/setup_token.py --env-only # only .env (when the backend is not on Modal)
+
+The backend's gateway reads FREEFIELD_TOKEN from the Modal secret; the web app and the CLI read
+the same value from .env. This creates .env from .env.example when it is missing, stores a fresh
+random token in the secret (overwriting it), and only then writes it to .env, so the two never
+disagree. The token is not printed; .env is ignored by git. Run it again to rotate the token,
+then redeploy the backend so new containers pick it up.
+"""
+from __future__ import annotations
+
+import argparse
+import re
+import secrets
+import shutil
+import subprocess
+import sys
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parent.parent
+
+
+def main(argv: list[str] | None = None) -> int:
+ ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+ ap.add_argument("--env-only", action="store_true", help="write .env without touching the Modal secret")
+ a = ap.parse_args(argv)
+
+ env = ROOT / ".env"
+ if not env.exists():
+ shutil.copy(ROOT / ".env.example", env)
+ print("created .env from .env.example")
+ token = secrets.token_urlsafe(32)
+
+ if not a.env_only:
+ cmd = [sys.executable, "-m", "modal", "secret", "create", "freefield", f"FREEFIELD_TOKEN={token}", "--force"]
+ try:
+ r = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
+ except (OSError, subprocess.TimeoutExpired) as e:
+ print(f"could not run the Modal CLI ({e}); nothing changed", file=sys.stderr)
+ return 1
+ if r.returncode != 0:
+ print((r.stderr or r.stdout).strip(), file=sys.stderr)
+ print('the Modal secret "freefield" was not updated, so .env was left as it was', file=sys.stderr)
+ return 1
+ print('stored the token in the Modal secret "freefield"')
+
+ text = env.read_text(encoding="utf-8")
+ line = f"FREEFIELD_TOKEN={token}"
+ if re.search(r"^FREEFIELD_TOKEN=.*$", text, flags=re.M):
+ text = re.sub(r"^FREEFIELD_TOKEN=.*$", line, text, count=1, flags=re.M)
+ else:
+ text = text.rstrip("\n") + "\n" + line + "\n"
+ with open(env, "w", encoding="utf-8", newline="\n") as f:
+ f.write(text)
+ print("wrote the same token to .env")
+ if "YOUR-WORKSPACE" in text or not re.search(r"^COMFY_BASE_URL=\S+", text, flags=re.M):
+ print("next: put the backend URL from `modal deploy infra/modal_app.py` in .env as COMFY_BASE_URL")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())