-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrun.py
More file actions
42 lines (31 loc) · 1.06 KB
/
run.py
File metadata and controls
42 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
def main() -> None:
"""Launch Gunicorn the same way we do in the Dockerfile for local dev.
This avoids importing the FastAPI app in the parent process, so no DB
connections are opened before Gunicorn forks its workers (prevents SSL
errors when using Railway/PostgreSQL).
"""
host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", "8000")
reload = os.getenv("RELOAD", "false").lower() == "true"
# Optional: let caller override the number of Gunicorn workers
workers_env = os.getenv("WORKERS") # e.g. WORKERS=10
cmd = [
"gunicorn",
"app.main:app",
"-k",
"uvicorn.workers.UvicornWorker",
"--bind",
f"{host}:{port}",
"--log-level",
"info",
]
if reload:
cmd.append("--reload")
# Inject --workers flag if WORKERS env var is set
if workers_env and workers_env.isdigit():
cmd.extend(["--workers", workers_env])
# Replace the current process with Gunicorn.
os.execvp(cmd[0], cmd)
if __name__ == "__main__":
main()