openenv/coding_env
21
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 __ENV_TITLE_NAME__ Environment.9 10This module creates an HTTP server that exposes the __ENV_CLASS_NAME__Environment11over 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 38try:39 from ..models import __ENV_CLASS_NAME__Action, __ENV_CLASS_NAME__Observation40 from .__ENV_NAME___environment import __ENV_CLASS_NAME__Environment41except ModuleNotFoundError:42 from models import __ENV_CLASS_NAME__Action, __ENV_CLASS_NAME__Observation43 from server.__ENV_NAME___environment import __ENV_CLASS_NAME__Environment44 45 46# Create the app with web interface and README integration47app = create_app(48 __ENV_CLASS_NAME__Environment,49 __ENV_CLASS_NAME__Action,50 __ENV_CLASS_NAME__Observation,51 env_name="__ENV_NAME__",52 max_concurrent_envs=1, # increase this number to allow more concurrent WebSocket sessions53)54 55 56def main(host: str = "0.0.0.0", port: int = 8000):57 """58 Entry point for direct execution via uv run or python -m.59 60 This function enables running the server without Docker:61 uv run --project . server62 uv run --project . server --port 800163 python -m __ENV_NAME__.server.app64 65 Args:66 host: Host address to bind to (default: "0.0.0.0")67 port: Port number to listen on (default: 8000)68 69 For production deployments, consider using uvicorn directly with70 multiple workers:71 uvicorn __ENV_NAME__.server.app:app --workers 472 """73 import uvicorn74 75 uvicorn.run(app, host=host, port=port)76 77 78if __name__ == "__main__":79 import argparse80 81 parser = argparse.ArgumentParser()82 parser.add_argument("--port", type=int, default=8000)83 args = parser.parse_args()84 main(port=args.port)85 