ovdashboard/api/ovdashboard_api/routers/misc.py

45 lines
906 B
Python

"""
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_host, 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")