Modify backend app

- add download route
- combine chart endpoints to single get_json route
This commit is contained in:
Sam 2024-08-13 18:55:20 +01:00
parent 1ef277edc5
commit 198ab2a1f8
1 changed files with 20 additions and 37 deletions

View File

@ -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 from flask_cors import CORS
import orjson import orjson, os
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
FILES_DIRECTORY = '../data/'
@app.route('/bitcoin_business_growth_by_country', methods=['GET']) @app.route('/bitcoin_business_growth_by_country', methods=['GET'])
def business_growth(): def business_growth():
# Parse args from request # Parse args from request
@ -36,45 +38,26 @@ def business_growth():
# Return json # Return json
return Response(json.dumps(sorted_data), mimetype='application/json') return Response(json.dumps(sorted_data), mimetype='application/json')
@app.route('/price', methods=['GET']) @app.route('/get_json/<filename>', methods=['GET'])
def price(): 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(file_path, 'r') as file:
with open('../data/final__price.json', 'rb') as f: data = json.load(file)
data = orjson.loads(f.read())
# Return json return jsonify(data)
return Response(json.dumps(data), mimetype='application/json')
@app.route('/miner_rewards', methods=['GET']) @app.route('/download/<filename>', methods=['GET'])
def miner_rewards(): def download_file(filename):
try:
return send_from_directory(FILES_DIRECTORY, filename, as_attachment=True)
except FileNotFoundError:
abort(404)
# Open json locally if __name__ == '__main__':
with open('../data/final__miner_rewards.json', 'rb') as f: app.run(debug=True)
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__': if __name__ == '__main__':
app.run() app.run()