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:
parent
9f3de33337
commit
0d1020a3d5
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
@ -4,7 +4,8 @@ authors = [
|
|||
{ name = "Eli Ribble", email = "eli@theribbles.org"}
|
||||
]
|
||||
dependencies = [
|
||||
"zeroconf"
|
||||
"aiohttp",
|
||||
"zeroconf",
|
||||
]
|
||||
dynamic = ["version", "description"]
|
||||
|
||||
|
|
Loading…
Reference in New Issue