feat(sandbox): add Node.js code execution support to sandbox
This commit is contained in:
@@ -11,51 +11,15 @@ from fastapi import FastAPI
|
||||
|
||||
from app.config import get_config
|
||||
from app.controllers import manager_router
|
||||
from app.core.runners import init_sandbox_user
|
||||
from app.dependencies import setup_dependencies, update_dependencies_periodically
|
||||
from app.logger import setup_logger, get_logger
|
||||
|
||||
setup_logger()
|
||||
config = get_config()
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan manager"""
|
||||
logger = get_logger()
|
||||
|
||||
# Startup
|
||||
logger.info("Starting RedBear Sandbox...")
|
||||
|
||||
# Setup dependencies in background
|
||||
asyncio.create_task(setup_dependencies())
|
||||
|
||||
# Start periodic dependency updates
|
||||
config = get_config()
|
||||
if config.python_deps_update_interval:
|
||||
asyncio.create_task(update_dependencies_periodically())
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logger.info("Shutting down Redbear Sandbox...")
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""Create FastAPI application"""
|
||||
config = get_config()
|
||||
|
||||
app = FastAPI(
|
||||
title="Sandbox",
|
||||
description="Secure code execution sandbox",
|
||||
version="2.0.0",
|
||||
lifespan=lifespan,
|
||||
debug=config.app.debug
|
||||
)
|
||||
|
||||
app.include_router(manager_router)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def check_root_privileges():
|
||||
"""Check if running with root privileges"""
|
||||
if os.geteuid() != 0:
|
||||
@@ -63,35 +27,38 @@ def check_root_privileges():
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point"""
|
||||
# Check root privileges
|
||||
check_root_privileges()
|
||||
check_root_privileges()
|
||||
|
||||
# Setup logging
|
||||
setup_logger()
|
||||
|
||||
config = get_config()
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan manager"""
|
||||
logger = get_logger()
|
||||
|
||||
config = get_config()
|
||||
# Startup
|
||||
logger.info("Starting RedBear Sandbox...")
|
||||
logger.info(f"Starting server on port {config.app.port}")
|
||||
logger.info(f"Debug mode: {config.app.debug}")
|
||||
logger.info(f"Max workers: {config.max_workers}")
|
||||
logger.info(f"Max requests: {config.max_requests}")
|
||||
logger.info(f"Network enabled: {config.enable_network}")
|
||||
init_sandbox_user()
|
||||
await setup_dependencies()
|
||||
|
||||
# Create app
|
||||
app = create_app()
|
||||
if config.python_deps_update_interval:
|
||||
asyncio.create_task(update_dependencies_periodically())
|
||||
|
||||
# Run server
|
||||
uvicorn.run(
|
||||
app,
|
||||
host="0.0.0.0",
|
||||
port=config.app.port,
|
||||
log_level="debug" if config.app.debug else "info",
|
||||
access_log=config.app.debug
|
||||
)
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logger.info("Shutting down Redbear Sandbox...")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
app = FastAPI(
|
||||
title="Sandbox",
|
||||
description="Secure code execution sandbox",
|
||||
version="0.1.0",
|
||||
lifespan=lifespan,
|
||||
debug=config.app.debug
|
||||
)
|
||||
|
||||
app.include_router(manager_router)
|
||||
|
||||
Reference in New Issue
Block a user