-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_product_api.py
More file actions
59 lines (44 loc) · 1.64 KB
/
Copy pathmain_product_api.py
File metadata and controls
59 lines (44 loc) · 1.64 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import annotations
import os
from dataclasses import is_dataclass, replace
from src.config import get_product_api_settings
from src.product.api import build_product_api_server
def _product_api_bind_config() -> tuple[str, int]:
host = (
os.environ.get("AI_DECISION_STUDIO_PRODUCT_API_HOST")
or os.environ.get("PRODUCT_API_HOST")
or "127.0.0.1"
)
port_raw = (
os.environ.get("AI_DECISION_STUDIO_PRODUCT_API_PORT")
or os.environ.get("PRODUCT_API_PORT")
or "8011"
)
try:
port = int(port_raw)
except ValueError:
port = 8011
return host, port
def _settings_with_product_api_bind(settings, host: str, port: int):
if is_dataclass(settings):
return replace(settings, server_name=host, server_port=port)
if hasattr(settings, "model_copy"):
return settings.model_copy(update={"server_name": host, "server_port": port})
try:
settings.server_name = host
settings.server_port = port
return settings
except Exception:
values = dict(getattr(settings, "__dict__", {}))
values["server_name"] = host
values["server_port"] = port
return settings.__class__(**values)
def main() -> None:
settings = get_product_api_settings()
bind_host, bind_port = _product_api_bind_config()
settings = _settings_with_product_api_bind(settings, bind_host, bind_port)
server = build_product_api_server(settings=settings)
print(f"Axiovance Product API listening on http://{settings.server_name}:{settings.server_port}")
server.serve_forever()
if __name__ == "__main__":
main()