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

62 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-09-09 02:21:52 +00:00
"""
Router "misc" provides:
- getting the project version
- getting the device IP
"""
2023-10-26 16:21:07 +00:00
import importlib.metadata
import logging
2022-09-09 02:21:52 +00:00
from socket import AF_INET, SOCK_DGRAM, socket
2022-09-15 22:32:11 +00:00
from fastapi import APIRouter, Depends
2022-09-09 02:21:52 +00:00
2023-10-26 16:26:51 +00:00
from ...core.config import Config, LogoUIConfig, ServerUIConfig
2023-10-20 08:43:15 +00:00
from ...core.settings import SETTINGS
2023-10-26 16:26:51 +00:00
from ._common import get_config
2022-09-09 02:21:52 +00:00
2023-10-26 16:21:07 +00:00
_logger = logging.getLogger(__name__)
2022-09-09 02:21:52 +00:00
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")
2023-10-26 21:17:38 +00:00
async def get_lan_ip() -> str:
2022-09-09 02:21:52 +00:00
with socket(
family=AF_INET,
type=SOCK_DGRAM,
) as s:
try:
2023-10-26 22:40:04 +00:00
s.settimeout(0)
2022-09-09 02:41:15 +00:00
s.connect((SETTINGS.ping_host, SETTINGS.ping_port))
2022-09-09 02:21:52 +00:00
IP = s.getsockname()[0]
except Exception:
IP = "127.0.0.1"
return IP
@router.get("/version")
2023-10-26 21:17:38 +00:00
async def get_server_api_version() -> str:
2023-10-26 22:40:04 +00:00
return importlib.metadata.version("ovdashboard_api")
2022-09-15 22:32:11 +00:00
2023-10-24 17:39:43 +00:00
@router.get("/config/server")
2022-09-15 22:32:11 +00:00
async def get_server_ui_config(
2023-10-20 08:43:15 +00:00
cfg: Config = Depends(get_config),
2022-09-15 22:32:11 +00:00
) -> ServerUIConfig:
return cfg.server
2023-10-24 17:39:43 +00:00
@router.get("/config/logo")
2022-09-15 22:32:11 +00:00
async def get_logo_ui_config(
2023-10-20 08:43:15 +00:00
cfg: Config = Depends(get_config),
2022-09-15 22:32:11 +00:00
) -> LogoUIConfig:
return cfg.logo