app.py 735 B

1234567891011121314151617181920212223242526272829303132
  1. from apistar import App, Route, wsgi
  2. import ujson as json
  3. def json_view() -> wsgi.WSGIResponse:
  4. content = json.dumps({'message': 'Hello, world!'}).encode('utf-8')
  5. return wsgi.WSGIResponse(
  6. '200 OK',
  7. [
  8. ('Content-Type', 'application/json'),
  9. ('Content-Length', str(len(content)))
  10. ],
  11. [content]
  12. )
  13. def plaintext_view() -> wsgi.WSGIResponse:
  14. content = b'Hello, world!'
  15. return wsgi.WSGIResponse(
  16. '200 OK',
  17. [
  18. ('Content-Type', 'text/plain'),
  19. ('Content-Length', str(len(content)))
  20. ],
  21. [content]
  22. )
  23. app = App(routes=[
  24. Route('/json', 'GET', json_view),
  25. Route('/plaintext', 'GET', plaintext_view),
  26. ])