-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
149 lines (117 loc) · 3.52 KB
/
app.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
146
147
148
from flask import Flask, render_template, request
import psycopg2
import os
import socket
import json
import time
app = Flask(__name__)
db_user = os.environ.get('POSTGRES_DB_USER')
db_psw = os.environ.get('POSTGRES_DB_PSW')
db_host = os.environ.get('SERVICE_POSTGRES_SERVICE_HOST')
db_name = os.environ.get('POSTGRES_DB_NAME')
def table_exists(table_str):
exists = False
try:
cursor = conn.cursor()
cursor.execute("select exists(select relname from pg_class where relname='" + table_str + "')")
exists = cursor.fetchone()[0]
except psycopg2.Error as e:
print(e)
finally:
if (conn):
cursor.close()
print("PostgreSQL cursor is closed")
return exists
if db_name == None:
db_name = "workshopdb"
else:
print("database name: " + str(db_name))
conn = None
try:
conn = psycopg2.connect(dbname=db_name, user=db_user, password=db_psw, host=db_host, port="5432")
print(conn)
print("connected database: " + str(db_name))
except Exception as e:
print("Error: " + str(e))
if not table_exists("users"):
try:
cur = conn.cursor()
cur.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255) NOT NULL);")
cur.close()
conn.commit()
print("Table users was Created")
except Exception as e:
print("Error: " + str(e))
else:
print("Table users already exists")
@app.route('/')
def index():
return render_template('index.html')
@app.route('/success')
def success():
return render_template('success.html')
@app.route('/healthz')
def healthz():
return "OK"
@app.route('/healthx')
def healthx():
time.sleep(2);
return "OK"
@app.route('/system')
def systemInfo():
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
data = {}
data['ContainerName'] = hostname
data['ContainerIP'] = IPAddr
json_data = json.dumps(data)
return json_data
@app.route('/prereg', methods=['POST'])
def prereg():
print("---prereg---")
email = None
cursor = conn.cursor()
print(request)
print(request.form)
if request.method == 'POST':
email = request.form['email']
try:
cursor.execute(" select exists ( select email from users where email = %s )", (email,))
if not cursor.fetchone()[0]:
print("Email:" + str(email) + " was added")
cursor.execute("INSERT INTO users (email) VALUES(%s)", (email,))
cursor.close()
conn.commit()
return render_template('success.html')
else:
print(str(email) + " already exists!")
except Exception as e:
print("Error: " + str(e))
print("---end prereg---")
return render_template('index.html')
@app.route('/getdata', methods=['GET'])
def contextget():
print("---getdata---")
print(request, request.endpoint)
cursor = conn.cursor()
data = None
response = []
try:
cursor.execute("SELECT * FROM users")
data = cursor.fetchall()
except Exception as e:
print("Error: " + str(e))
finally:
# closing database connection.
if (conn):
cursor.close()
print("PostgreSQL cursor is closed")
for result in data:
regist = {"id": result[0], "email": result[1]}
response.append(regist)
response = json.dumps(response)
print(response)
print("---end getdata---")
return response
if __name__ == '__main__':
app.run(host='0.0.0.0')