|
|
|
# BUILT-INS
|
|
|
|
import os
|
|
|
|
|
|
|
|
# VENDOR
|
|
|
|
import aiohttp
|
|
|
|
from aiohttp import web
|
|
|
|
import aiofiles
|
|
|
|
|
|
|
|
dir = os.path.dirname(__file__)
|
|
|
|
app = web.Application()
|
|
|
|
|
|
|
|
|
|
|
|
async def index(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
async with aiofiles.open(os.path.join(dir, 'public', 'index.html')) as conn:
|
|
|
|
return web.Response(text=await conn.read(), content_type='text/html')
|
|
|
|
|
|
|
|
return web.HTTPMethodNotAllowed
|
|
|
|
|
|
|
|
|
|
|
|
async def api(request):
|
|
|
|
path = request.match_info["path"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get('http://127.0.0.1:8050/%s' % path) as res:
|
|
|
|
return web.Response(text=await res.text(), content_type="application/json")
|
|
|
|
except Exception as e:
|
|
|
|
raise web.HTTPBadRequest(e)
|
|
|
|
|
|
|
|
|
|
|
|
app.add_routes([
|
|
|
|
web.get("/", index),
|
|
|
|
web.get("/rest/{path:.+}", api)
|
|
|
|
])
|
|
|
|
|
|
|
|
app.router.add_static('/',
|
|
|
|
path=os.path.join(dir, 'public'),
|
|
|
|
name='public')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
web.run_app(app)
|