Use Blueprints

This commit is contained in:
Santiago Lo Coco 2024-10-20 16:00:58 +02:00
parent daebf53be6
commit 7f52d4e2a8
3 changed files with 32 additions and 29 deletions

28
api.py Normal file
View File

@ -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/<int:endpoint_id>', 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/<int:endpoint_id>', 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

31
app.py
View File

@ -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/<int:endpoint_id>', 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/<int:endpoint_id>', 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)

2
db.py
View File

@ -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)