app.py 1014 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import ujson as json
  2. def json_endpoint(message):
  3. content = json.dumps({'message': 'Hello, world!'}).encode('utf-8')
  4. response = {
  5. 'status': 200,
  6. 'headers': [
  7. [b'content-type', b'application/json'],
  8. ],
  9. 'content': content
  10. }
  11. message['reply_channel'].send(response)
  12. def plaintext_endpoint(message):
  13. content = b'Hello, world!'
  14. response = {
  15. 'status': 200,
  16. 'headers': [
  17. [b'content-type', b'text/plain'],
  18. ],
  19. 'content': content
  20. }
  21. message['reply_channel'].send(response)
  22. def handle_404(message):
  23. content = b'Not found'
  24. response = {
  25. 'status': 404,
  26. 'headers': [
  27. [b'content-type', b'text/plain'],
  28. ],
  29. 'content': content
  30. }
  31. message['reply_channel'].send(response)
  32. routes = {
  33. '/json': json_endpoint,
  34. '/plaintext': plaintext_endpoint
  35. }
  36. def main(message):
  37. path = message['content']['path']
  38. routes.get(path, handle_404)(message)