Files
CyberGril/samples/companion/tools/graph_server.py
T

42 lines
1.3 KiB
Python

import argparse
import asyncio
from pathlib import Path
import aiohttp
from aiohttp import web
p = argparse.ArgumentParser()
p.add_argument('--wav', type=Path, required=True)
p.add_argument('--index', type=Path, required=True)
a = p.parse_args()
INDEX = a.index.read_bytes()
WAV = a.wav.read_bytes()
async def proxy(request):
data = await request.read()
async with aiohttp.ClientSession() as http, http.request(
request.method,
"http://127.0.0.1:8010" + request.path[len("/lt") :],
data=data if request.method == "POST" else None,
headers={"Content-Type": request.headers.get("Content-Type", "")} if request.method == "POST" else {},
) as r:
body = await r.read()
return web.Response(status=r.status, body=body, content_type=r.content_type)
async def main():
app = web.Application()
app.router.add_get("/", lambda _: web.Response(body=INDEX, content_type="text/html"))
app.router.add_get("/fixed.wav", lambda _: web.Response(body=WAV, content_type="audio/wav"))
app.router.add_route("*", "/lt/{tail:.*}", proxy)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "0.0.0.0", 8933)
await site.start()
print("serving on 8933")
await asyncio.Event().wait()
asyncio.run(main())