diff --git a/api/ovdashboard_api/routers/__init__.py b/api/ovdashboard_api/routers/__init__.py index f09888c..ddba1a0 100644 --- a/api/ovdashboard_api/routers/__init__.py +++ b/api/ovdashboard_api/routers/__init__.py @@ -7,7 +7,7 @@ This file: Main API router definition. from fastapi import APIRouter from ..settings import SETTINGS -from . import aggregate, calendar, image, text, ticker +from . import aggregate, calendar, image, misc, text, ticker main_router = APIRouter(prefix=f"/{SETTINGS.api_v1_prefix}") main_router.include_router(text.router) @@ -15,6 +15,7 @@ main_router.include_router(ticker.router) main_router.include_router(image.router) main_router.include_router(calendar.router) main_router.include_router(aggregate.router) +main_router.include_router(misc.router) __all__ = [ "main_router", diff --git a/api/ovdashboard_api/routers/misc.py b/api/ovdashboard_api/routers/misc.py new file mode 100644 index 0000000..d5b864e --- /dev/null +++ b/api/ovdashboard_api/routers/misc.py @@ -0,0 +1,45 @@ +""" +Router "misc" provides: + +- getting the project version +- getting the device IP +""" + +from importlib.metadata import version +from logging import getLogger +from socket import AF_INET, SOCK_DGRAM, socket + +from fastapi import APIRouter + +from ..settings import SETTINGS + +_logger = getLogger(__name__) + +router = APIRouter(prefix="/misc", tags=["misc"]) + + +@router.on_event("startup") +async def start_router() -> None: + _logger.debug(f"{router.prefix} router starting.") + + +@router.get("/lanip") +async def get_ip() -> str: + with socket( + family=AF_INET, + type=SOCK_DGRAM, + ) as s: + s.settimeout(0) + try: + s.connect((SETTINGS.ping_address, SETTINGS.ping_port)) + IP = s.getsockname()[0] + + except Exception: + IP = "127.0.0.1" + + return IP + + +@router.get("/version") +async def get_version() -> str: + return version("ovdashboard-api") diff --git a/api/ovdashboard_api/settings.py b/api/ovdashboard_api/settings.py index c41967e..9caf888 100644 --- a/api/ovdashboard_api/settings.py +++ b/api/ovdashboard_api/settings.py @@ -52,6 +52,10 @@ class Settings(BaseSettings): cache_seconds: int = 30 cache_size: int = 30 + # doesn't even have to be reachable + ping_address: str = "10.0.0.0" + ping_port: int = 1 + api_v1_prefix: str = "api/v1" openapi_url: str = "/openapi.json" docs_url: Optional[str] = None if production_mode else "/docs"