Skip to content

Commit

Permalink
Update rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftyos committed Nov 1, 2024
1 parent 8950021 commit 69fcf2f
Show file tree
Hide file tree
Showing 3 changed files with 588 additions and 1 deletion.
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"]
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

0 comments on commit 69fcf2f

Please sign in to comment.