-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHello.py
260 lines (163 loc) · 5.97 KB
/
Hello.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
import logging
import urllib
import string
import random
from flask import request, json, Flask, render_template
from flask import jsonify, redirect
from cb.circuitbreaker import circuitBreaker
from flask_sqlalchemy import SQLAlchemy
from raven.contrib.flask import Sentry
from config import SECRET_KEY, SQLALCHEMY_TRACK_MODIFICATIONS, DSN_URL,\
GET_URL, POST_URL
import redis #import for demo function
# Initialize the Flask application
app = Flask(__name__)
app.config.from_object('config')
sentry = Sentry(app, logging=True, level=logging.INFO, dsn=DSN_URL)
redisDB = redis.Redis('localhost') # dont need this in app as such
# here to provide manual control over trip demo
db = SQLAlchemy(app)
class stuff(db.Model):
id = db.Column('user_id', db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
reqType = db.Column(db.String(100))
def __init__(self, name, email, reqType):
self.name = name
self.email = email
self.reqType = reqType
cb = circuitBreaker()
# Define a route for the default URL, which loads the form
@app.route('/index')
@app.route('/')
def form():
return render_template('form_submit.html')
# Proxy function to issue post requests
@app.route('/postcaller', methods=['POST'])
def postcaller():
name = request.form['yourname']
email = request.form['youremail']
sentry.captureMessage(request)
# Our parameters
params = dict(yourname=name, youremail=email)
url_params = urllib.urlencode(params)
callServ = cb.postreq(
POST_URL,
headers={'Content-Type' : 'application/x-www-form-urlencoded'}, # Form
data=url_params) # Post data
sentry.captureMessage(callServ)
return callServ._content
#proxy function to issue get requests
@app.route('/getcaller', methods=['GET'])
def getcaller():
sentry.captureMessage(request)
callServ = cb.getreq(GET_URL, params=request.args)
sentry.captureMessage(callServ)
return callServ._content
##################################################################
### DEMO FUNCTIONS ####
##################################################################
@app.route('/manualControl')
def manualControl():
return render_template('restoreList.html')
@app.route('/trippyStuff', methods=['POST'])
def trippyStuff():
getStatus = request.form['getHello']
postStatus = request.form['postHello']
if getStatus == 'trip':
redisDB.hset('circuitStatus', GET_URL, 0)
else:
redisDB.hset('circuitStatus', GET_URL, 1)
if postStatus == 'trip':
redisDB.hset('circuitStatus', POST_URL, 0)
else:
redisDB.hset('circuitStatus', POST_URL, 1)
return redirect('/')
#############################################################################
#############################################################################
####function to fire 50 random requests
@app.route('/superfire')
def superfire():
for i in range(1, 50):
num = random.randrange(1, 3) #randomly issue post or get request
if num == 1:
#random string generator
name = ''.join([random.choice(string.ascii_letters +\
string.digits) for n in xrange(5)])
email = ''.join([random.choice(string.ascii_letters +\
string.digits) for n in xrange(10)])
reqType = "POST"
params = dict(yourname=name,
youremail=email,
reqType=reqType)
callServ = cb.postreq(
POST_URL,
headers={'Content-Type':'application/x-www-form-urlencoded'},
data=params # post data
)
sentry.captureMessage(callServ)
elif num == 2:
name = ''.join([random.choice(string.ascii_letters +\
string.digits) for n in xrange(5)])
callServ = cb.getreq(GET_URL, params={'yourname':name})
sentry.captureMessage(callServ)
return render_template('DisplayAll.html', entries=stuff.query.all())
#############################################################################
##################################### EXTERNAL SERVICE
#############################################################################
#GET simulation
@app.route('/gethello', methods=['GET'])
def gethello():
name = request.args['yourname']
num = random.randrange(0, 6)
if num == 0:
return jsonify({'Message': 'Success 200', 'Name': name}), 200
elif num == 1:
return jsonify({'Message': 'Success 201', 'Name': name}), 201
elif num == 2:
return jsonify({'Message': '410 Errors', 'Name': name}), 410
elif num == 3:
return jsonify({'Message': '502 Errors', 'Name': name}), 502
elif num == 4:
return jsonify({'Message': '503 Errors', 'Name': name}), 503
elif num == 5:
return jsonify({'Message': '504 Errors', 'Name': name}), 504
# Responder Service (POST simulation)
@app.route('/hello', methods=['POST'])
def hello():
num = random.randrange(0, 6)
name = request.form['yourname']
email = request.form['youremail']
reqType = "POST"
if num == 0:
newStuff = stuff(name, email, reqType)
db.session.add(newStuff)
db.session.commit()
return jsonify({'code': 201, "name": name, "email": email}), 201
elif num == 1:
newStuff = stuff(name, email, reqType)
db.session.add(newStuff)
db.session.commit()
return jsonify({'code': 200, "name": name, "email": email}), 200
elif num == 2:
return jsonify({"code": 410, "name": name, "email": email}), 410
elif num == 3:
return jsonify({"code": 502, "name": name, "email": email}), 502
elif num == 4:
return jsonify({"code": 503, "name": name, "email": email}), 503
elif num == 5:
return jsonify({"code": 504, "name": name, "email": email}), 504
#############################################################################
#############################################################################
#written as a post method (which captures nothing actually)
#scaled to implement a queue of live requests
@app.route('/serviceDown', methods=['POST'])
def serviceDown():
return render_template("400.html")
#small view to display all db entries
@app.route('/test')
def test():
return render_template('DisplayAll.html', entries=stuff.query.all())
if __name__ == '__main__':
db.create_all()
app.run(debug=True, threaded=True)