Files
infoscreen/server/wsgi.py
olaf 628a3b1fe9 introduce nginex-proxy
use host ip if working in wsl
2025-06-10 22:55:13 +02:00

67 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# server/wsgi.py
import glob
import os
from flask import Flask, jsonify, send_from_directory
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from models import Client, Base
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_HOST = os.getenv("DB_HOST")
DB_NAME = os.getenv("DB_NAME")
# Datenbank-Engine und Session anlegen (passe ggf. die DB-URL an)
DB_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}"
engine = create_engine(DB_URL, echo=False)
Session = sessionmaker(bind=engine)
app = Flask(__name__)
@app.route("/health")
def health():
return jsonify(status="ok")
# Optional: Test-Route
@app.route("/")
def index():
return "Hello from InfoscreenAPI!"
# (Weitere Endpunkte, Blueprints, Datenbank-Initialisierung usw. kommen hierher)
@app.route("/screenshots/<uuid>")
def get_screenshot(uuid):
"""Liefert den aktuellen Screenshot für die angegebene UUID zurück."""
print(f"Anfrage für Screenshot mit UUID: {uuid}")
pattern = os.path.join("screenshots", f"{uuid}*.jpg")
files = glob.glob(pattern)
if not files:
return jsonify({"error": "Screenshot not found"}), 404
# Es gibt nur eine Datei pro UUID
filename = os.path.basename(files[0])
print(filename)
print("Arbeitsverzeichnis:", os.getcwd())
print("Suchmuster:", pattern)
print("Gefundene Dateien:", files)
return send_from_directory("screenshots", filename)
@app.route("/api/clients")
def get_clients():
# from models import Client # Import lokal, da im selben Container
print("Abrufen der Clients aus der Datenbank...")
session = Session()
clients = session.query(Client).all()
result = [
{
"uuid": c.uuid,
"location": c.location,
"hardware_hash": c.hardware_hash,
"ip_address": c.ip_address,
"last_alive": c.last_alive.isoformat() if c.last_alive else None
}
for c in clients
]
session.close()
return jsonify(result)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)