app_wrk.py 771 B

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