Spaces:
Running
Running
| """ | |
| API routes for nginx configuration generation. | |
| """ | |
| import uuid | |
| from fastapi import APIRouter, Depends, HTTPException, Request, status | |
| from app.api.deps import get_devops_service, get_logger, get_settings | |
| from app.core.security import limiter | |
| from app.models.schemas import ConfigState, GeneratedFiles | |
| router = APIRouter() | |
| async def generate_nginx_config( | |
| request: Request, | |
| config: ConfigState, | |
| service=Depends(get_devops_service), | |
| settings=Depends(get_settings), | |
| logger=Depends(get_logger), | |
| ): | |
| """Generate nginx configuration based on stack type.""" | |
| client_ip = request.client.host if request.client else "unknown" | |
| logger.info( | |
| f"Nginx config generation request from {client_ip} for {config.stackType}" | |
| ) | |
| try: | |
| # Generate unique filename | |
| script_id = str(uuid.uuid4())[:8] | |
| # Generate nginx config | |
| nginx_config = service.generate_nginx_config(config, script_id) | |
| nginx_filename = f"nginx-{script_id}.conf" | |
| nginx_path = settings.generated_dir / nginx_filename | |
| # Ensure generated directory exists | |
| nginx_path.parent.mkdir(exist_ok=True) | |
| # Write nginx config file | |
| with open(nginx_path, "w") as f: | |
| f.write(nginx_config) | |
| # Create nginx config URL for frontend | |
| nginx_url = f"/download/{nginx_filename}" | |
| response = GeneratedFiles( | |
| script_path="", | |
| script_url="", | |
| nginx_config_path=str(nginx_path), | |
| nginx_url=nginx_url, | |
| description="Nginx Configuration for " + config.stackType + " + " + | |
| (config.domain or 'your-domain.com'), | |
| ) | |
| return response | |
| except Exception as e: | |
| logger.error(f"Failed to generate nginx config: {str(e)}") | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to generate nginx config: {str(e)}", | |
| ) | |