-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
41 lines (34 loc) · 1021 Bytes
/
application.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
import os
import flask
import MySQLdb
application = flask.Flask(__name__)
application.debug = True
@application.route('/')
def hello_world():
storage = Storage()
storage.populate()
print("get score:")
score = storage.score()
print(score)
return "Hello Beijing 123, %d!" % score
class Storage():
def __init__(self):
self.db = MySQLdb.connect(
user = os.getenv('MYSQL_USERNAME'),
passwd = os.getenv('MYSQL_PASSWORD'),
db = os.getenv('MYSQL_INSTANCE_NAME'),
host = os.getenv('MYSQL_PORT_3306_TCP_ADDR'),
port = int(os.getenv('MYSQL_PORT_3306_TCP_PORT'))
)
cur = self.db.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS scores(score INT)")
def populate(self):
cur = self.db.cursor()
cur.execute("INSERT INTO scores(score) VALUES(1234)")
def score(self):
cur = self.db.cursor()
cur.execute("SELECT * FROM scores")
row = cur.fetchone()
return row[0]
if __name__ == "__main__":
application.run(host='0.0.0.0', port=3000)