-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
304 lines (255 loc) · 9.57 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin python
"""
#
# Program to get data from remote DB server and show in the frontend
# This script defines all the routes to be accessed by frontend web server
# Author Sajal Debnath <sdebnath@vmware.com><debnathsajal@gmail.com>
#
"""
# Importing the Modules
from flask import Flask, render_template, request, jsonify
import requests
# Defining the Database details
import time
import json
# Importing socket library
import socket
import os
db_fqdn = "db-vm.vercel.app" # Modify the value with the actual database server
api_url_base = "http://" + db_fqdn + "/employee"
# Defining the app details
# Init app
app = Flask(__name__)
app.config["DEBUG"] = True
################### Define functions and routes ##########################
# Function to determine Web Server
def get_Web_Host_name_IP():
try:
webservers = {
'web-server-1': 'web-01',
'web-server-2': 'web-02'
}
remote = request.remote_addr
#print(remote)
if remote in webservers:
web_host_name = webservers[remote]
else:
web_host_name = remote
#print(web_host_name)
return web_host_name
except:
print("Unable to get Web Hostname and IP")
return "Unable to get Web Hostname"
# Function to display hostname and
# IP address
def get_Host_name_IP():
try:
host_name = socket.gethostname()
#host_ip = socket.gethostbyname(host_name)
print("Hostname : ", host_name)
#print("IP : ", host_ip)
return host_name
except:
print("Unable to get Hostname and IP")
return ""
# Get all the employees data
@app.route('/')
def employees():#
api_url = "{0}".format(api_url_base)
response = ''
while response == '':
try:
response = requests.get(api_url, headers=header, verify=False)
break
except:
for i in range(3):
print("Connection refused by the server..")
print("Let's wait for 5 seconds and try again")
time.sleep(5)
print("Trying again...")
continue
#print(response)
if response.status_code == 200:
json_data_all = response.json()
json_data = json_data_all["data"]
employee_data = json_data[0]
#print(json_data[0])
host_name = get_Host_name_IP()
web_host_name = get_Web_Host_name_IP()
return render_template('homepage.html',employee_data=employee_data, db_fqdn=db_fqdn, web_host_name=web_host_name, host_name=host_name)
else:
print("Could not get employee list")
return "Could not get employee list"
# Create a new employee record
@app.route('/employee', methods=["POST"]) #,"GET"])
def single_employee():
api_url = "{0}".format(api_url_base)
headers = {'content-type': 'application/json'}
if request.method == 'POST':
emp_id = request.form['emp_id']
first_name = request.form['first_name']
last_name = request.form['last_name']
#email = first_name + '.' + last_name + 'acme.com' # Email auto-generated
email = request.form['email']
ph_no = request.form['ph_no']
home_addr = request.form['home_addr']
st_addr = request.form['st_addr']
gender = request.form['gender']
job_type = request.form['job_type']
# print(emp_id)
# print(first_name)
# print(last_name)
# print(email)
# print(ph_no)
# print(home_addr)
# print(st_addr)
# print(gender)
# print(job_type)
# print("I am making the request now")
request_body = {
"emp_id": emp_id,
"first_name": first_name,
"last_name": last_name,
"email": email,
"ph_no": ph_no,
"home_addr": home_addr,
"st_addr": st_addr,
"gender": gender,
"job_type": job_type
}
#payload = jsonify(request_body)
print("Request Body", request_body)
response = ''
while response == '':
try:
response = requests.post(api_url, data = json.dumps(request_body), headers=headers)
break
except:
for i in range(3):
print("Connection refused by the server..")
print("Let's wait for 5 seconds and try again")
time.sleep(5)
print("Trying again...")
continue
#print(response)
info=response.json()
#print(info)
#print(response.status_code)
if response.status_code == 201:
json_data_all = response.json()
json_data = json_data_all["data"]
print("Successfully added the record for the employee: {}".format(first_name))
return ("Successfully added the record for the employee: {}".format(first_name))
else:
print("Could not add to the employee record. Error: {}".format(info))
return ("Could not add to the employee record. Error: {}".format(info))
# GET, UPDATE/Edit, DELETE an existing employee record
@app.route('/employee/<int:id>', methods=["PUT","GET", "DELETE"])
def edit_employee_record(id):
api_url = "{0}/{1}".format(api_url_base, id)
headers = {'content-type': 'application/json'}
# response = requests.get(api_url, headers=header, verify=False)
if request.method == 'PUT':
emp_id = request.form['emp_id']
first_name = request.form['first_name']
last_name = request.form['last_name']
email = first_name + '.' + last_name + '@acme.com' # Email auto-generated
ph_no = request.form['ph_no']
home_addr = request.form['home_addr']
st_addr = request.form['st_addr']
gender = request.form['gender']
job_type = request.form['job_type']
# print(emp_id)
# print(first_name)
# print(last_name)
# print(email)
# print(ph_no)
# print(home_addr)
# print(st_addr)
# print(gender)
# print(job_type)
# print("I am making the edit request now")
request_body = {
"emp_id": emp_id,
"first_name": first_name,
"last_name": last_name,
"email": email,
"ph_no": ph_no,
"home_addr": home_addr,
"st_addr": st_addr,
"gender": gender,
"job_type": job_type
}
#payload = jsonify(request_body)
print("Request Body", request_body)
response = ''
while response == '':
try:
response = requests.put(api_url, data = json.dumps(request_body), headers=headers)
break
except:
for i in range(3):
print("Connection refused by the server..")
print("Let's wait for 5 seconds and try again")
time.sleep(5)
print("Trying again...")
continue
info=response.json()
print(info)
print(response.status_code)
#if response.status_code == 201:
if response.status_code == 200:
json_data_all = response.json()
json_data = json_data_all["data"]
print("Successfully edited the record for the employee: {}".format(json_data))
return ("Successfully edited the record for the employee: {}".format(json_data))
else:
print("Could not edit the employee record. Error: {}".format(info))
return ("Could not edit the employee record. Error: {}".format(info))
elif request.method == 'GET':
response = ''
while response == '':
try:
response = requests.get(api_url, headers=header, verify=False)
break
except:
for i in range(3):
print("Connection refused by the server..")
print("Let's wait for 5 seconds and try again")
time.sleep(5)
print("Trying again...")
continue
#print(response)
if response.status_code == 200:
json_data_all = response.json()
json_data = json_data_all["data"]
return render_template('homepage.html',json_data=json_data)
else:
print("Could not get employee list")
return "Could not get employee list"
elif request.method == 'DELETE':
response = ''
while response == '':
try:
response = requests.delete(api_url, headers=header, verify=False)
break
except:
for i in range(3):
print("Connection refused by the server..")
print("Let's wait for 5 seconds and try again")
time.sleep(5)
print("Trying again...")
continue
#print(response)
if response.status_code == 200:
json_data_all = response.json()
json_data = json_data_all["data"]
print("Successfully deleted the record: {}".format(json_data))
return ("Successfully deleted the record: {}".format(json_data))
else:
print("Could not delete employee record")
return "Could not delete employee record"
##### MAIN ##########
header = {"Content-Type": "application/json", "Accept": "application/json"}
if __name__ == '__main__':
app.run()