|
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 |
4 |
| -import redis.asyncio as redis |
5 |
| -from arq import create_pool |
6 |
| -from arq.connections import RedisSettings |
7 |
| -import anyio |
8 |
| - |
| 1 | +from app.core.config import settings |
9 | 2 | from app.api import router
|
10 |
| -from app.api.dependencies import get_current_superuser |
11 |
| -from app.core import cache, queue |
12 |
| -from app.core.database import Base |
13 |
| -from app.core.database import async_engine as engine |
14 |
| -from app.core.config import ( |
15 |
| - settings, |
16 |
| - DatabaseSettings, |
17 |
| - RedisCacheSettings, |
18 |
| - AppSettings, |
19 |
| - ClientSideCacheSettings, |
20 |
| - RedisQueueSettings, |
21 |
| - EnvironmentOption, |
22 |
| - EnvironmentSettings |
23 |
| -) |
24 |
| - |
25 |
| -# -------------- database -------------- |
26 |
| -async def create_tables(): |
27 |
| - async with engine.begin() as conn: |
28 |
| - await conn.run_sync(Base.metadata.create_all) |
29 |
| - |
30 |
| - |
31 |
| -# -------------- cache -------------- |
32 |
| -async def create_redis_cache_pool(): |
33 |
| - cache.pool = redis.ConnectionPool.from_url(settings.REDIS_CACHE_URL) |
34 |
| - cache.client = redis.Redis.from_pool(cache.pool) |
35 |
| - |
36 |
| - |
37 |
| -async def close_redis_cache_pool(): |
38 |
| - await cache.client.aclose() |
39 |
| - |
40 |
| - |
41 |
| -# -------------- queue -------------- |
42 |
| -async def create_redis_queue_pool(): |
43 |
| - queue.pool = await create_pool( |
44 |
| - RedisSettings(host=settings.REDIS_QUEUE_HOST, port=settings.REDIS_QUEUE_PORT) |
45 |
| - ) |
46 |
| - |
47 |
| - |
48 |
| -async def close_redis_queue_pool(): |
49 |
| - await queue.pool.aclose() |
50 |
| - |
51 |
| - |
52 |
| -# -------------- application -------------- |
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: |
60 |
| - """ |
61 |
| - Creates and configures a FastAPI application based on the provided settings. |
62 |
| -
|
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. |
66 |
| -
|
67 |
| - Parameters |
68 |
| - ---------- |
69 |
| - router : APIRouter |
70 |
| - The APIRouter object that contains the routes to be included in the FastAPI application. |
71 |
| -
|
72 |
| - settings |
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 |
| -
|
85 |
| - Returns |
86 |
| - ------- |
87 |
| - FastAPI |
88 |
| - A fully configured FastAPI application instance. |
89 |
| -
|
90 |
| - """ |
91 |
| - |
92 |
| - # --- before creating application --- |
93 |
| - if isinstance(settings, AppSettings): |
94 |
| - to_update = { |
95 |
| - "title": settings.APP_NAME, |
96 |
| - "description": settings.APP_DESCRIPTION, |
97 |
| - "contact": { |
98 |
| - "name": settings.CONTACT_NAME, |
99 |
| - "email": settings.CONTACT_EMAIL |
100 |
| - }, |
101 |
| - "license_info": { |
102 |
| - "name": settings.LICENSE_NAME |
103 |
| - } |
104 |
| - } |
105 |
| - kwargs.update(to_update) |
106 |
| - |
107 |
| - if isinstance(settings, EnvironmentSettings): |
108 |
| - kwargs.update( |
109 |
| - { |
110 |
| - "docs_url": None, |
111 |
| - "redoc_url": None, |
112 |
| - "openapi_url": None |
113 |
| - } |
114 |
| - ) |
115 |
| - |
116 |
| - application = FastAPI(**kwargs) |
117 |
| - |
118 |
| - # --- application created --- |
119 |
| - application.include_router(router) |
120 |
| - application.add_event_handler("startup", set_threadpool_tokens) |
121 |
| - |
122 |
| - if isinstance(settings, DatabaseSettings): |
123 |
| - application.add_event_handler("startup", create_tables) |
124 |
| - |
125 |
| - if isinstance(settings, RedisCacheSettings): |
126 |
| - application.add_event_handler("startup", create_redis_cache_pool) |
127 |
| - application.add_event_handler("shutdown", close_redis_cache_pool) |
128 |
| - |
129 |
| - if isinstance(settings, ClientSideCacheSettings): |
130 |
| - application.add_middleware(cache.ClientCacheMiddleware, max_age=60) |
131 |
| - |
132 |
| - if isinstance(settings, RedisQueueSettings): |
133 |
| - application.add_event_handler("startup", create_redis_queue_pool) |
134 |
| - application.add_event_handler("shutdown", close_redis_queue_pool) |
135 |
| - |
136 |
| - if isinstance(settings, EnvironmentSettings): |
137 |
| - if settings.ENVIRONMENT != EnvironmentOption.PRODUCTION: |
138 |
| - docs_router = APIRouter() |
139 |
| - if settings.ENVIRONMENT != EnvironmentOption.LOCAL: |
140 |
| - docs_router = APIRouter(dependencies=[Depends(get_current_superuser)]) |
141 |
| - |
142 |
| - @docs_router.get("/docs", include_in_schema=False) |
143 |
| - async def get_swagger_documentation(): |
144 |
| - return get_swagger_ui_html(openapi_url="/openapi.json", title="docs") |
145 |
| - |
146 |
| - |
147 |
| - @docs_router.get("/redoc", include_in_schema=False) |
148 |
| - async def get_redoc_documentation(): |
149 |
| - return get_redoc_html(openapi_url="/openapi.json", title="docs") |
150 |
| - |
151 |
| - |
152 |
| - @docs_router.get("/openapi.json", include_in_schema=False) |
153 |
| - async def openapi(): |
154 |
| - return get_openapi(title=application.title, version=application.version, routes=application.routes) |
155 |
| - |
156 |
| - application.include_router(docs_router) |
157 |
| - |
158 |
| - return application |
159 |
| - |
| 3 | +from app.core.setup import create_application |
160 | 4 |
|
161 | 5 | app = create_application(router=router, settings=settings)
|
0 commit comments