Enhance static directory handling and job deployment configuration

This commit is contained in:
2025-02-26 17:22:35 +07:00
parent 5c619e1f19
commit acae88076c
8 changed files with 984 additions and 151 deletions

View File

@ -92,8 +92,78 @@ async def health_check():
return health_status
# Mount static files
app.mount("/", StaticFiles(directory="static", html=True), name="static")
# Find the static directory
def find_static_directory():
"""Find the static directory by checking multiple possible locations."""
logger.info("Starting static directory search...")
# First check if STATIC_DIR environment variable is set
static_dir_env = os.getenv("STATIC_DIR")
if static_dir_env:
logger.info(f"STATIC_DIR environment variable found: '{static_dir_env}'")
if os.path.isdir(static_dir_env):
logger.info(f"✅ Confirmed '{static_dir_env}' exists and is a directory")
return static_dir_env
else:
logger.warning(f"❌ STATIC_DIR '{static_dir_env}' does not exist or is not a directory")
# List parent directory contents if possible
parent_dir = os.path.dirname(static_dir_env)
if os.path.exists(parent_dir):
logger.info(f"Contents of parent directory '{parent_dir}':")
try:
for item in os.listdir(parent_dir):
item_path = os.path.join(parent_dir, item)
item_type = "directory" if os.path.isdir(item_path) else "file"
logger.info(f" - {item} ({item_type})")
except Exception as e:
logger.error(f"Error listing parent directory: {str(e)}")
else:
logger.info("STATIC_DIR environment variable not set")
# Possible locations for the static directory
possible_paths = [
"static", # Local development
"/app/static", # Docker container
"/local/nomad_mcp/static", # Nomad with artifact
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "static") # Relative to this file
]
logger.info(f"Checking {len(possible_paths)} possible static directory locations:")
# Check each path and use the first one that exists
for path in possible_paths:
logger.info(f"Checking path: '{path}'")
if os.path.isdir(path):
logger.info(f"✅ Found valid static directory at: '{path}'")
return path
else:
logger.info(f"❌ Path '{path}' does not exist or is not a directory")
# If no static directory is found, log a warning but don't fail
# This allows the API to still function even without the UI
logger.warning("No static directory found in any of the checked locations. UI will not be available.")
# Try to create the static directory if STATIC_DIR is set
if static_dir_env:
try:
logger.info(f"Attempting to create static directory at '{static_dir_env}'")
os.makedirs(static_dir_env, exist_ok=True)
if os.path.isdir(static_dir_env):
logger.info(f"✅ Successfully created static directory at '{static_dir_env}'")
return static_dir_env
else:
logger.error(f"Failed to create static directory at '{static_dir_env}'")
except Exception as e:
logger.error(f"Error creating static directory: {str(e)}")
return None
# Mount static files if the directory exists
static_dir = find_static_directory()
if static_dir:
app.mount("/", StaticFiles(directory=static_dir, html=True), name="static")
else:
logger.warning("Static files not mounted. API endpoints will still function.")
if __name__ == "__main__":
import uvicorn