44import redis .asyncio as redis
55from arq import create_pool
66from arq .connections import RedisSettings
7+ import anyio
78
89from app .api import router
910from app .api .dependencies import get_current_superuser
@@ -49,31 +50,43 @@ async def close_redis_queue_pool():
4950
5051
5152# -------------- application --------------
52- def create_application (settings , ** kwargs ) -> FastAPI :
53+ async def set_threadpool_tokens (number_of_tokens = 100 ):
54+ limiter = anyio .to_thread .current_default_thread_limiter ()
55+ limiter .total_tokens = number_of_tokens
56+
57+
58+ # -------------- application --------------
59+ def create_application (router : APIRouter , settings , ** kwargs ) -> FastAPI :
5360 """
54- Creates and configures a FastAPI application based on the provided keyword arguments .
61+ Creates and configures a FastAPI application based on the provided settings .
5562
56- The function initializes a FastAPI application and conditionally configures it
57- with various settings and handlers. The configuration is determined by the type
58- of settings object provided.
63+ This function initializes a FastAPI application, then conditionally configures
64+ it with various settings and handlers. The specific configuration is determined
65+ by the type of the ` settings` object provided.
5966
6067 Parameters
6168 ----------
69+ router : APIRouter
70+ The APIRouter object that contains the routes to be included in the FastAPI application.
71+
6272 settings
63- The settings object can be an instance of one or more of the following:
64- - AppSettings: Configures basic app information like name, description, contact, and license info.
65- - DatabaseSettings: Adds event handlers related to database tables during startup.
66- - RedisCacheSettings: Adds event handlers for creating and closing Redis cache pool.
67- - ClientSideCacheSettings: Adds middleware for client-side caching.
68- - RedisQueueSettings: Adds event handlers for creating and closing Redis queue pool.
69- - EnvironmentSettings: Sets documentation URLs and sets up custom routes for documentation.
70-
71- **kwargs
72- Additional keyword arguments that are passed directly to the FastAPI constructor.
73-
73+ An instance representing the settings for configuring the FastAPI application. It determines the configuration applied:
74+
75+ - AppSettings: Configures basic app metadata like name, description, contact, and license info.
76+ - DatabaseSettings: Adds event handlers for initializing database tables during startup.
77+ - RedisCacheSettings: Sets up event handlers for creating and closing a Redis cache pool.
78+ - ClientSideCacheSettings: Integrates middleware for client-side caching.
79+ - RedisQueueSettings: Sets up event handlers for creating and closing a Redis queue pool.
80+ - EnvironmentSettings: Conditionally sets documentation URLs and integrates custom routes for API documentation based on environment type.
81+
82+ **kwargs
83+ Extra keyword arguments passed directly to the FastAPI constructor.
84+
7485 Returns
7586 -------
76- FastAPI: A configured FastAPI application instance.
87+ FastAPI
88+ A fully configured FastAPI application instance.
89+
7790 """
7891
7992 # --- before creating application ---
@@ -104,7 +117,8 @@ def create_application(settings, **kwargs) -> FastAPI:
104117
105118 # --- application created ---
106119 application .include_router (router )
107-
120+ application .add_event_handler ("startup" , set_threadpool_tokens )
121+
108122 if isinstance (settings , DatabaseSettings ):
109123 application .add_event_handler ("startup" , create_tables )
110124
@@ -120,9 +134,9 @@ def create_application(settings, **kwargs) -> FastAPI:
120134 application .add_event_handler ("shutdown" , close_redis_queue_pool )
121135
122136 if isinstance (settings , EnvironmentSettings ):
123- if settings .ENVIRONMENT is not EnvironmentOption .PRODUCTION :
137+ if settings .ENVIRONMENT != EnvironmentOption .PRODUCTION :
124138 docs_router = APIRouter ()
125- if settings .ENVIRONMENT is not EnvironmentOption .LOCAL :
139+ if settings .ENVIRONMENT != EnvironmentOption .LOCAL :
126140 docs_router = APIRouter (dependencies = [Depends (get_current_superuser )])
127141
128142 @docs_router .get ("/docs" , include_in_schema = False )
@@ -137,12 +151,11 @@ async def get_redoc_documentation():
137151
138152 @docs_router .get ("/openapi.json" , include_in_schema = False )
139153 async def openapi ():
140- return get_openapi (title = app .title , version = app .version , routes = app .routes )
141-
154+ return get_openapi (title = application .title , version = application .version , routes = application .routes )
142155
143156 application .include_router (docs_router )
144157
145158 return application
146159
147160
148- app = create_application (settings = settings )
161+ app = create_application (router = router , settings = settings )
0 commit comments