-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (40 loc) · 1.27 KB
/
app.py
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
import asyncio
import aiohttp_cors
import uvloop
from aiohttp import web
import settings
from files.routes import routes
from middleware import error_middleware
from storage import MinioStorage
from utils import get_class_by_path
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
app = web.Application(
middlewares=[error_middleware],
client_max_size=settings.MAX_FILE_SIZE
)
cors = aiohttp_cors.setup(app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_methods=["OPTION", "POST", "GET"],
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})
for route in routes:
cors.add(app.router.add_route(**route))
if MinioStorage.__name__ in settings.STORAGE_CLASS:
async def create_minio_client(app):
app['file_storage'] = MinioStorage()
await app['file_storage'].init(loop)
yield
async def close_minio_client(app):
await app['file_storage'].close()
app.cleanup_ctx.append(create_minio_client)
app.on_cleanup.append(close_minio_client)
else:
app['file_storage'] = get_class_by_path(settings.STORAGE_CLASS)()
async def close_session(app):
await app.session.close()
yield
web.run_app(app, host=settings.HOST, port=settings.PORT)