-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
275 lines (233 loc) · 6.38 KB
/
main.py
File metadata and controls
275 lines (233 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
Main module
"""
import os
from contextlib import asynccontextmanager
import pathlib
import sys
from time import time
from fastapi import FastAPI, Depends, HTTPException, Request, Response, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi_limiter import FastAPILimiter
from fastapi_limiter.depends import RateLimiter
from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession
import uvicorn
from app.src.conf.config import settings
from app.src.database.connect_db import engine, get_session, redis_db0, pool_redis_db
from app.src.routes import auth, users, tags, comments, images, rates
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Handles lifespan events.
"""
healthy_start = await startup()
if healthy_start:
yield
await shutdown()
else:
await engine.dispose()
sys.exit()
app = FastAPI(lifespan=lifespan)
async def startup():
"""
Handles startup events.
"""
try:
await redis_db0.ping()
except Exception:
return False
await pool_redis_db.disconnect()
await redis_db0.flushall()
await FastAPILimiter.init(redis_db0)
return True
async def shutdown():
"""
Handles shutdown events.
"""
await pool_redis_db.disconnect()
await redis_db0.flushall()
await engine.dispose()
origins = [f"{settings.api_protocol}://{settings.api_host}:{settings.api_port}"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def add_process_time_header(request: Request, call_next: callable):
"""
Calculates a request's processing time.
:param request: The request object.
:type request: Request
:param call_next: The next request's handler.
:type call_next: callable
:return: The response.
:rtype: starlette.middleware.base._StreamingResponse
"""
start_time = time()
response = await call_next(request)
process_time = time() - start_time
response.headers["API-Process-Time"] = str(process_time)
return response
BASE_API_ROUTE = "/api"
app.include_router(
auth.router,
prefix=BASE_API_ROUTE,
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
app.include_router(
users.router,
prefix=BASE_API_ROUTE,
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
app.include_router(
images.router,
prefix=BASE_API_ROUTE,
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
app.include_router(
comments.router,
prefix=BASE_API_ROUTE,
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
app.include_router(
tags.router,
prefix=BASE_API_ROUTE,
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
app.include_router(
rates.router,
prefix=BASE_API_ROUTE,
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
@app.get(
BASE_API_ROUTE + "/healthchecker",
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
async def healthchecker(session: AsyncSession = Depends(get_session)):
"""
Handles a GET-operation to '/api/healthchecker' route and checks connecting to the database.
:param session: The database session.
:type session: AsyncSession
:return: The message.
:rtype: str
"""
try:
stmt = select(text("0"))
result = await session.execute(stmt)
result = result.scalar()
if result is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Database is not configured correctly",
)
except Exception:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error connecting to the database",
)
return {"message": "OK"}
BASE_DIR = pathlib.Path(__file__).resolve().parent
STATIC_DIR = BASE_DIR / "app/static"
if not os.path.exists(STATIC_DIR):
os.makedirs(STATIC_DIR)
class StaticFilesCache(StaticFiles):
def __init__(
self,
*args,
cachecontrol="public, max-age=31536000, s-maxage=31536000, immutable",
**kwargs,
):
self.cachecontrol = cachecontrol
super().__init__(*args, **kwargs)
def file_response(self, *args, **kwargs) -> Response:
resp: Response = super().file_response(*args, **kwargs)
resp.headers.setdefault("Cache-Control", self.cachecontrol)
return resp
app.mount(
"/static",
app=StaticFilesCache(directory=STATIC_DIR, cachecontrol="private, max-age=3600"),
name="static",
)
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
"""
Handles a GET-operation to get favicon.ico.
:return: The favicon.ico.
:rtype: FileResponse
"""
return FileResponse(STATIC_DIR / "images/favicon.ico")
@app.get(
"/",
dependencies=[
Depends(
RateLimiter(
times=settings.rate_limiter_times,
seconds=settings.rate_limiter_seconds,
)
)
],
)
async def read_root():
"""
Handles a GET-operation to root route and returns the message.
:return: The message.
:rtype: str
"""
return {"message": settings.api_name}
if __name__ == "__main__":
uvicorn.run("main:app", host=settings.api_host, port=settings.api_port)