2026-03-24 23:06:07 +01:00
|
|
|
from flask import Flask, render_template, request, jsonify
|
|
|
|
|
from led import led
|
2026-03-21 10:53:02 +01:00
|
|
|
import os
|
2026-03-24 23:06:07 +01:00
|
|
|
from add_user import add_user
|
|
|
|
|
import auth
|
|
|
|
|
import re
|
|
|
|
|
|
2026-03-21 10:53:02 +01:00
|
|
|
app = Flask(__name__)
|
2026-03-24 23:06:07 +01:00
|
|
|
|
|
|
|
|
current_user = None
|
|
|
|
|
|
2026-03-21 10:53:02 +01:00
|
|
|
@app.route("/")
|
|
|
|
|
def index():
|
2026-03-24 23:06:07 +01:00
|
|
|
return render_template("index.html")
|
2026-03-21 10:53:02 +01:00
|
|
|
|
|
|
|
|
@app.route("/login", methods=["POST"])
|
|
|
|
|
def login():
|
2026-03-24 23:06:07 +01:00
|
|
|
global current_user
|
2026-03-21 10:53:02 +01:00
|
|
|
data = request.get_json()
|
|
|
|
|
succes = auth.login(data["username"], data["password"])
|
|
|
|
|
if succes:
|
2026-03-24 23:06:07 +01:00
|
|
|
current_user = data["username"]
|
|
|
|
|
return jsonify({"success": True})
|
2026-03-21 10:53:02 +01:00
|
|
|
else:
|
2026-03-24 23:06:07 +01:00
|
|
|
return jsonify({"success": False})
|
|
|
|
|
|
2026-03-21 10:53:02 +01:00
|
|
|
@app.route("/dashboard")
|
|
|
|
|
def dashboard():
|
2026-03-24 23:06:07 +01:00
|
|
|
return render_template("dashboard.html")
|
2026-03-21 10:53:02 +01:00
|
|
|
|
2026-03-24 23:06:07 +01:00
|
|
|
@app.route("/led", methods=["POST"])
|
2026-03-21 10:53:02 +01:00
|
|
|
def call_led():
|
2026-03-24 23:06:07 +01:00
|
|
|
led(current_user)
|
|
|
|
|
return jsonify({"success": True})
|
|
|
|
|
|
|
|
|
|
@app.route("/admin")
|
|
|
|
|
def admin_page():
|
|
|
|
|
return render_template("admin.html")
|
|
|
|
|
|
|
|
|
|
@app.route("/admin/logs")
|
|
|
|
|
def logs_page():
|
|
|
|
|
return render_template("log.html")
|
2026-03-21 10:53:02 +01:00
|
|
|
|
2026-03-24 23:06:07 +01:00
|
|
|
|
|
|
|
|
@app.route("/admin/logs/data")
|
|
|
|
|
def get_logs():
|
|
|
|
|
try:
|
|
|
|
|
with open('/var/log/loustique.log', 'r') as f:
|
|
|
|
|
lines = f.readlines()
|
|
|
|
|
ansi_escape = re.compile(r'\x1b\[[0-9;]*m')
|
|
|
|
|
lines = [ansi_escape.sub('', line) for line in lines[-200:]]
|
|
|
|
|
return jsonify({"success": True, "logs": lines})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({"success": False, "message": str(e)})
|
|
|
|
|
|
|
|
|
|
@app.route("/admin/add_user",methods=["POST"])
|
|
|
|
|
@app.route("/admin/add_user", methods=["POST"])
|
|
|
|
|
def create_user():
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
succes = add_user(data["username"], data["password"], data["role"])
|
|
|
|
|
if succes:
|
|
|
|
|
return jsonify({"success": True})
|
|
|
|
|
else:
|
|
|
|
|
return jsonify({"success": False, "message": "Utilisateur déjà existant"})
|
|
|
|
|
@app.route("/admin/get_users")
|
|
|
|
|
def get_users():
|
|
|
|
|
users = auth.get_users()
|
|
|
|
|
return jsonify({"success": True, "users": users})
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
app.run(host="0.0.0.0", port=5000)
|