import os import sys import shutil import logging import re from pathlib import Path # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Add the backend directory to the Python path sys.path.append(os.path.abspath("backend")) # Create necessary directories if they don't exist os.makedirs("./temp_audio", exist_ok=True) os.makedirs("./temp", exist_ok=True) os.makedirs("./static", exist_ok=True) # Function to fix localhost URLs in JavaScript files def fix_js_files(): """Fix all JS files in the static directory to replace localhost:8000 with relative URLs""" assets_dir = Path("./static/assets") if not assets_dir.exists(): logger.warning(f"Assets directory not found at {assets_dir}") return logger.info(f"Searching for JS files in {assets_dir}") js_files = list(assets_dir.glob("*.js")) logger.info(f"Found {len(js_files)} JS files") for js_file in js_files: try: logger.info(f"Processing {js_file}") with open(js_file, "r", encoding="utf-8") as f: content = f.read() # Count occurrences before replacement count_before = content.count("localhost:8000") if count_before > 0: logger.info(f"Found {count_before} instances of localhost:8000 in {js_file}") # Replace localhost URLs with relative ones modified_content = content.replace("http://localhost:8000", "") modified_content = modified_content.replace("https://localhost:8000", "") # Create a backup just in case backup_file = js_file.with_suffix(".js.bak") shutil.copy(js_file, backup_file) # Write the modified content back with open(js_file, "w", encoding="utf-8") as f: f.write(modified_content) logger.info(f"Fixed {count_before} localhost URLs in {js_file}") except Exception as e: logger.error(f"Error processing {js_file}: {str(e)}") # Run the JS file fixer at startup fix_js_files() # Check for index.html and create a simple one if it doesn't exist static_path = Path("./static") index_path = static_path / "index.html" if not index_path.exists(): logger.warning("index.html not found in static directory, creating a simple one") with open(index_path, "w") as f: f.write("""