64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import uuid
|
|
|
|
import trio
|
|
|
|
import capport.comm.hub
|
|
import capport.comm.message
|
|
import capport.database
|
|
import capport.utils.cli
|
|
import capport.utils.ipneigh
|
|
from capport.utils.sd_notify import open_sdnotify
|
|
|
|
from .app import app
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ApiHubApp(capport.comm.hub.HubApplication):
|
|
async def mac_states_changed(
|
|
self,
|
|
*,
|
|
from_peer_id: uuid.UUID,
|
|
pending_updates: capport.database.PendingUpdates,
|
|
) -> None:
|
|
# TODO: support websocket notification updates to clients?
|
|
pass
|
|
|
|
|
|
async def _run_hub(*, task_status=trio.TASK_STATUS_IGNORED) -> None:
|
|
try:
|
|
async with capport.utils.ipneigh.connect() as mync:
|
|
app.my_nc = mync
|
|
_logger.info("Running hub for API")
|
|
myapp = ApiHubApp()
|
|
myhub = capport.comm.hub.Hub(config=app.my_config, app=myapp, is_controller=False)
|
|
app.my_hub = myhub
|
|
await myhub.run(task_status=task_status)
|
|
finally:
|
|
app.my_hub = None
|
|
app.my_nc = None
|
|
_logger.info("Done running hub for API")
|
|
await app.shutdown()
|
|
|
|
|
|
async def _setup(*, task_status=trio.TASK_STATUS_IGNORED):
|
|
async with open_sdnotify() as sn:
|
|
await sn.send("STATUS=Starting hub")
|
|
async with trio.open_nursery() as nursery:
|
|
await nursery.start(_run_hub)
|
|
await sn.send("READY=1", "STATUS=Ready for client requests")
|
|
task_status.started()
|
|
# continue running hub and systemd watchdog handler
|
|
|
|
|
|
@app.before_serving
|
|
async def init():
|
|
app.debug = app.my_config.debug
|
|
app.secret_key = app.my_config.cookie_secret
|
|
capport.utils.cli.init_logger(app.my_config)
|
|
await app.nursery.start(_setup)
|