app_rsgi_nogil.py 793 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import json
  2. JSON_HEADERS = [('content-type', 'application/json')]
  3. PLAINTEXT_HEADERS = [('content-type', 'text/plain; charset=utf-8')]
  4. # json_dumps = orjson.dumps
  5. json_dumps = json.dumps
  6. async def route_json(scope, proto):
  7. proto.response_str(
  8. 200,
  9. JSON_HEADERS,
  10. json_dumps({'message': 'Hello, world!'})
  11. )
  12. async def route_plaintext(scope, proto):
  13. proto.response_bytes(
  14. 200,
  15. PLAINTEXT_HEADERS,
  16. b'Hello, world!'
  17. )
  18. async def handle_404(scope, proto):
  19. proto.response_bytes(
  20. 404,
  21. PLAINTEXT_HEADERS,
  22. b'Not found'
  23. )
  24. routes = {
  25. '/json': route_json,
  26. '/plaintext': route_plaintext
  27. }
  28. def main(scope, proto):
  29. handler = routes.get(scope.path, handle_404)
  30. return handler(scope, proto)