-
Notifications
You must be signed in to change notification settings - Fork 44.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
588 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.