Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/server/rest_api.py (Python)

Code before:
            await graph_db.import_packaged_templates()
        yield
        await self.event_queue.close()
        await db.disconnect()

    def run_service(self):
        app = 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=self.lifespan,
        )

        if self._test_dependency_overrides:
            app.dependency_overrides.update(self._test_dependency_overrides)

        app.add_middleware(
            CORSMiddleware,
            allow_origins=[
                "http://localhost:3000",
                "http://127.0.0.1:3000",
                "https://dev-builder.agpt.co",
            ],
            allow_credentials=True,
        )

        # Define the API routes
        api_router = APIRouter(prefix="/api")
        api_router.dependencies.append(Depends(auth_middleware))

        # Import & Attach sub-routers
        import backend.server.routers.analytics
        import backend.server.routers.integrations

        api_router.include_router(
            backend.server.routers.integrations.router,
            prefix="/integrations",
            tags=["integrations"],
            dependencies=[Depends(auth_middleware)],
        )

        api_router.include_router(
            backend.server.routers.analytics.router,
            prefix="/analytics",
            tags=["analytics"],
            dependencies=[Depends(auth_middleware)],
        )

Review comment (suggestion):
We can put `BACKEND_CORS_ALLOW_ORIGINS=http://localhost:3000` in `.env.template` and configure it as `https://dev-builder.agpt.co` in our deployment, and get rid of these hardcoded values.

Code after:
        await block.initialize_blocks()
        if await user_db.create_default_user(settings.config.enable_auth):
            await graph_db.import_packaged_templates()
        yield
        await self.event_queue.close()
        await db.disconnect()

    def run_service(self):
        app = 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=self.lifespan,
        )

        if self._test_dependency_overrides:
            app.dependency_overrides.update(self._test_dependency_overrides)

        logger.debug(
            f"FastAPI CORS allow origins: {Config().backend_cors_allow_origins}"
        )

        app.add_middleware(
            CORSMiddleware,
            allow_origins=Config().backend_cors_allow_origins,
            allow_credentials=True,
            allow_methods=["*"],  # Allows all methods
            allow_headers=["*"],  # Allows all headers
        )

        # Define the API routes
        api_router = APIRouter(prefix="/api")
        api_router.dependencies.append(Depends(auth_middleware))

        # Import & Attach sub-routers
        import backend.server.routers.analytics
        import backend.server.routers.integrations

        api_router.include_router(
            backend.server.routers.integrations.router,
            prefix="/integrations",
            tags=["integrations"],
            dependencies=[Depends(auth_middleware)],
        )

        api_router.include_router(
            backend.server.routers.analytics.router,