-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
72 lines (55 loc) · 1.92 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# _
# | |
# __| |_ __ ___ __ _ _ __ ___ ___
# / _` | '__/ _ \/ _` | '_ ` _ \/ __|
# | (_| | | | __/ (_| | | | | | \__ \
# \__,_|_| \___|\__,_|_| |_| |_|___/ .
#
# A 'Fog Creek'–inspired demo by Kenneth Reitz™
import json
import random
import os
from flask import Flask, request, render_template, jsonify
import excuses as e
# Support for gomix's 'front-end' and 'back-end' UI.
app = Flask(__name__, static_folder='public', template_folder='views')
# Set the app secret key from the secret environment variables.
app.secret = os.environ.get('SECRET')
# Dream database. Store dreams in memory for now.
DREAMS = ['Python. Python, everywhere.']
f = open("words.json", "r")
words = json.load(f)
f.close()
@app.after_request
def apply_kr_hello(response):
"""Adds some headers to all responses."""
# Made by Kenneth Reitz.
if 'MADE_BY' in os.environ:
response.headers["X-Was-Here"] = os.environ.get('MADE_BY')
# Powered by Flask.
response.headers["X-Powered-By"] = os.environ.get('POWERED_BY')
return response
@app.route('/')
def homepage():
"""Displays the homepage."""
return render_template('index.html')
@app.route('/dreams', methods=['GET', 'POST'])
def dreams():
"""Simple API endpoint for dreams.
In memory, ephemeral, like real dreams.
"""
# Add a dream to the in-memory database, if given.
if 'dream' in request.args:
DREAMS.append(request.args['dream'])
# Return the list of remembered dreams.
return jsonify(DREAMS)
@app.route('/excuses')
def excuses():
if random.random() < 0.001:
return "I'm in bed in my pyjamas."
scheme = e.choose_scheme(words)
return e.excuse_from_scheme(scheme, words)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))