123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import os, sys
- from starlette.applications import Starlette
- from starlette.responses import HTMLResponse, JSONResponse, Response
- from starlette.routing import Route
- if "peers_identity" not in sys.path:
- sys.path.insert(0, os.path.abspath("./src"))
- from peers_identity import register, inbox, outbox
- async def get_request_json(request):
- try:
- return await request.json()
- except:
- return None
- def get_request_method(request):
- try:
- return request.method
- except:
- return None
- async def homepage_r(request):
- return Response("Welcome home.", media_type="text/plain")
- async def register_r(request):
- return JSONResponse(
- register(get_request_method(request), await get_request_json(request))
- )
- async def inbox_r(request):
- return JSONResponse(inbox())
- async def outbox_r(request):
- return JSONResponse(outbox())
- app = Starlette(
- debug=True,
- routes=[
- Route("/", homepage_r),
- Route("/register", register_r, methods=["GET", "POST"]),
- Route("/inbox", inbox_r),
- Route("/outbox", outbox_r),
- ],
- )
|