Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(platform/rest): Update REST API to use standard FastAPI structure #8519

Merged
3 changes: 2 additions & 1 deletion autogpt_platform/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ COPY autogpt_platform/backend /app/autogpt_platform/backend

ENV DATABASE_URL=""
ENV PORT=8000
ENV HOST=0.0.0.0

CMD ["poetry", "run", "rest"]
CMD ["uvicorn", "backend.server.app:app", "--host", "$HOST", "--port", "$PORT"]
15 changes: 7 additions & 8 deletions autogpt_platform/backend/backend/rest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from backend.app import run_processes
from backend.executor import ExecutionScheduler
from backend.server.rest_api import AgentServer

import uvicorn
import backend.util.settings

def main():
"""
Run all the processes required for the AutoGPT-server REST API.
"""
run_processes(
ExecutionScheduler(),
AgentServer(),
Swiftyos marked this conversation as resolved.
Show resolved Hide resolved
uvicorn.run(
"backend.server.app:app",
reload=True,
host=backend.util.settings.Config().agent_api_host,
port=backend.util.settings.Config().agent_api_port,
)


if __name__ == "__main__":
main()
66 changes: 66 additions & 0 deletions autogpt_platform/backend/backend/server/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import contextlib

import fastapi
import fastapi.middleware.cors
import fastapi.responses

import backend.data.block
import backend.data.db
import backend.data.user
import backend.server.routers.v1
import backend.util.settings

settings = backend.util.settings.Settings()


@contextlib.asynccontextmanager
async def lifespan_context(app: fastapi.FastAPI):
await backend.data.db.connect()
await backend.data.block.initialize_blocks()
await backend.data.user.migrate_and_encrypt_user_integrations()
yield
await backend.data.db.disconnect()


docs_url = (
"/docs"
if settings.config.app_env == backend.util.settings.AppEnvironment.LOCAL
else None
)

app = fastapi.FastAPI(
title="AutoGPT Agent Server",
description=(
"This server is used to execute agents that are created by the "
"AutoGPT system."
),
summary="AutoGPT Agent Server",
version="0.1",
lifespan=lifespan_context,
docs_url=docs_url,
)

app.include_router(backend.server.routers.v1.v1_router)
app.add_middleware(
fastapi.middleware.cors.CORSMiddleware,
allow_origins=settings.config.backend_cors_allow_origins,
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)


@app.get(path="/health", tags=["health"], dependencies=[])
async def health():
return {"status": "healthy"}


@app.exception_handler(Exception)
def handle_internal_http_error(request: fastapi.Request, exc: Exception):
return fastapi.responses.JSONResponse(
content={
"message": f"{request.method} {request.url.path} failed",
"error": str(exc),
},
status_code=500,
)
Loading
Loading