-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix circular import in Azure function loop (#21)
* Make all requests to the API simultaneously * Async API * Add an await to the async wrapper * add nest_asyncio * co-pilot take the wheel * Attempt to fix circular import * Fix views not showing on successful request * test import app inside function * Build app outside loop * Fix circular import --------- Co-authored-by: Reinder Vos de Wael <[email protected]>
- Loading branch information
1 parent
9ac0fca
commit 3d23c6c
Showing
6 changed files
with
78 additions
and
72 deletions.
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,45 @@ | ||
"""Builder of the FastAPI application instance. | ||
This is separate from main.py as the app needs to be created | ||
inside the loop on Azure.""" | ||
import logging | ||
|
||
import fastapi | ||
from fastapi.middleware import cors | ||
|
||
from src.core import settings | ||
from src.routers.features import views as feature_views | ||
from src.routers.surfaces import views as surface_views | ||
|
||
config = settings.get_settings() | ||
LOGGER_NAME = config.LOGGER_NAME | ||
|
||
|
||
def build_app() -> fastapi.FastAPI: | ||
"""Builds and returns a FastAPI application instance with the necessary | ||
routers and middleware. | ||
Returns: | ||
fastapi.FastAPI: The FastAPI application instance. | ||
""" | ||
settings.initialize_logger() | ||
logger = logging.getLogger(LOGGER_NAME) | ||
|
||
api = fastapi.APIRouter(prefix="/api") | ||
api.include_router(feature_views.router) | ||
api.include_router(surface_views.router) | ||
|
||
logger.info("Starting API.") | ||
app = fastapi.FastAPI() | ||
app.include_router(api) | ||
|
||
logger.info("Adding CORS middleware.") | ||
app.add_middleware( | ||
cors.CORSMiddleware, | ||
allow_origins="*", | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
return app |
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 |
---|---|---|
@@ -1,32 +1,4 @@ | ||
"""Main module for the API.""" | ||
import logging | ||
"""Entrypoint for Uvicorn.""" | ||
from src import builder | ||
|
||
import fastapi | ||
from fastapi.middleware import cors | ||
|
||
from src.core import settings | ||
from src.routers.features import views as feature_views | ||
from src.routers.surfaces import views as surface_views | ||
|
||
config = settings.get_settings() | ||
LOGGER_NAME = config.LOGGER_NAME | ||
|
||
settings.initialize_logger() | ||
logger = logging.getLogger(LOGGER_NAME) | ||
|
||
api = fastapi.APIRouter(prefix="/api") | ||
api.include_router(feature_views.router) | ||
api.include_router(surface_views.router) | ||
|
||
logger.info("Starting API.") | ||
app = fastapi.FastAPI() | ||
app.include_router(api) | ||
|
||
logger.info("Adding CORS middleware.") | ||
app.add_middleware( | ||
cors.CORSMiddleware, | ||
allow_origins="*", | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
app = builder.build_app() |
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
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