-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwo1proxy.py
57 lines (47 loc) · 1.65 KB
/
two1proxy.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
# http
from flask import Flask, request, abort
import requests
#import from the 21 Developer Library
from two1.wallet import Wallet
from two1.bitserv.flask import Payment
#helpers
from loader import load_yaml_config
# set up server side wallet
app = Flask(__name__)
wallet = Wallet()
payment = Payment(app, wallet)
# setup routes to handle services
config = load_yaml_config()
# gets fee from service definition
def get_service_fee(request):
service_path = request.path.replace(config['prefix'],'')
match = config['routes'].get(service_path, None)
if match:
print('fee:', match['price'], 'for', service_path)
return match['price']
else:
print('invalid service route:',service_path)
abort(404)
@app.route(config['prefix'], methods=['GET', 'POST'], defaults={'path': ''})
@app.route(config['prefix'] +'<path:path>', methods=['GET', 'POST'])
@payment.required(get_service_fee)
def catch_all(path):
# check if service is handling this request
match = config['routes'].get(path, None)
if match:
destination = match.get('destination', path);
# call service
if request.method == 'GET':
service_get = 'http://' + match['service']['host'] + ':' + str(match['service']['port']) + '/' + destination + '?' + request.query_string.decode("utf-8")
r = requests.get(service_get)
return r.text, r.status_code
elif request.method == "POST":
r = requests.post(
'http://' + match['host'] + ':' + match['port'] + '/' + destination,
data = request.data
);
return r.text, r.status_code
else:
return 404
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')