app.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os, sys
  2. from starlette.applications import Starlette
  3. from starlette.responses import HTMLResponse, JSONResponse, Response
  4. from starlette.routing import Route
  5. if "peers_identity" not in sys.path:
  6. sys.path.insert(0, os.path.abspath("./src"))
  7. from peers_identity import register, inbox, outbox
  8. async def get_request_json(request):
  9. try:
  10. return await request.json()
  11. except:
  12. return None
  13. def get_request_method(request):
  14. try:
  15. return request.method
  16. except:
  17. return None
  18. async def homepage_r(request):
  19. return Response("Welcome home.", media_type="text/plain")
  20. async def register_r(request):
  21. return JSONResponse(
  22. register(get_request_method(request), await get_request_json(request))
  23. )
  24. async def inbox_r(request):
  25. return JSONResponse(inbox())
  26. async def outbox_r(request):
  27. return JSONResponse(outbox())
  28. app = Starlette(
  29. debug=True,
  30. routes=[
  31. Route("/", homepage_r),
  32. Route("/register", register_r, methods=["GET", "POST"]),
  33. Route("/inbox", inbox_r),
  34. Route("/outbox", outbox_r),
  35. ],
  36. )