benchmarks/dockerize/sanic/gateway.py
import logging
from typing import Optional
from aiohttp import ClientSession
from sanic import Sanic
from sanic.response import json, text
logger = logging.getLogger(__name__)
try:
import uvloop
uvloop.install()
except ImportError:
logger.warn("Cannot use uvloop")
pass
session: Optional[ClientSession] = None
app = Sanic("gateway")
@app.route("/hello")
async def test(request):
global session
if session is None:
session = ClientSession()
r = await session.get("http://server:8011/hello")
return text(await r.text())
@app.route("/order")
async def test(request):
global session
if session is None:
session = ClientSession()
r = await session.post(
"http://server:8011/order",
json={"user_id": "1", "items": ["apple", "python"]},
)
return json(await r.json())
if __name__ == "__main__":
app.run()