mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-02-19 17:06:37 +00:00
Update flask_server.py
This commit is contained in:
@@ -15,14 +15,14 @@ import logging
|
|||||||
from flask import Flask, jsonify, send_file, send_from_directory
|
from flask import Flask, jsonify, send_file, send_from_directory
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
|
||||||
# --- Importar Blueprints Existentes (Los que ya tenías y no hemos tocado) ---
|
# --- Importar Blueprints Existentes ---
|
||||||
from flask_auth_routes import auth_bp
|
from flask_auth_routes import auth_bp
|
||||||
from flask_health_routes import health_bp
|
from flask_health_routes import health_bp
|
||||||
from flask_proxmenux_routes import proxmenux_bp
|
from flask_proxmenux_routes import proxmenux_bp
|
||||||
from flask_terminal_routes import terminal_bp, init_terminal_routes
|
from flask_terminal_routes import init_terminal_routes
|
||||||
|
# Nota: No importamos terminal_bp aquí porque init_terminal_routes ya lo registra
|
||||||
|
|
||||||
# --- Importar Nuevos Blueprints (Los 5 módulos que acabas de crear) ---
|
# --- Importar Nuevos Blueprints ---
|
||||||
# Asegúrate de que los archivos .py se llamen exactamente así para que funcionen los imports:
|
|
||||||
from flask_system_routes import system_bp
|
from flask_system_routes import system_bp
|
||||||
from flask_storage_routes import storage_bp
|
from flask_storage_routes import storage_bp
|
||||||
from flask_network_routes import network_bp
|
from flask_network_routes import network_bp
|
||||||
@@ -39,42 +39,40 @@ logger = logging.getLogger("proxmenux.server")
|
|||||||
|
|
||||||
# Inicializar Flask
|
# Inicializar Flask
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app) # Habilitar CORS para permitir peticiones desde el frontend
|
CORS(app) # Habilitar CORS
|
||||||
|
|
||||||
# -------------------------------------------------------------------
|
# -------------------------------------------------------------------
|
||||||
# Registro de Módulos (Blueprints)
|
# Registro de Módulos (Blueprints)
|
||||||
# Aquí es donde "conectamos" todos los archivos separados a la app principal
|
|
||||||
# -------------------------------------------------------------------
|
# -------------------------------------------------------------------
|
||||||
|
|
||||||
# 1. Módulos de Utilidad y Autenticación
|
# 1. Módulos de Utilidad y Autenticación
|
||||||
app.register_blueprint(auth_bp)
|
app.register_blueprint(auth_bp)
|
||||||
app.register_blueprint(health_bp)
|
app.register_blueprint(health_bp)
|
||||||
app.register_blueprint(proxmenux_bp)
|
app.register_blueprint(proxmenux_bp)
|
||||||
app.register_blueprint(terminal_bp)
|
# ELIMINADO: app.register_blueprint(terminal_bp) -> Se registra dentro de init_terminal_routes()
|
||||||
|
|
||||||
# 2. Módulos Principales de Monitorización (Nuevos)
|
# 2. Módulos Principales de Monitorización
|
||||||
app.register_blueprint(system_bp) # Rutas: /api/system, /api/logs, /api/events
|
app.register_blueprint(system_bp) # /api/system, /api/logs
|
||||||
app.register_blueprint(storage_bp) # Rutas: /api/storage, /api/backups
|
app.register_blueprint(storage_bp) # /api/storage, /api/backups
|
||||||
app.register_blueprint(network_bp) # Rutas: /api/network
|
app.register_blueprint(network_bp) # /api/network
|
||||||
app.register_blueprint(vm_bp) # Rutas: /api/vms
|
app.register_blueprint(vm_bp) # /api/vms
|
||||||
app.register_blueprint(hardware_bp) # Rutas: /api/hardware, /api/gpu
|
app.register_blueprint(hardware_bp) # /api/hardware, /api/gpu
|
||||||
app.register_blueprint(script_bp) # Rutas: /api/scripts
|
app.register_blueprint(script_bp) # /api/scripts
|
||||||
|
|
||||||
# Inicializar WebSocket para la terminal y ejecución de scripts
|
# Inicializar WebSocket para la terminal y ejecución de scripts
|
||||||
|
# Esta función registra el blueprint 'terminal' internamente
|
||||||
init_terminal_routes(app)
|
init_terminal_routes(app)
|
||||||
|
|
||||||
# -------------------------------------------------------------------
|
# -------------------------------------------------------------------
|
||||||
# Rutas del Frontend (Servir Dashboard Next.js estático)
|
# Rutas del Frontend
|
||||||
# -------------------------------------------------------------------
|
# -------------------------------------------------------------------
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def serve_dashboard():
|
def serve_dashboard():
|
||||||
"""Sirve la página principal (index.html) del dashboard."""
|
"""Sirve la página principal (index.html) del dashboard."""
|
||||||
try:
|
try:
|
||||||
# Detectar la raíz del AppImage o directorio actual
|
|
||||||
appimage_root = os.environ.get('APPDIR')
|
appimage_root = os.environ.get('APPDIR')
|
||||||
if not appimage_root:
|
if not appimage_root:
|
||||||
# Fallback para desarrollo: detectar ruta relativa
|
|
||||||
base_dir = os.path.dirname(os.path.abspath(__file__))
|
base_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
if base_dir.endswith('usr/bin'):
|
if base_dir.endswith('usr/bin'):
|
||||||
appimage_root = os.path.dirname(os.path.dirname(base_dir))
|
appimage_root = os.path.dirname(os.path.dirname(base_dir))
|
||||||
@@ -86,12 +84,10 @@ def serve_dashboard():
|
|||||||
if os.path.exists(index_path):
|
if os.path.exists(index_path):
|
||||||
return send_file(index_path)
|
return send_file(index_path)
|
||||||
|
|
||||||
# Si no encuentra el dashboard, mostrar mensaje de error útil en el navegador
|
|
||||||
return f"""
|
return f"""
|
||||||
<html><body style="background:#111;color:#eee;font-family:sans-serif;padding:2rem;">
|
<html><body style="background:#111;color:#eee;font-family:sans-serif;padding:2rem;">
|
||||||
<h1>ProxMenux Monitor</h1>
|
<h1>ProxMenux Monitor</h1>
|
||||||
<p>Dashboard not found at: {index_path}</p>
|
<p>Dashboard not found at: {index_path}</p>
|
||||||
<p>The API is running correctly. Check your AppImage build structure.</p>
|
|
||||||
</body></html>
|
</body></html>
|
||||||
""", 404
|
""", 404
|
||||||
|
|
||||||
@@ -100,7 +96,7 @@ def serve_dashboard():
|
|||||||
|
|
||||||
@app.route('/_next/<path:filename>')
|
@app.route('/_next/<path:filename>')
|
||||||
def serve_next_static(filename):
|
def serve_next_static(filename):
|
||||||
"""Sirve archivos estáticos de Next.js (_next/static/...)."""
|
"""Sirve archivos estáticos de Next.js."""
|
||||||
try:
|
try:
|
||||||
appimage_root = os.environ.get('APPDIR')
|
appimage_root = os.environ.get('APPDIR')
|
||||||
if not appimage_root:
|
if not appimage_root:
|
||||||
@@ -134,7 +130,7 @@ def serve_images(filename):
|
|||||||
|
|
||||||
@app.route('/<path:filename>')
|
@app.route('/<path:filename>')
|
||||||
def serve_static_files(filename):
|
def serve_static_files(filename):
|
||||||
"""Sirve archivos raíz (favicon, manifest, etc.)."""
|
"""Sirve archivos raíz."""
|
||||||
try:
|
try:
|
||||||
appimage_root = os.environ.get('APPDIR')
|
appimage_root = os.environ.get('APPDIR')
|
||||||
if not appimage_root:
|
if not appimage_root:
|
||||||
@@ -151,29 +147,23 @@ def serve_static_files(filename):
|
|||||||
|
|
||||||
@app.route('/api/info', methods=['GET'])
|
@app.route('/api/info', methods=['GET'])
|
||||||
def api_info():
|
def api_info():
|
||||||
"""Endpoint raíz de la API para verificar funcionamiento y listar endpoints disponibles."""
|
"""Endpoint raíz de la API."""
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'name': 'ProxMenux Monitor API',
|
'name': 'ProxMenux Monitor API',
|
||||||
'version': '1.0.3 (Modular)',
|
'version': '1.0.3 (Modular)',
|
||||||
'status': 'online',
|
'status': 'online',
|
||||||
'endpoints': [
|
'endpoints': [
|
||||||
'/api/system',
|
'/api/system', '/api/storage', '/api/network',
|
||||||
'/api/storage',
|
'/api/vms', '/api/hardware', '/api/gpu/realtime'
|
||||||
'/api/network',
|
|
||||||
'/api/vms',
|
|
||||||
'/api/hardware',
|
|
||||||
'/api/gpu/realtime'
|
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Silenciar banner de Flask CLI para mantener logs limpios en la consola
|
|
||||||
import sys
|
import sys
|
||||||
try:
|
try:
|
||||||
cli = sys.modules['flask.cli']
|
cli = sys.modules['flask.cli']
|
||||||
cli.show_server_banner = lambda *x: None
|
cli.show_server_banner = lambda *x: None
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
# Iniciar servidor en el puerto 8008
|
|
||||||
print("🚀 ProxMenux Monitor API (Modular) running on port 8008...")
|
print("🚀 ProxMenux Monitor API (Modular) running on port 8008...")
|
||||||
app.run(host='0.0.0.0', port=8008, debug=False)
|
app.run(host='0.0.0.0', port=8008, debug=False)
|
||||||
Reference in New Issue
Block a user