diff --git a/app.py b/app.py index cb4ae3d..dadb7a9 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,5 @@ -from flask import Flask, request, jsonify, render_template_string, redirect, url_for -import json +from flask import Flask import os -from datetime import datetime app = Flask(__name__) @@ -12,124 +10,12 @@ if not os.path.exists(ROUTES_DIR): os.makedirs(ROUTES_DIR) -# Load existing data from files -data_store = {} -for filename in os.listdir(ROUTES_DIR): - if filename.endswith('.json'): - with open(os.path.join(ROUTES_DIR, filename), 'r') as f: - key = filename[:-5] # Remove '.json' from filename - data_store[key] = json.load(f) +from routes import home, post_json, get_json, delete_json -# Save data to file -def save_data(key, data): - filepath = os.path.join(ROUTES_DIR, f"{key}.json") - with open(filepath, 'w') as f: - json.dump(data, f, indent=4) - -# Delete data file -def delete_data(key): - filepath = os.path.join(ROUTES_DIR, f"{key}.json") - if os.path.exists(filepath): - os.remove(filepath) - data_store.pop(key, None) - -# Home route to render the form and list all routes -@app.route('/') -def home(): - grouped_routes = {} - for key in data_store: - prefix = key.split('_')[0] - if prefix not in grouped_routes: - grouped_routes[prefix] = [] - grouped_routes[prefix].append(key) - - return render_template_string(''' - - - - - - JSON Input - - - - -
-
-

Post JSON Data

-
- - -
- -

Available Routes

- {% for category, routes in grouped_routes.items() %} -

{{ category | capitalize }}

- - {% endfor %} -
-
- - - ''', grouped_routes=grouped_routes, data_store=data_store) - -# Route to handle JSON data submission -@app.route('/post-json', methods=['POST']) -def post_json(): - try: - json_data = request.form['json_data'] - parsed_json = json.loads(json_data) - - timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') - original_key = list(parsed_json.keys())[0] - key_with_timestamp = f"{original_key}_{timestamp}" - data_store[key_with_timestamp] = parsed_json - - save_data(key_with_timestamp, parsed_json) - - return redirect(url_for('home')) - - except (json.JSONDecodeError, IndexError) as e: - return jsonify({"error": "Invalid JSON"}), 400 - -# Dynamic route to return JSON data -@app.route('/') -def get_json(key): - data = data_store.get(key) - if data: - return jsonify(data) - else: - return jsonify({"error": "Not found"}), 404 - -# Route to handle JSON data deletion -@app.route('/delete-json/', methods=['POST']) -def delete_json(key): - delete_data(key) - return redirect(url_for('home')) +app.add_url_rule('/', 'home', home) +app.add_url_rule('/post-json', 'post_json', post_json, methods=['POST']) +app.add_url_rule('/', 'get_json', get_json) +app.add_url_rule('/delete-json/', 'delete_json', delete_json, methods=['POST']) if __name__ == '__main__': app.run(debug=True) diff --git a/data_store.py b/data_store.py new file mode 100644 index 0000000..760c88e --- /dev/null +++ b/data_store.py @@ -0,0 +1,23 @@ +import json +import os + +ROUTES_DIR = 'routes' +data_store = {} + +# Load existing data from files +for filename in os.listdir(ROUTES_DIR): + if filename.endswith('.json'): + with open(os.path.join(ROUTES_DIR, filename), 'r') as f: + key = filename[:-5] # Remove '.json' from filename + data_store[key] = json.load(f) + +def save_data(key, data): + filepath = os.path.join(ROUTES_DIR, f"{key}.json") + with open(filepath, 'w') as f: + json.dump(data, f, indent=4) + +def delete_data(key): + filepath = os.path.join(ROUTES_DIR, f"{key}.json") + if os.path.exists(filepath): + os.remove(filepath) + data_store.pop(key, None) diff --git a/routes.py b/routes.py new file mode 100644 index 0000000..687cd81 --- /dev/null +++ b/routes.py @@ -0,0 +1,94 @@ +from flask import request, jsonify, render_template_string, redirect, url_for +import json +from datetime import datetime +from data_store import data_store, save_data, delete_data + +def home(): + grouped_routes = {} + for key in data_store: + prefix = key.split('_')[0] + if prefix not in grouped_routes: + grouped_routes[prefix] = [] + grouped_routes[prefix].append(key) + + return render_template_string(''' + + + + + + JSON Input + + + + +
+
+

Post JSON Data

+
+ + +
+ +

Available Routes

+ {% for category, routes in grouped_routes.items() %} +

{{ category | capitalize }}

+
    + {% for route in routes %} +
  • + {{ route }} +
    + +
    +
  • + {% endfor %} +
+ {% endfor %} +
+
+ + + ''', grouped_routes=grouped_routes, data_store=data_store) + +def post_json(): + try: + json_data = request.form['json_data'] + parsed_json = json.loads(json_data) + + timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') + original_key = list(parsed_json.keys())[0] + key_with_timestamp = f"{original_key}_{timestamp}" + data_store[key_with_timestamp] = parsed_json + + save_data(key_with_timestamp, parsed_json) + + return redirect(url_for('home')) + + except (json.JSONDecodeError, IndexError) as e: + return jsonify({"error": "Invalid JSON"}), 400 + +def get_json(key): + data = data_store.get(key) + if data: + return jsonify(data) + else: + return jsonify({"error": "Not found"}), 404 + +def delete_json(key): + delete_data(key) + return redirect(url_for('home'))