commit 257b4312e526dbe2828940d00e3888203f348a62 Author: Santiago Lo Coco Date: Sun Oct 20 15:46:47 2024 +0200 Add API and webapp diff --git a/.env.dev b/.env.dev new file mode 100644 index 0000000..f610df0 --- /dev/null +++ b/.env.dev @@ -0,0 +1,2 @@ +FLASK_SECRET_KEY=your_secret_key +DATABASE=sqlite:///endpoints.db \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a97a307 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.env +.venv +__pycache__/ +*.py[cod] +*$py.class + diff --git a/app.py b/app.py new file mode 100644 index 0000000..03684d0 --- /dev/null +++ b/app.py @@ -0,0 +1,46 @@ +import os +from flask import Flask, render_template, request, redirect, url_for, jsonify, abort +from db import * +# from routes import register_routes +from dotenv import load_dotenv + +load_dotenv() +DATABASE_URI = os.getenv('DATABASE') + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +init_db(app) + +@app.route('/') +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 new file mode 100644 index 0000000..f853b94 --- /dev/null +++ b/db.py @@ -0,0 +1,43 @@ +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()} + + # 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) + +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) + if endpoint: + endpoint.url = 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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a65ce4c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +Flask==3.0.3 +waitress==3.0.0 +python-dotenv==1.0.1 +SQLAlchemy==2.0.36 +Flask-SQLAlchemy==3.1.1 \ No newline at end of file diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..d50d8f8 --- /dev/null +++ b/run.bat @@ -0,0 +1,6 @@ +@echo off + +python -m venv .venv +call .venv\Scripts\activate +pip install -r requirements.txt +python app.py \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..4210f5a --- /dev/null +++ b/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +python -m venv .venv +source .venv/bin/activate +pip install -r requirements.txt +python app.py \ No newline at end of file diff --git a/templates/index copy 2.html b/templates/index copy 2.html new file mode 100644 index 0000000..1804962 --- /dev/null +++ b/templates/index copy 2.html @@ -0,0 +1,174 @@ + + + + + + Endpoints manager + + + + + +

Endpoints Manager

+ + + + + + + + + + + {% for id, url in endpoints.items() %} + + + + + + {% endfor %} + +
IDURLAction
{{ id }}{{ url }} + + +
+ +

cURL Commands

+
+# Get all endpoints
+curl http://windows.local:5000/api/endpoints
+
+# Get Endpoint 1
+curl http://windows.local:5000/api/endpoints/1
+
+# Get Endpoint 2
+curl http://windows.local:5000/api/endpoints/2
+
+# Update Endpoint 1
+curl -X PUT http://windows.local:5000/api/endpoints/1 -H "Content-Type: application/json" -d '{"url": "http://new.url/for/endpoint1"}'
+
+# Update Endpoint 2
+curl -X PUT http://windows.local:5000/api/endpoints/2 -H "Content-Type: application/json" -d '{"url": "http://new.url/for/endpoint2"}'
+
+ + + + + diff --git a/templates/index copy.html b/templates/index copy.html new file mode 100644 index 0000000..25b4ae5 --- /dev/null +++ b/templates/index copy.html @@ -0,0 +1,108 @@ + + + + + + Endpoints Manager + + + + + +

Endpoints Manager

+ + + + + + + + + + + {% for id, url in endpoints.items() %} + + + + + + {% endfor %} + +
IDURLAction
{{ id }}{{ url }} + + +
+ +

cURL Commands

+
+# Get all endpoints
+curl http://windows.local:5000/api/endpoints
+
+# Get Endpoint 1
+curl http://windows.local:5000/api/endpoints/1
+
+# Get Endpoint 2
+curl http://windows.local:5000/api/endpoints/2
+
+# Update Endpoint 1
+curl -X PUT http://windows.local:5000/api/endpoints/1 -H "Content-Type: application/json" -d '{"url": "http://new.url/for/endpoint1"}'
+
+# Update Endpoint 2
+curl -X PUT http://windows.local:5000/api/endpoints/2 -H "Content-Type: application/json" -d '{"url": "http://new.url/for/endpoint2"}'
+
+ + + + + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..ee15cfd --- /dev/null +++ b/templates/index.html @@ -0,0 +1,173 @@ + + + + + + Endpoints manager + + + + + +

Endpoints manager

+ + + + + + + + + + + {% for endpoint in endpoints %} + + + + + + {% endfor %} + +
IDURLAction
{{ endpoint.id }}{{ endpoint.url }} +
+ + +
+
+ +

cURL Commands

+
+# Get all endpoints
+curl http://windows.local:5000/api/endpoints
+
+# Get Endpoint 1
+curl http://windows.local:5000/api/endpoints/1
+
+# Get Endpoint 2
+curl http://windows.local:5000/api/endpoints/2
+
+# Update Endpoint 1
+curl -X PUT http://windows.local:5000/api/endpoints/1 -H "Content-Type: application/json" -d '{"url": "http://new.url/for/endpoint1"}'
+
+# Update Endpoint 2
+curl -X PUT http://windows.local:5000/api/endpoints/2 -H "Content-Type: application/json" -d '{"url": "http://new.url/for/endpoint2"}'
+
+ + + + +