-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
57 lines (40 loc) · 1.18 KB
/
web.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
import os
import bottle
from db import get_latest_messages, insert_message
MESSAGE_FORM = """
<br><br>
<h3>Post New Message</h3>
<form action="/messages" method="post">
Username:<br>
<input name="username"><br>
Message:<br>
<textarea name="message"></textarea><br>
<button type="submit">Submit</button>
</form>
"""
@bottle.get('/')
def index():
messages = get_latest_messages()
html = '<h1>Heroku Workshop!</h1>'
# List messages
html += '<br><br>'
html += '<h3>Latest Messages:</h3>'
html += '<ul>'
for message in messages:
html += '<li>@%s: %s</li>' % (message['username'], message['text'])
html += '</ul>'
html += MESSAGE_FORM
return html
@bottle.post('/messages')
def messages():
username = bottle.request.forms.get('username')
message = bottle.request.forms.get('message')
print 'New Message: %s - %s' % (username, message)
insert_message(username, message)
bottle.redirect('/')
# Run server
if __name__ == '__main__':
if 'PORT' in os.environ:
bottle.run(host='0.0.0.0', port=os.environ['PORT'])
else:
bottle.run(host='localhost', port=8080)