generated from john/python-template
separated cli settings
This commit is contained in:
@@ -1,17 +1,24 @@
|
|||||||
import uvicorn
|
import uvicorn
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
from .config import LOGGING_CONFIG
|
from .app import create_app
|
||||||
from .config import get_settings
|
from .config import parse_cli_settings
|
||||||
|
|
||||||
|
|
||||||
|
def create_cli_app() -> FastAPI:
|
||||||
|
"""Create an app from CLI settings for Uvicorn's reload process."""
|
||||||
|
return create_app(settings=parse_cli_settings())
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
settings = get_settings()
|
settings = parse_cli_settings()
|
||||||
|
application = "transcription.__main__:create_cli_app" if settings.reload else create_app(settings=settings)
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
"transcription.app:create_app",
|
application,
|
||||||
factory=True,
|
factory=settings.reload,
|
||||||
host=settings.host,
|
host=settings.host,
|
||||||
port=settings.port,
|
port=settings.port,
|
||||||
log_level=LOGGING_CONFIG.get("root", {}).get("level", "info").lower(),
|
log_level=settings.log_level,
|
||||||
reload=settings.reload,
|
reload=settings.reload,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ are resolved by the provider adapters, not here.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging.config
|
import logging.config
|
||||||
|
from collections.abc import Sequence
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from functools import cache
|
from functools import cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -51,7 +52,6 @@ class Settings(BaseSettings):
|
|||||||
env_file=".env",
|
env_file=".env",
|
||||||
env_file_encoding="utf-8",
|
env_file_encoding="utf-8",
|
||||||
extra="ignore",
|
extra="ignore",
|
||||||
cli_parse_args=True,
|
|
||||||
cli_implicit_flags=True,
|
cli_implicit_flags=True,
|
||||||
cli_kebab_case=True,
|
cli_kebab_case=True,
|
||||||
)
|
)
|
||||||
@@ -99,8 +99,15 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
|
|
||||||
@cache
|
@cache
|
||||||
def get_settings(**kwargs) -> Settings:
|
def get_settings(**kwargs: Any) -> Settings:
|
||||||
return Settings(**kwargs)
|
"""Load cached settings without reading process CLI arguments."""
|
||||||
|
return Settings(_cli_parse_args=False, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_cli_settings(args: Sequence[str] | None = None) -> Settings:
|
||||||
|
"""Load settings with CLI arguments at the executable boundary."""
|
||||||
|
cli_args = True if args is None else list(args)
|
||||||
|
return Settings(_cli_parse_args=cli_args)
|
||||||
|
|
||||||
|
|
||||||
LOGGING_CONFIG: dict[str, Any] = {
|
LOGGING_CONFIG: dict[str, Any] = {
|
||||||
|
|||||||
Reference in New Issue
Block a user