ovdashboard/api/ovdashboard_api/routers/v1/misc.py

61 lines
1.3 KiB
Python

"""
Router "misc" provides:
- getting the project version
- getting the device IP
"""
import importlib.metadata
import logging
from socket import AF_INET, SOCK_DGRAM, socket
from fastapi import APIRouter, Depends
from ...core.config import Config, LogoUIConfig, ServerUIConfig
from ...core.settings import SETTINGS
from ._common import get_config
_logger = logging.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_lan_ip() -> str:
with socket(
family=AF_INET,
type=SOCK_DGRAM,
) as s:
try:
s.settimeout(0)
s.connect((SETTINGS.ping_host, SETTINGS.ping_port))
IP = s.getsockname()[0]
except Exception:
IP = "127.0.0.1"
return IP
@router.get("/version")
async def get_server_api_version() -> str:
return importlib.metadata.version("ovdashboard_api")
@router.get("/config/server")
async def get_server_ui_config(
cfg: Config = Depends(get_config),
) -> ServerUIConfig:
return cfg.server
@router.get("/config/logo")
async def get_logo_ui_config(
cfg: Config = Depends(get_config),
) -> LogoUIConfig:
return cfg.logo