diff --git a/backend/app.py b/backend/app.py index 1d072f8..0a3b779 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,10 +1,12 @@ -from flask import Flask, request, json, Response +from flask import Flask, jsonify, request, json, Response, send_from_directory, abort from flask_cors import CORS -import orjson +import orjson, os app = Flask(__name__) CORS(app) +FILES_DIRECTORY = '../data/' + @app.route('/bitcoin_business_growth_by_country', methods=['GET']) def business_growth(): # Parse args from request @@ -36,45 +38,26 @@ def business_growth(): # Return json return Response(json.dumps(sorted_data), mimetype='application/json') -@app.route('/price', methods=['GET']) -def price(): +@app.route('/get_json/', methods=['GET']) +def get_json(filename): + file_path = os.path.join(FILES_DIRECTORY, filename) + if not os.path.isfile(file_path): + abort(404) - # Open json locally - with open('../data/final__price.json', 'rb') as f: - data = orjson.loads(f.read()) + with open(file_path, 'r') as file: + data = json.load(file) - # Return json - return Response(json.dumps(data), mimetype='application/json') + return jsonify(data) -@app.route('/miner_rewards', methods=['GET']) -def miner_rewards(): +@app.route('/download/', methods=['GET']) +def download_file(filename): + try: + return send_from_directory(FILES_DIRECTORY, filename, as_attachment=True) + except FileNotFoundError: + abort(404) - # Open json locally - with open('../data/final__miner_rewards.json', 'rb') as f: - data = orjson.loads(f.read()) - - # Return json - return Response(json.dumps(data), mimetype='application/json') - -@app.route('/hashrate', methods=['GET']) -def hashrate(): - - # Open json locally - with open('../data/dev/final__hashrate.json', 'rb') as f: - data = orjson.loads(f.read()) - - # Return json - return Response(json.dumps(data), mimetype='application/json') - -@app.route('/feerates', methods=['GET']) -def feerates(): - - # Open json locally - with open('../data/final__feerate_percentiles.json', 'rb') as f: - data = orjson.loads(f.read()) - - # Return json - return Response(json.dumps(data), mimetype='application/json') +if __name__ == '__main__': + app.run(debug=True) if __name__ == '__main__': app.run()