173 lines
5.8 KiB
Python
173 lines
5.8 KiB
Python
# backend/app.py
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add the parent directory to Python path
|
|
backend_dir = Path(__file__).parent.parent
|
|
if str(backend_dir) not in sys.path:
|
|
sys.path.append(str(backend_dir))
|
|
|
|
from flask import Flask, render_template, jsonify, request
|
|
from core.directory_handler import select_directory
|
|
from core.script_manager import ScriptManager
|
|
from core.profile_manager import ProfileManager
|
|
|
|
app = Flask(__name__,
|
|
template_folder='../frontend/templates',
|
|
static_folder='../frontend/static')
|
|
|
|
# Initialize managers
|
|
data_dir = Path(__file__).parent.parent / 'data'
|
|
script_groups_dir = Path(__file__).parent / 'script_groups'
|
|
|
|
profile_manager = ProfileManager(data_dir)
|
|
script_manager = ScriptManager(script_groups_dir)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""Render main page"""
|
|
return render_template('index.html')
|
|
|
|
# Profile endpoints
|
|
@app.route('/api/profiles', methods=['GET'])
|
|
def get_profiles():
|
|
"""Get all profiles"""
|
|
return jsonify(profile_manager.get_all_profiles())
|
|
|
|
@app.route('/api/profiles/<profile_id>', methods=['GET'])
|
|
def get_profile(profile_id):
|
|
"""Get specific profile"""
|
|
profile = profile_manager.get_profile(profile_id)
|
|
if profile:
|
|
return jsonify(profile)
|
|
return jsonify({"error": "Profile not found"}), 404
|
|
|
|
@app.route('/api/profiles', methods=['POST'])
|
|
def create_profile():
|
|
"""Create new profile"""
|
|
try:
|
|
profile = profile_manager.create_profile(request.json)
|
|
return jsonify(profile)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
|
|
@app.route('/api/profiles/<profile_id>', methods=['PUT'])
|
|
def update_profile(profile_id):
|
|
"""Update existing profile"""
|
|
try:
|
|
profile = profile_manager.update_profile(profile_id, request.json)
|
|
return jsonify(profile)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
|
|
@app.route('/api/profiles/<profile_id>', methods=['DELETE'])
|
|
def delete_profile(profile_id):
|
|
"""Delete profile"""
|
|
try:
|
|
profile_manager.delete_profile(profile_id)
|
|
return jsonify({"status": "success"})
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
|
|
# Script group endpoints
|
|
@app.route('/api/script-groups', methods=['GET'])
|
|
def get_script_groups():
|
|
"""Get all available script groups"""
|
|
try:
|
|
groups = script_manager.get_available_groups()
|
|
return jsonify(groups)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/api/script-groups/<group_id>/config', methods=['GET'])
|
|
def get_group_config(group_id):
|
|
"""Get script group configuration"""
|
|
try:
|
|
config = script_manager.get_group_data(group_id)
|
|
return jsonify(config)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/api/script-groups/<group_id>/config', methods=['PUT'])
|
|
def update_group_config(group_id):
|
|
"""Update script group configuration"""
|
|
try:
|
|
config = script_manager.update_group_data(group_id, request.json)
|
|
return jsonify(config)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
|
|
@app.route('/api/script-groups/<group_id>/scripts', methods=['GET'])
|
|
def get_group_scripts(group_id):
|
|
"""Get scripts for a specific group"""
|
|
try:
|
|
scripts = script_manager.get_group_scripts(group_id)
|
|
return jsonify(scripts)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/api/script-groups/<group_id>/schema', methods=['GET'])
|
|
def get_group_schema(group_id):
|
|
"""Get script group schema"""
|
|
try:
|
|
schema = script_manager.get_global_schema()
|
|
return jsonify(schema)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
# Directory handling endpoints
|
|
@app.route('/api/select-directory', methods=['GET'])
|
|
def handle_select_directory():
|
|
"""Handle directory selection"""
|
|
result = select_directory()
|
|
if "error" in result:
|
|
return jsonify(result), 400
|
|
return jsonify(result)
|
|
|
|
# Work directory configuration endpoints
|
|
@app.route('/api/workdir-config/<group_id>', methods=['GET'])
|
|
def get_workdir_config(group_id):
|
|
"""Get work directory configuration for a group"""
|
|
try:
|
|
group_data = script_manager.get_group_data(group_id)
|
|
work_dir = group_data.get('work_dir')
|
|
|
|
if not work_dir:
|
|
return jsonify({"error": "Work directory not configured"}), 400
|
|
|
|
from core.workdir_config import WorkDirConfigManager
|
|
workdir_manager = WorkDirConfigManager(work_dir, group_id)
|
|
return jsonify(workdir_manager.get_group_config())
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/api/workdir-config/<group_id>', methods=['PUT'])
|
|
def update_workdir_config(group_id):
|
|
"""Update work directory configuration for a group"""
|
|
try:
|
|
group_data = script_manager.get_group_data(group_id)
|
|
work_dir = group_data.get('work_dir')
|
|
|
|
if not work_dir:
|
|
return jsonify({"error": "Work directory not configured"}), 400
|
|
|
|
from core.workdir_config import WorkDirConfigManager
|
|
workdir_manager = WorkDirConfigManager(work_dir, group_id)
|
|
workdir_manager.update_group_config(request.json)
|
|
return jsonify({"status": "success"})
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
|
|
# Script execution endpoint
|
|
@app.route('/api/script-groups/<group_id>/scripts/<script_id>/run', methods=['POST'])
|
|
def run_script(group_id, script_id):
|
|
"""Execute a specific script"""
|
|
try:
|
|
result = script_manager.execute_script(group_id, script_id, request.json.get('profile', {}))
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, port=5000) |