File size: 1,509 Bytes
e5e36db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
"""
server/app.py
=============
Entry point for multi-mode deployment (uv run server / python -m server.app).

Loads the FastAPI app from the root-level server.py via importlib so the
`server` package name does not shadow the root module, and exposes a
main() function for the [project.scripts] console script entry point.
"""

import importlib.util
import os
import sys

import uvicorn

# Load root server.py by absolute path to avoid circular package import
_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_spec = importlib.util.spec_from_file_location(
    "_server_root",
    os.path.join(_root, "server.py"),
)
_mod = importlib.util.module_from_spec(_spec)
# Ensure the root is on sys.path so root-level imports inside server.py work
if _root not in sys.path:
    sys.path.insert(0, _root)
_spec.loader.exec_module(_mod)

app = _mod.app  # re-export for ASGI runners (e.g. uvicorn server.app:app)


def main(host: str = "0.0.0.0", port: int = 8000):
    """Console script entry point: uv run server / python -m server.app."""
    host = os.getenv("HOST", host)
    port = int(os.getenv("PORT", port))
    log_level = os.getenv("LOG_LEVEL", "info")
    uvicorn.run(app, host=host, port=port, log_level=log_level)


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--host", default="0.0.0.0")
    parser.add_argument("--port", type=int, default=8000)
    args = parser.parse_args()
    main(host=args.host, port=args.port)