misc router

This commit is contained in:
Jörn-Michael Miehe 2022-09-09 02:21:52 +00:00
parent 1f2b5a9607
commit a8c5180027
3 changed files with 51 additions and 1 deletions

View file

@ -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",

View file

@ -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")

View file

@ -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"