Reformat code
This commit is contained in:
parent
de72f32c6b
commit
8468506acb
|
@ -1,6 +1,6 @@
|
|||
services:
|
||||
flask-api:
|
||||
build:
|
||||
build:
|
||||
context: ../src
|
||||
dockerfile: ../docker/Dockerfile
|
||||
ports:
|
||||
|
|
2
run.sh
2
run.sh
|
@ -3,4 +3,4 @@
|
|||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -r src/requirements.txt
|
||||
python src/app.py
|
||||
python src/app.py
|
||||
|
|
26
src/api.py
26
src/api.py
|
@ -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
|
||||
|
||||
api = Blueprint('api', __name__)
|
||||
api = Blueprint("api", __name__)
|
||||
|
||||
@api.route('/api/endpoints', methods=['GET'])
|
||||
|
||||
@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'])
|
||||
|
||||
@api.route("/api/endpoints/<int:endpoint_id>", methods=["GET"])
|
||||
def get_endpoint(endpoint_id):
|
||||
endpoint = get_endpoint_by_id(endpoint_id)
|
||||
if endpoint:
|
||||
|
@ -16,13 +19,22 @@ def get_endpoint(endpoint_id):
|
|||
else:
|
||||
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):
|
||||
data = request.get_json()
|
||||
new_url = data.get('url')
|
||||
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
|
||||
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
|
||||
|
|
26
src/app.py
26
src/app.py
|
@ -1,25 +1,29 @@
|
|||
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 flask import Flask, render_template
|
||||
|
||||
from api import api
|
||||
from db import get_endpoints_from_db, init_db
|
||||
|
||||
load_dotenv()
|
||||
|
||||
DATABASE_URI = os.getenv('DATABASE')
|
||||
API_HOST = os.getenv('API_HOST')
|
||||
DATABASE_URI = os.getenv("DATABASE")
|
||||
API_HOST = os.getenv("API_HOST")
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
app.register_blueprint(api)
|
||||
|
||||
init_db(app)
|
||||
|
||||
@app.route('/')
|
||||
|
||||
@app.route("/")
|
||||
def homepage():
|
||||
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)
|
||||
|
|
|
@ -2,22 +2,25 @@ from flask_sqlalchemy import SQLAlchemy
|
|||
|
||||
db = SQLAlchemy()
|
||||
|
||||
|
||||
class Endpoint(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
url = db.Column(db.String, nullable=False)
|
||||
|
||||
|
||||
def init_db(app):
|
||||
db.init_app(app)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
init_default_endpoints()
|
||||
|
||||
|
||||
def init_default_endpoints():
|
||||
existing_endpoints = {endpoint.id for endpoint in Endpoint.query.all()}
|
||||
|
||||
default_endpoints = [
|
||||
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
|
||||
|
@ -29,10 +32,12 @@ def init_default_endpoints():
|
|||
if updated:
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def get_endpoints_from_db():
|
||||
endpoints = Endpoint.query.all()
|
||||
return [{"id": endpoint.id, "url": endpoint.url} for endpoint in endpoints]
|
||||
|
||||
|
||||
def update_endpoint_in_db(endpoint_id, new_url):
|
||||
with db.session.begin():
|
||||
endpoint = Endpoint.query.get(endpoint_id)
|
||||
|
@ -41,6 +46,7 @@ def update_endpoint_in_db(endpoint_id, new_url):
|
|||
else:
|
||||
db.session.add(Endpoint(id=endpoint_id, url=new_url))
|
||||
|
||||
|
||||
def get_endpoint_by_id(endpoint_id):
|
||||
endpoint = Endpoint.query.get(int(endpoint_id))
|
||||
return {"id": endpoint.id, "url": endpoint.url} if endpoint else None
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
[flake8]
|
||||
max-line-length = 110
|
Loading…
Reference in New Issue