def create_starlette_app(): app = applications.Starlette() @app.route("/foobar") def _(request): return PlainTextResponse( content="hi", headers={ "custom-test-header-1": "test-header-value-1", "custom-test-header-2": "test-header-value-2", }, ) @app.websocket_route("/foobar_web") async def _(websocket: WebSocket) -> None: message = await websocket.receive() if message.get("type") == "websocket.connect": await websocket.send({ "type": "websocket.accept", "headers": [ (b"custom-test-header-1", b"test-header-value-1"), (b"custom-test-header-2", b"test-header-value-2"), ], }) await websocket.send_json({"message": "hello world"}) await websocket.close() if message.get("type") == "websocket.disconnect": pass return app
def _create_starlette_app(): def home(_): return PlainTextResponse("hi") app = applications.Starlette( routes=[Route("/foobar", home), Route("/user/{username}", home)]) return app
import sys import aiohttp import asyncio import uvicorn import base64 from starlette import responses as sl_res from starlette import applications as sl_app from starlette.middleware import cors import PIL from lib import util DATA_DIR = 'data' MODEL_NAME = 'bears' app = sl_app.Starlette() app.add_middleware(cors.CORSMiddleware, allow_origins=['*'], allow_headers=['X-Requested-With', 'Content-Type']) async def setup_learner(): learner = util.load_inf_model(MODEL_NAME, DATA_DIR) return learner loop = asyncio.get_event_loop() tasks = [asyncio.ensure_future(setup_learner())] learner = loop.run_until_complete(asyncio.gather(*tasks))[0] loop.close()