|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +from pathlib import Path, PurePath |
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +import uvicorn |
| 8 | + |
| 9 | + |
| 10 | +def set_app_module() -> str: |
| 11 | + if Path("/app/app/main.py").is_file(): |
| 12 | + default_module_name = "app.main" |
| 13 | + elif Path("/app/main.py").is_file(): |
| 14 | + default_module_name = "main" |
| 15 | + module_name = os.getenv("MODULE_NAME", default_module_name) |
| 16 | + variable_name = os.getenv("VARIABLE_NAME", "app") |
| 17 | + app_module = os.getenv("APP_MODULE", f"{module_name}:{variable_name}") |
| 18 | + os.environ["APP_MODULE"] = app_module |
| 19 | + return app_module |
| 20 | + |
| 21 | + |
| 22 | +def set_gunicorn_conf() -> Tuple[str, str]: |
| 23 | + if Path("/app/gunicorn_conf.py").is_file(): |
| 24 | + default_gunicorn_conf = "/app/gunicorn_conf.py" |
| 25 | + elif Path("/app/app/gunicorn_conf.py").is_file(): |
| 26 | + default_gunicorn_conf = "/app/app/gunicorn_conf.py" |
| 27 | + elif Path("/gunicorn_conf.py").is_file(): |
| 28 | + default_gunicorn_conf = "/gunicorn_conf.py" |
| 29 | + gunicorn_conf = os.getenv("GUNICORN_CONF", default_gunicorn_conf) |
| 30 | + os.environ["GUNICORN_CONF"] = gunicorn_conf |
| 31 | + worker_class = os.getenv("WORKER_CLASS", "uvicorn.workers.UvicornWorker") |
| 32 | + os.environ["WORKER_CLASS"] = worker_class |
| 33 | + return gunicorn_conf, worker_class |
| 34 | + |
| 35 | + |
| 36 | +def run_pre_start_script( |
| 37 | + pre_start_path: str = os.getenv("PRE_START_PATH", "/app/inboard/prestart.py") |
| 38 | +) -> None: |
| 39 | + try: |
| 40 | + print(f"Checking for pre-start script in {pre_start_path}.") |
| 41 | + if Path(pre_start_path).is_file(): |
| 42 | + print(f"Running pre-start script script in {pre_start_path}.") |
| 43 | + process = "python" if PurePath(pre_start_path).suffix == ".py" else "sh" |
| 44 | + subprocess.run(process, pre_start_path) |
| 45 | + else: |
| 46 | + print("No pre-start script found.") |
| 47 | + except Exception as e: |
| 48 | + print(f"Error when running pre-start script: {e}") |
| 49 | + |
| 50 | + |
| 51 | +def start_server( |
| 52 | + app_module: str = str(os.getenv("APP_MODULE", "/app/inboard.base.main:app")), |
| 53 | + gunicorn_conf: str = str( |
| 54 | + os.getenv("GUNICORN_CONF", "/app/inboard/gunicorn_conf.py") |
| 55 | + ), |
| 56 | + worker_class: str = str(os.getenv("WORKER_CLASS", "uvicorn.workers.UvicornWorker")), |
| 57 | + with_reload: bool = bool(os.getenv("WITH_RELOAD", False)), |
| 58 | +) -> None: |
| 59 | + if with_reload: |
| 60 | + uvicorn.run( |
| 61 | + app_module, |
| 62 | + host=os.getenv("HOST", "0.0.0.0"), |
| 63 | + port=int(os.getenv("PORT", "80")), |
| 64 | + log_level=os.getenv("LOG_LEVEL", "info"), |
| 65 | + reload=True, |
| 66 | + ) |
| 67 | + else: |
| 68 | + subprocess.run("gunicorn", "-k", worker_class, "-c", gunicorn_conf, app_module) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + set_app_module() |
| 73 | + set_gunicorn_conf() |
| 74 | + run_pre_start_script() |
| 75 | + start_server() |
0 commit comments