villsim/backend/main.py

75 lines
1.6 KiB
Python

"""FastAPI entry point for the Village Simulation backend."""
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from backend.api.routes import router
from backend.core.engine import get_engine
# Create FastAPI app
app = FastAPI(
title="Village Simulation API",
description="API for the Village Economy Simulation",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
# Add CORS middleware for frontend access
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, specify actual origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API routes
app.include_router(router, prefix="/api", tags=["simulation"])
@app.on_event("startup")
async def startup_event():
"""Initialize the simulation on startup."""
engine = get_engine()
engine.initialize(num_agents=8)
print("Village Simulation initialized with 8 agents")
@app.get("/", tags=["root"])
def root():
"""Root endpoint with API information."""
return {
"name": "Village Simulation API",
"version": "1.0.0",
"docs": "/docs",
"status": "running",
}
@app.get("/health", tags=["health"])
def health_check():
"""Health check endpoint."""
engine = get_engine()
return {
"status": "healthy",
"simulation_running": engine.is_running,
"agents_alive": len(engine.world.get_living_agents()),
}
def main():
"""Run the server."""
uvicorn.run(
"backend.main:app",
host="0.0.0.0",
port=8000,
reload=True,
)
if __name__ == "__main__":
main()