Reformat code

This commit is contained in:
Santiago Lo Coco 2024-10-20 22:57:18 +02:00
parent de72f32c6b
commit 8468506acb
6 changed files with 45 additions and 21 deletions

View File

@ -1,6 +1,6 @@
services: services:
flask-api: flask-api:
build: build:
context: ../src context: ../src
dockerfile: ../docker/Dockerfile dockerfile: ../docker/Dockerfile
ports: ports:

2
run.sh
View File

@ -3,4 +3,4 @@
python -m venv .venv python -m venv .venv
source .venv/bin/activate source .venv/bin/activate
pip install -r src/requirements.txt pip install -r src/requirements.txt
python src/app.py python src/app.py

View File

@ -1,14 +1,17 @@
from flask import Flask, request, jsonify, abort, Blueprint from flask import Blueprint, abort, jsonify, request
from db import get_endpoint_by_id, get_endpoints_from_db, update_endpoint_in_db from db import get_endpoint_by_id, get_endpoints_from_db, update_endpoint_in_db
api = Blueprint('api', __name__) api = Blueprint("api", __name__)
@api.route('/api/endpoints', methods=['GET'])
@api.route("/api/endpoints", methods=["GET"])
def get_endpoints(): def get_endpoints():
endpoints = get_endpoints_from_db() endpoints = get_endpoints_from_db()
return jsonify(endpoints) return jsonify(endpoints)
@api.route('/api/endpoints/<int:endpoint_id>', methods=['GET'])
@api.route("/api/endpoints/<int:endpoint_id>", methods=["GET"])
def get_endpoint(endpoint_id): def get_endpoint(endpoint_id):
endpoint = get_endpoint_by_id(endpoint_id) endpoint = get_endpoint_by_id(endpoint_id)
if endpoint: if endpoint:
@ -16,13 +19,22 @@ def get_endpoint(endpoint_id):
else: else:
abort(404) abort(404)
@api.route('/api/endpoints/<int:endpoint_id>', methods=['PUT'])
@api.route("/api/endpoints/<int:endpoint_id>", methods=["PUT"])
def update_endpoint(endpoint_id): def update_endpoint(endpoint_id):
data = request.get_json() data = request.get_json()
new_url = data.get('url') new_url = data.get("url")
if new_url: if new_url:
update_endpoint_in_db(endpoint_id, new_url) update_endpoint_in_db(endpoint_id, new_url)
return jsonify({"status": "success", "message": f"Endpoint {endpoint_id} updated to {new_url}"}), 200 return (
jsonify(
{
"status": "success",
"message": f"Endpoint {endpoint_id} updated to {new_url}",
}
),
200,
)
else: else:
return jsonify({"status": "error", "message": "No new endpoint provided."}), 400 return jsonify({"status": "error", "message": "No new endpoint provided."}), 400

View File

@ -1,25 +1,29 @@
import os import os
from flask import Flask, render_template
from db import init_db, get_endpoints_from_db
from api import api
from dotenv import load_dotenv from dotenv import load_dotenv
from flask import Flask, render_template
from api import api
from db import get_endpoints_from_db, init_db
load_dotenv() load_dotenv()
DATABASE_URI = os.getenv('DATABASE') DATABASE_URI = os.getenv("DATABASE")
API_HOST = os.getenv('API_HOST') API_HOST = os.getenv("API_HOST")
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.register_blueprint(api) app.register_blueprint(api)
init_db(app) init_db(app)
@app.route('/')
@app.route("/")
def homepage(): def homepage():
endpoints = get_endpoints_from_db() endpoints = get_endpoints_from_db()
return render_template('index.html', endpoints=endpoints, api_host=API_HOST) return render_template("index.html", endpoints=endpoints, api_host=API_HOST)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000) if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)

View File

@ -2,22 +2,25 @@ from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() db = SQLAlchemy()
class Endpoint(db.Model): class Endpoint(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
url = db.Column(db.String, nullable=False) url = db.Column(db.String, nullable=False)
def init_db(app): def init_db(app):
db.init_app(app) db.init_app(app)
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
init_default_endpoints() init_default_endpoints()
def init_default_endpoints(): def init_default_endpoints():
existing_endpoints = {endpoint.id for endpoint in Endpoint.query.all()} existing_endpoints = {endpoint.id for endpoint in Endpoint.query.all()}
default_endpoints = [ default_endpoints = [
Endpoint(id=1, url="http://windows.local:8100/mystream/"), Endpoint(id=1, url="http://windows.local:8100/mystream/"),
Endpoint(id=2, url="http://windows.local:8200/mystream/") Endpoint(id=2, url="http://windows.local:8200/mystream/"),
] ]
updated = False updated = False
@ -29,10 +32,12 @@ def init_default_endpoints():
if updated: if updated:
db.session.commit() db.session.commit()
def get_endpoints_from_db(): def get_endpoints_from_db():
endpoints = Endpoint.query.all() endpoints = Endpoint.query.all()
return [{"id": endpoint.id, "url": endpoint.url} for endpoint in endpoints] return [{"id": endpoint.id, "url": endpoint.url} for endpoint in endpoints]
def update_endpoint_in_db(endpoint_id, new_url): def update_endpoint_in_db(endpoint_id, new_url):
with db.session.begin(): with db.session.begin():
endpoint = Endpoint.query.get(endpoint_id) endpoint = Endpoint.query.get(endpoint_id)
@ -41,6 +46,7 @@ def update_endpoint_in_db(endpoint_id, new_url):
else: else:
db.session.add(Endpoint(id=endpoint_id, url=new_url)) db.session.add(Endpoint(id=endpoint_id, url=new_url))
def get_endpoint_by_id(endpoint_id): def get_endpoint_by_id(endpoint_id):
endpoint = Endpoint.query.get(int(endpoint_id)) endpoint = Endpoint.query.get(int(endpoint_id))
return {"id": endpoint.id, "url": endpoint.url} if endpoint else None return {"id": endpoint.id, "url": endpoint.url} if endpoint else None

2
src/setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[flake8]
max-line-length = 110