1- from fastapi import FastAPI
1+ from fastapi import FastAPI , APIRouter , Depends
2+ from fastapi .openapi .docs import get_swagger_ui_html , get_redoc_html
3+ from fastapi .openapi .utils import get_openapi
24import redis .asyncio as redis
35from arq import create_pool
46from arq .connections import RedisSettings
57
68from app .api import router
9+ from app .api .dependencies import get_current_superuser
710from app .core import cache , queue
811from app .core .database import Base
912from app .core .database import async_engine as engine
1316 RedisCacheSettings ,
1417 AppSettings ,
1518 ClientSideCacheSettings ,
16- RedisQueueSettings
19+ RedisQueueSettings ,
20+ EnvironmentOption ,
21+ EnvironmentSettings
1722)
1823
1924# -------------- database --------------
@@ -44,22 +49,60 @@ async def close_redis_queue_pool():
4449
4550
4651# -------------- application --------------
47- def create_application () -> FastAPI :
52+ def create_application (settings , ** kwargs ) -> FastAPI :
53+ """
54+ Creates and configures a FastAPI application based on the provided keyword arguments.
55+
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.
59+
60+ Parameters
61+ ----------
62+ 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+
74+ Returns
75+ -------
76+ FastAPI: A configured FastAPI application instance.
77+ """
78+
79+ # --- before creating application ---
4880 if isinstance (settings , AppSettings ):
49- application = FastAPI (
50- title = settings .APP_NAME ,
51- description = settings .APP_DESCRIPTION ,
52- contact = {
81+ to_update = {
82+ " title" : settings .APP_NAME ,
83+ " description" : settings .APP_DESCRIPTION ,
84+ " contact" : {
5385 "name" : settings .CONTACT_NAME ,
5486 "email" : settings .CONTACT_EMAIL
5587 },
56- license_info = {
88+ " license_info" : {
5789 "name" : settings .LICENSE_NAME
5890 }
91+ }
92+ kwargs .update (to_update )
93+
94+ if isinstance (settings , EnvironmentSettings ):
95+ kwargs .update (
96+ {
97+ "docs_url" : None ,
98+ "redoc_url" : None ,
99+ "openapi_url" : None
100+ }
59101 )
60- else :
61- application = FastAPI ()
62102
103+ application = FastAPI (** kwargs )
104+
105+ # --- application created ---
63106 application .include_router (router )
64107
65108 if isinstance (settings , DatabaseSettings ):
@@ -76,7 +119,30 @@ def create_application() -> FastAPI:
76119 application .add_event_handler ("startup" , create_redis_queue_pool )
77120 application .add_event_handler ("shutdown" , close_redis_queue_pool )
78121
122+ if isinstance (settings , EnvironmentSettings ):
123+ if settings .ENVIRONMENT is not EnvironmentOption .PRODUCTION :
124+ docs_router = APIRouter ()
125+ if settings .ENVIRONMENT is not EnvironmentOption .LOCAL :
126+ docs_router = APIRouter (dependencies = [Depends (get_current_superuser )])
127+
128+ @docs_router .get ("/docs" , include_in_schema = False )
129+ async def get_swagger_documentation ():
130+ return get_swagger_ui_html (openapi_url = "/openapi.json" , title = "docs" )
131+
132+
133+ @docs_router .get ("/redoc" , include_in_schema = False )
134+ async def get_redoc_documentation ():
135+ return get_redoc_html (openapi_url = "/openapi.json" , title = "docs" )
136+
137+
138+ @docs_router .get ("/openapi.json" , include_in_schema = False )
139+ async def openapi ():
140+ return get_openapi (title = app .title , version = app .version , routes = app .routes )
141+
142+
143+ application .include_router (docs_router )
144+
79145 return application
80146
81147
82- app = create_application ()
148+ app = create_application (settings = settings )
0 commit comments