3lips/api/main.py

97 lines
3 KiB
Python
Raw Normal View History

2024-02-01 14:50:24 +00:00
# main.py
2024-02-05 13:43:59 +00:00
from flask import Flask, Response, render_template, request, redirect, jsonify, send_from_directory
2024-02-01 14:50:24 +00:00
import os
import sqlite3
2024-02-05 13:43:59 +00:00
import requests
import time
import socket
from util.Sqlite import Sqlite
2024-02-01 14:50:24 +00:00
app = Flask(__name__)
# init db
# sqlite = Sqlite('./db/3lips.db')
# schema = "api TEXT PRIMARY KEY, timestamp INTEGER NOT NULL"
# sqlite.create_table("data", schema)
2024-02-01 14:50:24 +00:00
# store state data
servers = [
{"name": "radar4", "url": "radar4.30hours.dev"},
{"name": "radar5", "url": "radar5.30hours.dev"}
]
associators = [
{"name": "ADSB Associator", "id": "adsb-associator"}
]
2024-02-07 12:44:56 +00:00
# coordregs = [
# {"name": "Ellipse Analytic Intersection", "id": "ellipse-conic-int"},
# {"name": "Ellipse Parametric", "id": "ellipse-parametric"},
# {"name": "Ellipse Parametric (Arc Length)", "id": "ellipse-parametric-arc"},
# {"name": "Ellipsoid Parametric", "id": "ellipsoid-parametric"},
# {"name": "Ellipsoid Parametric (Arc Length)", "id": "ellipsoid-parametric-arc"}
# ]
2024-02-01 14:50:24 +00:00
coordregs = [
2024-02-07 12:44:56 +00:00
{"name": "Ellipse Parametric", "id": "ellipse-parametric"}
2024-02-04 10:47:40 +00:00
]
adsbs = [
{"name": "adsb.30hours.dev", "url": "adsb.30hours.dev"},
{"name": "None", "url": ""}
2024-02-01 14:50:24 +00:00
]
@app.route("/")
def index():
return render_template("index.html", servers=servers, \
2024-02-04 10:47:40 +00:00
associators=associators, coordregs=coordregs, adsbs=adsbs)
2024-02-01 14:50:24 +00:00
# serve static files from the /app/public folder
@app.route('/public/<path:file>')
def serve_static(file):
base_dir = os.path.abspath(os.path.dirname(__file__))
public_folder = os.path.join(base_dir, 'public')
return send_from_directory(public_folder, file)
@app.route("/api")
def api():
api = request.query_string.decode('utf-8')
# timestamp = time.time()*1000
# sqlite.add_entry("data", api, timestamp)
send_data_to_event(api)
2024-02-01 14:50:24 +00:00
urls = request.args.getlist("url")
2024-02-04 10:47:40 +00:00
data = [{"url": 'http://' + url} for url in urls]
2024-02-01 14:50:24 +00:00
return jsonify(data)
2024-02-04 12:08:48 +00:00
@app.route("/map/<path:file>")
def serve_map(file):
base_dir = os.path.abspath(os.path.dirname(__file__))
public_folder = os.path.join(base_dir, 'map')
return send_from_directory(public_folder, file)
2024-02-01 14:50:24 +00:00
# handle /cesium/ specifically
2024-02-05 13:43:59 +00:00
@app.route('/cesium/')
def serve_cesium_index():
return redirect('/cesium/index.html')
@app.route('/cesium/<path:file>')
def serve_cesium_content(file):
apache_url = 'http://cesium-apache/' + file
try:
response = requests.get(apache_url)
if response.status_code == 200:
return Response(response.content, content_type=response.headers['content-type'])
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error fetching content from Apache server: {e}")
return Response('Error fetching content from Apache server', status=500, content_type='text/plain')
def send_data_to_event(data):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# Use the service name 'event' as specified in your Docker Compose file
s.connect(('event', 12345))
s.sendall(data.encode())
2024-02-01 14:50:24 +00:00
if __name__ == "__main__":
app.run(debug=True)