|
| 1 | +import os |
| 2 | + |
| 3 | +from aiohttp import ClientSession, web |
| 4 | + |
| 5 | +from bridger.meshtastic import DeviceModel |
| 6 | + |
| 7 | +VERSION = os.getenv("VERSION", "development") |
| 8 | + |
| 9 | +app = web.Application() |
| 10 | +routes = web.RouteTableDef() |
| 11 | +device = None |
| 12 | +session = None |
| 13 | + |
| 14 | + |
| 15 | +async def on_startup(app): |
| 16 | + global device, session |
| 17 | + session = ClientSession() |
| 18 | + device = DeviceModel(session=session) |
| 19 | + |
| 20 | + |
| 21 | +async def on_cleanup(app): |
| 22 | + await session.close() |
| 23 | + |
| 24 | + |
| 25 | +@routes.get("/") |
| 26 | +async def index_view(request): |
| 27 | + # Get list of routes and their methods |
| 28 | + routes_list = [(route.method, route.resource.canonical) for route in app.router.routes()] |
| 29 | + # Create HTML response |
| 30 | + html_response = "<h1>Bridger API</h1><ul>" |
| 31 | + for method, path in routes_list: |
| 32 | + html_response += f"<li>{method} {path}</li>" |
| 33 | + html_response += "</ul>" |
| 34 | + html_response += f"<p>Version: {VERSION}</p>" |
| 35 | + |
| 36 | + return web.Response(text=html_response, content_type="text/html") |
| 37 | + |
| 38 | + |
| 39 | +@routes.get("/model/displaynames/{model_id}") |
| 40 | +async def get_displaynames(request): |
| 41 | + model_id = int(request.match_info["model_id"]) |
| 42 | + displaynames = await device.get_displaynames(model_id) |
| 43 | + return web.json_response(displaynames) |
| 44 | + |
| 45 | + |
| 46 | +@routes.get("/model/displaynames") |
| 47 | +async def get_displaynames_all(request): |
| 48 | + displaynames = await device.get_all_displaynames() |
| 49 | + return web.json_response(displaynames) |
| 50 | + |
| 51 | + |
| 52 | +app.on_startup.append(on_startup) |
| 53 | +app.on_cleanup.append(on_cleanup) |
| 54 | +app.add_routes(routes) |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + web.run_app(app) |
0 commit comments