| 1234567891011121314151617181920212223242526272829303132333435 |
- #!/usr/bin/env python3
- import os
- import sys
- import subprocess
- def main():
- """
- 启动 FastAPI 应用,根据环境选择不同参数:
- - DEV: 热重载,单进程
- - PROD: 多进程,高性能 uvloop + httptools
- """
- env = os.getenv("ENV", "DEV").upper() # 默认开发环境
- host = "0.0.0.0"
- port = "8888"
- app_module = "app.main:app"
- base_cmd = ["uvicorn", app_module, "--host", host, "--port", port]
- if env == "DEV":
- print("启动开发环境(热重载)...")
- base_cmd += ["--reload", "--workers", "1"]
- elif env == "PROD":
- print("启动生产环境(多进程 + 高性能)...")
- base_cmd += ["--workers", str(os.cpu_count()), "--loop", "uvloop", "--http", "httptools"]
- else:
- print(f"未知环境 {env},使用默认开发配置")
- base_cmd += ["--reload", "--workers", "1"]
- print("执行命令:", " ".join(base_cmd))
- print(f"Swagger UI: http://{host}:{port}/docs")
- print(f"ReDoc UI: http://{host}:{port}/redoc")
- subprocess.run(base_cmd)
- if __name__ == "__main__":
- main()
|