4 # Copyright 2019 AT&T Intellectual Property
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 from flask import Flask, jsonify, request
21 from datetime import timedelta
22 from flask import make_response, request, current_app
23 from flask_cors import CORS, cross_origin
24 from functools import update_wrapper
27 def crossdomain(origin=None, methods=None, headers=None, max_age=21600,
28 attach_to_all=True, automatic_options=True):
29 """Decorator function that allows crossdomain requests.
31 https://blog.skyred.fi/articles/better-crossdomain-snippet-for-flask.html
33 if methods is not None:
34 methods = ', '.join(sorted(x.upper() for x in methods))
35 if headers is not None and not isinstance(headers, list):
36 headers = ', '.join(x.upper() for x in headers)
37 if not isinstance(origin, list):
38 origin = ', '.join(origin)
39 if isinstance(max_age, timedelta):
40 max_age = max_age.total_seconds()
43 """ Determines which methods are allowed
45 if methods is not None:
48 options_resp = current_app.make_default_options_response()
49 return options_resp.headers['allow']
52 """The decorator function
54 def wrapped_function(*args, **kwargs):
55 """Caries out the actual cross domain code
57 if automatic_options and request.method == 'OPTIONS':
58 resp = current_app.make_default_options_response()
60 resp = make_response(f(*args, **kwargs))
61 if not attach_to_all and request.method != 'OPTIONS':
65 h['Access-Control-Allow-Origin'] = origin
66 h['Access-Control-Allow-Methods'] = get_methods()
67 h['Access-Control-Max-Age'] = str(max_age)
68 h['Access-Control-Allow-Credentials'] = 'true'
69 h['Access-Control-Allow-Headers'] = \
70 "Origin, X-Requested-With, Content-Type, Accept, Authorization"
71 if headers is not None:
72 h['Access-Control-Allow-Headers'] = headers
75 f.provide_automatic_options = False
76 return update_wrapper(wrapped_function, f)
82 @app.route('/a1ric/metrics', methods=['GET'])
84 with open('metrics.json') as json_file:
85 metrics = json.load(json_file)
86 return jsonify(metrics)
88 @app.route('/a1ric/delay', methods=['GET'])
90 with open('delay.json') as json_file:
91 delay = json.load(json_file)
94 @app.route('/a1ric/load', methods=['GET'])
96 with open('load.json') as json_file:
97 load = json.load(json_file)
100 @app.route('/a1ric/delay', methods=['PUT'])
101 def write_delay_file():
102 if not request.json or not 'delay' in request.json:
105 'delay': request.json['delay'],
107 delay_json = json.dumps(delay)
108 f = open("delay.json","w")
111 f = open("delay.txt","w")
112 print (request.json['delay'])
113 f.write(str(request.json['delay']))
115 return jsonify(delay), 201
117 @app.route('/a1ric/load', methods=['PUT'])
118 def write_load_file():
119 if not request.json or not 'load' in request.json:
122 'load': request.json['load'],
124 load_json = json.dumps(load)
125 f = open("load.json","w")
128 f = open("load.txt","w")
129 print (request.json['load'])
130 #f.write(str(request.json['load']*80000))
131 f.write(str(request.json['load']*(8/1000)))
133 return jsonify(load), 201
135 if __name__ == '__main__':
136 app.run(debug=True, host='0.0.0.0', port=10080)
137 #app.run(debug=True, host='0.0.0.0', port=3000)