diff --git a/api.py b/api.py new file mode 100644 index 0000000..d04a7c2 --- /dev/null +++ b/api.py @@ -0,0 +1,28 @@ +from flask import Flask, request, jsonify, abort, Blueprint +from db import get_endpoint_by_id, get_endpoints_from_db, update_endpoint_in_db + +api = Blueprint('api', __name__) + +@api.route('/api/endpoints', methods=['GET']) +def get_endpoints(): + endpoints = get_endpoints_from_db() + return jsonify(endpoints) + +@api.route('/api/endpoints/', methods=['GET']) +def get_endpoint(endpoint_id): + endpoint = get_endpoint_by_id(endpoint_id) + if endpoint: + return jsonify(endpoint) + else: + abort(404) + +@api.route('/api/endpoints/', methods=['PUT']) +def update_endpoint(endpoint_id): + data = request.get_json() + new_url = data.get('url') + + if new_url: + update_endpoint_in_db(endpoint_id, new_url) + return jsonify({"status": "success", "message": f"Endpoint {endpoint_id} updated to {new_url}"}), 200 + else: + return jsonify({"status": "error", "message": "No new endpoint provided."}), 400 diff --git a/app.py b/app.py index 03684d0..220f965 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ import os -from flask import Flask, render_template, request, redirect, url_for, jsonify, abort -from db import * -# from routes import register_routes +from flask import Flask, render_template +from db import init_db, get_endpoints_from_db +from api import api from dotenv import load_dotenv load_dotenv() @@ -10,6 +10,7 @@ DATABASE_URI = os.getenv('DATABASE') app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.register_blueprint(api) init_db(app) @@ -18,29 +19,5 @@ def homepage(): endpoints = get_endpoints_from_db() return render_template('index.html', endpoints=endpoints) -@app.route('/api/endpoints', methods=['GET']) -def get_endpoints(): - endpoints = get_endpoints_from_db() - return jsonify(endpoints) - -@app.route('/api/endpoints/', methods=['GET']) -def get_endpoint(endpoint_id): - endpoint = get_endpoint_by_id(endpoint_id) - if endpoint: - return jsonify(endpoint) - else: - abort(404) - -@app.route('/api/endpoints/', methods=['PUT']) -def update_endpoint(endpoint_id): - data = request.get_json() - new_url = data.get('url') - - if new_url: - update_endpoint_in_db(endpoint_id, new_url) - return jsonify({"status": "success", "message": f"Endpoint {endpoint_id} updated to {new_url}"}), 200 - else: - return jsonify({"status": "error", "message": "No new endpoint provided."}), 400 - if __name__ == '__main__': app.run(host='windows.local', port=5000) diff --git a/db.py b/db.py index f853b94..943652f 100644 --- a/db.py +++ b/db.py @@ -15,13 +15,11 @@ def init_db(app): def init_default_endpoints(): existing_endpoints = {endpoint.id for endpoint in Endpoint.query.all()} - # Define default endpoints default_endpoints = [ Endpoint(id="1", url="http://windows.local:8100/mystream/"), Endpoint(id="2", url="http://windows.local:8200/mystream/") ] - # with db.session.begin(): for endpoint in default_endpoints: if endpoint.id not in existing_endpoints: db.session.add(endpoint)