-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hello.py
215 lines (121 loc) · 4.37 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
from flask import request, json, Blueprint, Flask, render_template, request, url_for,jsonify, redirect, g
import random
import circuitbreaker
import sqlite3
from flask_sqlalchemy import SQLAlchemy
from functools import wraps
import boto.sqs
from boto.sqs.message import Message
from httplib2 import Http
import urllib
import re
import requests
import string
from raven.contrib.flask import Sentry
import logging
# Initialize the Flask application
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.sqlite3'
app.config['SECRET_KEY'] = "random string"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
sentry = Sentry (app, logging = True, level= logging.INFO, dsn = 'https://c6e82df45a6b4362a50f95a053d6d949:4918d4a757574a90812d7c4236adaa72@sentry.io/110511')
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
# Caller Service endpoint
# Define a route for the default URL, which loads the form
@app.route('/index')
@app.route('/')
def form():
return render_template('form_submit.html')
@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 = circuitbreaker.postReq(
"http://127.0.0.1:5000/hello",
headers={ 'Content-Type' : 'application/x-www-form-urlencoded' }, # Form type
data=url_params # post data
)
sentry.captureMessage(callServ)
return callServ._content
@app.route('/getcaller',methods=['GET'])
def getcaller():
sentry.captureMessage(request)
callServ=circuitbreaker.getReq('http://127.0.0.1:5000/gethello',params=request.args)
sentry.captureMessage(callServ)
return callServ._content
@app.route('/superfire')
def superfire():
for i in range(1,50):
num = random.randrange(1,3)
if num == 1:
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)
url_params = urllib.urlencode(params)
callServ = circuitbreaker.postReq(
"http://127.0.0.1:5000/hello",
headers={ 'Content-Type' : 'application/x-www-form-urlencoded' }, # Form type
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=circuitbreaker.getReq('http://127.0.0.1:5000/gethello',params={'yourname':name})
sentry.captureMessage(callServ)
return render_template('DisplayAll.html', entries=stuff.query.all())
#####################################################################################
##################################### EXTERNAL SERVICE
#####################################################################################
@app.route('/gethello',methods=['GET'])
def gethello():
name = request.args['yourname']
num = random.randrange(0,3)
if num == 0:
return jsonify({'Message': 'Success', 'Name': name}), 200
elif num == 1:
return jsonify({'Message': '400 Errors', 'Name': name}), 400
elif num == 2:
return jsonify({'Message': '500 Errors', 'Name': name}), 500
# Responder Service
@app.route('/hello', methods=['POST'])
def hello():
num= random.randrange(0,3)
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': 200, "name": name, "email": email}), 200
elif num==1:
return jsonify({"code":400, "name": name,"email": email}), 400
elif num==2:
return jsonify({"code": 500, "name": name,"email": email}), 500
###############################
@app.route('/serviceDown', methods=['POST'])
def serviceDown():
return render_template("400.html")
@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)