-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapplication.py
145 lines (112 loc) · 3.86 KB
/
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import time
from flask import request
from flask import Flask, render_template
import mysql.connector
from mysql.connector import errorcode
application = Flask(__name__)
app = application
def get_db_creds():
db = os.environ.get("DB", None) or os.environ.get("database", None)
username = os.environ.get("USER", None) or os.environ.get("username", None)
password = os.environ.get("PASSWORD", None) or os.environ.get("password", None)
hostname = os.environ.get("HOST", None) or os.environ.get("dbhost", None)
return db, username, password, hostname
def create_table():
# Check if table exists or not. Create and populate it only if it does not exist.
db, username, password, hostname = get_db_creds()
table_ddl = 'CREATE TABLE message(id INT UNSIGNED NOT NULL AUTO_INCREMENT, greeting TEXT, PRIMARY KEY (id))'
cnx = ''
try:
cnx = mysql.connector.connect(user=username, password=password,
host=hostname,
database=db)
except Exception as exp:
print(exp)
raise
cur = cnx.cursor()
try:
cur.execute(table_ddl)
cnx.commit()
populate_data()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
def populate_data():
db, username, password, hostname = get_db_creds()
print("Inside populate_data")
print("DB: %s" % db)
print("Username: %s" % username)
print("Password: %s" % password)
print("Hostname: %s" % hostname)
cnx = ''
try:
cnx = mysql.connector.connect(user=username, password=password,
host=hostname,
database=db)
except Exception as exp:
print(exp)
raise
cur = cnx.cursor()
cur.execute("INSERT INTO message (greeting) values ('Hello, World!')")
cnx.commit()
print("Returning from populate_data")
def query_data():
db, username, password, hostname = get_db_creds()
print("Inside query_data")
print("DB: %s" % db)
print("Username: %s" % username)
print("Password: %s" % password)
print("Hostname: %s" % hostname)
cnx = ''
try:
cnx = mysql.connector.connect(user=username, password=password,
host=hostname,
database=db)
except Exception as exp:
print(exp)
raise
cur = cnx.cursor()
cur.execute("SELECT greeting FROM message")
entries = [dict(greeting=row[0]) for row in cur.fetchall()]
return entries
try:
print("---------" + time.strftime('%a %H:%M:%S'))
print("Before create_table global")
create_table()
print("After create_data global")
except Exception as exp:
print("Got exception %s" % exp)
conn = None
@app.route('/add_to_db', methods=['POST'])
def add_to_db():
print("Received request.")
print(request.form['message'])
msg = request.form['message']
db, username, password, hostname = get_db_creds()
cnx = ''
try:
cnx = mysql.connector.connect(user=username, password=password,
host=hostname,
database=db)
except Exception as exp:
print(exp)
raise
cur = cnx.cursor()
cur.execute("INSERT INTO message (greeting) values ('" + msg + "')")
cnx.commit()
return hello()
@app.route("/")
def hello():
print("Inside hello")
print("Printing available environment variables")
print(os.environ)
print("Before displaying index.html")
entries = query_data()
print("Entries: %s" % entries)
return render_template('index.html', entries=entries)
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0')