main.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # _
  4. # | |
  5. # __| |_ __ ___ __ _ _ __ ___ ___
  6. # / _` | '__/ _ \/ _` | '_ ` _ \/ __|
  7. # | (_| | | | __/ (_| | | | | | \__ \
  8. # \__,_|_| \___|\__,_|_| |_| |_|___/ .
  9. #
  10. # A 'Fog Creek'–inspired demo by Kenneth Reitz™
  11. import os
  12. from flask import Flask, request, render_template, jsonify
  13. # Support for gomix's 'front-end' and 'back-end' UI.
  14. app = Flask(__name__, static_folder='public', template_folder='views')
  15. # Set the app secret key from the secret environment variables.
  16. app.secret = os.environ.get('SECRET')
  17. # Dream database. Store dreams in memory for now.
  18. DREAMS = ['Dreams are ephemeral']
  19. @app.after_request
  20. def apply_kr_hello(response):
  21. """Adds some headers to all responses."""
  22. # Made by Kenneth Reitz.
  23. if 'MADE_BY' in os.environ:
  24. response.headers["X-Was-Here"] = os.environ.get('MADE_BY')
  25. # Powered by Flask.
  26. response.headers["X-Powered-By"] = os.environ.get('POWERED_BY')
  27. return response
  28. @app.route('/')
  29. def homepage():
  30. """Displays the homepage."""
  31. return render_template('index.html')
  32. @app.route('/dreams', methods=['GET', 'POST'])
  33. def dreams():
  34. """Simple API endpoint for dreams.
  35. In memory, ephemeral, like real dreams.
  36. """
  37. # Add a dream to the in-memory database, if given.
  38. if 'dream' in request.args:
  39. DREAMS.append(request.args['dream'])
  40. # Return the list of remembered dreams.
  41. return jsonify(DREAMS)
  42. if __name__ == '__main__':
  43. app.run()