3lips/api/main.py

95 lines
2.8 KiB
Python
Raw Normal View History

"""
@file api.py
@brief API for 3lips.
@author 30hours
"""
2024-02-01 14:50:24 +00:00
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
2024-02-05 13:43:59 +00:00
import requests
import time
2024-02-09 14:47:13 +00:00
import asyncio
2024-02-09 14:47:13 +00:00
from common.Message import Message
2024-02-01 14:50:24 +00:00
app = Flask(__name__)
# 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-19 09:27:05 +00:00
{"name": "Ellipsoid Parametric", "id": "ellipsoid-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
]
2024-02-10 02:18:08 +00:00
# message received callback
async def callback_message_received(msg):
print(f"Callback: Received message in main.py: {msg}", flush=True)
2024-02-09 14:47:13 +00:00
# init messaging
2024-02-10 02:18:08 +00:00
message_api_request = Message('event', 6969)
2024-02-09 14:47:13 +00:00
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')
2024-02-27 01:49:10 +00:00
try:
reply = message_api_request.send_message(api)
except Exception as e:
reply = "Exception: " + str(e)
2024-02-10 02:18:08 +00:00
print(reply, flush=True)
return reply
2024-02-01 14:50:24 +00:00
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')
2024-02-01 14:50:24 +00:00
if __name__ == "__main__":
2024-02-10 04:19:08 +00:00
app.run()