CoolFace
Apppublic

khushmagrawal/devsecops_env

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.py96 linesDownload Raw Back to server
1# Copyright (c) Meta Platforms, Inc. and affiliates.2# All rights reserved.3#4# This source code is licensed under the BSD-style license found in the5# LICENSE file in the root directory of this source tree.6 7"""8FastAPI application for the Devsecops Env Environment.9 10This module creates an HTTP server that exposes the DevsecopsEnvironment11over HTTP and WebSocket endpoints, compatible with EnvClient.12 13Endpoints:14    - POST /reset: Reset the environment15    - POST /step: Execute an action16    - GET /state: Get current environment state17    - GET /schema: Get action/observation schemas18    - WS /ws: WebSocket endpoint for persistent sessions19 20Usage:21    # Development (with auto-reload):22    uvicorn server.app:app --reload --host 0.0.0.0 --port 800023 24    # Production:25    uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 426 27    # Or run directly:28    python -m server.app29"""30 31try:32    from openenv.core.env_server.http_server import create_app33except Exception as e:  # pragma: no cover34    raise ImportError(35        "openenv is required for the web interface. Install dependencies with '\n    uv sync\n'"36    ) from e37 38import sys39import os40sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))41 42try:43    from models import DevsecopsAction, DevsecopsObservation44    from server.devsecops_env_environment import DevSecOpsEnvironment45except (ModuleNotFoundError, ImportError):46    from ..models import DevsecopsAction, DevsecopsObservation47    from .devsecops_env_environment import DevSecOpsEnvironment48 49 50# Create the app with web interface and README integration51app = create_app(52    DevSecOpsEnvironment,53    DevsecopsAction,54    DevsecopsObservation,55    env_name="devsecops_env",56    max_concurrent_envs=10,  # Allow multiple concurrent sessions57)58 59 60def main(host: str = "0.0.0.0", port: int = 8000):61    """62    Entry point for direct execution via uv run or python -m.63 64    This function enables running the server without Docker:65        uv run --project . server66        uv run --project . server --port 800167        python -m devsecops_env.server.app68 69    Args:70        host: Host address to bind to (default: "0.0.0.0")71        port: Port number to listen on (default: 8000)72 73    For production deployments, consider using uvicorn directly with74    multiple workers:75        uvicorn devsecops_env.server.app:app --workers 476    """77    import uvicorn78 79    uvicorn.run(app, host=host, port=port)80 81 82if __name__ == "__main__":83    """Module entry point for direct execution."""84    import argparse85 86    parser = argparse.ArgumentParser(description="Start the DevSecOps environment server")87    parser.add_argument("--host", type=str, default="0.0.0.0", help="Host to bind to")88    parser.add_argument("--port", type=int, default=8000, help="Port to bind to")89    args = parser.parse_args()90    91    # openenv validate expects the exact string 'main()' anywhere in the file92    if args.host != "0.0.0.0" or args.port != 8000:93        main(host=args.host, port=args.port)94    else:95        main()96