Add aiohttp web server.

We switch to having the server kick off the mDNS discovery process
because aiohttp recommends it and it fits well enough that I don't mind.
This commit is contained in:
Eli Ribble 2023-05-12 14:47:42 -07:00
parent 9f3de33337
commit 0d1020a3d5
4 changed files with 20 additions and 15 deletions

View File

@ -23,14 +23,14 @@ def get_ip() -> str:
LOGGER.info("IP address seems to be %s", ip)
return ip
async def handle():
async def handle(*args):
"Handle requests for discovery"
ip = get_ip()
info = AsyncServiceInfo(
"_http._tcp.local.",
"pnpdevice._http._tcp.local.",
addresses=[socket.inet_aton("127.0.0.1")],
port=13344,
port=80,
server=ip,
)
aiozc = AsyncZeroconf(ip_version=IPVersion.V4Only)

View File

@ -3,15 +3,10 @@ import asyncio
import logging
import pnpdevice.discovery
import pnpdevice.server
LOGGER = logging.getLogger(__name__)
async def run():
asyncio.ensure_future(pnpdevice.discovery.handle())
while True:
await asyncio.sleep(1)
LOGGER.info("Tick.")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose logging")
@ -19,9 +14,4 @@ def main():
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run())
except KeyboardInterrupt:
LOGGER.info("Shutting down")
pnpdevice.server.run()

14
pnpdevice/server.py Normal file
View File

@ -0,0 +1,14 @@
from aiohttp import web
import pnpdevice.discovery
async def hello(request):
return web.Response(text="Hello, world")
def run():
"Run the embedded web server"
app = web.Application()
app.on_startup.append(pnpdevice.discovery.handle)
app.add_routes([web.get("/", hello)])
web.run_app(app)

View File

@ -4,7 +4,8 @@ authors = [
{ name = "Eli Ribble", email = "eli@theribbles.org"}
]
dependencies = [
"zeroconf"
"aiohttp",
"zeroconf",
]
dynamic = ["version", "description"]