4
4
import redis .asyncio as redis
5
5
from arq import create_pool
6
6
from arq .connections import RedisSettings
7
+ import anyio
7
8
8
9
from app .api import router
9
10
from app .api .dependencies import get_current_superuser
@@ -49,31 +50,43 @@ async def close_redis_queue_pool():
49
50
50
51
51
52
# -------------- 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 :
53
60
"""
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 .
55
62
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.
59
66
60
67
Parameters
61
68
----------
69
+ router : APIRouter
70
+ The APIRouter object that contains the routes to be included in the FastAPI application.
71
+
62
72
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
+
74
85
Returns
75
86
-------
76
- FastAPI: A configured FastAPI application instance.
87
+ FastAPI
88
+ A fully configured FastAPI application instance.
89
+
77
90
"""
78
91
79
92
# --- before creating application ---
@@ -104,7 +117,8 @@ def create_application(settings, **kwargs) -> FastAPI:
104
117
105
118
# --- application created ---
106
119
application .include_router (router )
107
-
120
+ application .add_event_handler ("startup" , set_threadpool_tokens )
121
+
108
122
if isinstance (settings , DatabaseSettings ):
109
123
application .add_event_handler ("startup" , create_tables )
110
124
@@ -120,9 +134,9 @@ def create_application(settings, **kwargs) -> FastAPI:
120
134
application .add_event_handler ("shutdown" , close_redis_queue_pool )
121
135
122
136
if isinstance (settings , EnvironmentSettings ):
123
- if settings .ENVIRONMENT is not EnvironmentOption .PRODUCTION :
137
+ if settings .ENVIRONMENT != EnvironmentOption .PRODUCTION :
124
138
docs_router = APIRouter ()
125
- if settings .ENVIRONMENT is not EnvironmentOption .LOCAL :
139
+ if settings .ENVIRONMENT != EnvironmentOption .LOCAL :
126
140
docs_router = APIRouter (dependencies = [Depends (get_current_superuser )])
127
141
128
142
@docs_router .get ("/docs" , include_in_schema = False )
@@ -137,12 +151,11 @@ async def get_redoc_documentation():
137
151
138
152
@docs_router .get ("/openapi.json" , include_in_schema = False )
139
153
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 )
142
155
143
156
application .include_router (docs_router )
144
157
145
158
return application
146
159
147
160
148
- app = create_application (settings = settings )
161
+ app = create_application (router = router , settings = settings )
0 commit comments