-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_auth.py
126 lines (106 loc) · 3.23 KB
/
main_auth.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
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
"""Основной файл приложения."""
import asyncio
from contextlib import asynccontextmanager
from aiohttp import ClientSession
from fastapi import FastAPI
from jaeger_client import Config
from prometheus_client import make_asgi_app
from pydantic_settings import BaseSettings
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.cors import CORSMiddleware
from config.config import AUTH_APP_PORT, PATH_PREFIX
from config.kafka_setup import (
kafka_response_listener,
start_producer,
stop_producer,
)
from credit_card_auth.src.middlewares import (
metrics_middleware,
tracing_middleware,
)
from credit_card_auth.src.routers import (
balance_router,
readiness,
token_router,
transactions_router,
verification_router,
)
class Settings(BaseSettings):
"""Конфигурация приложения."""
app_host: str = '0.0.0.0' # noqa: F401, S104
app_port: int = AUTH_APP_PORT
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Контекст для работы приложения.
В нашем случае используется для Егеря.
Args:
app: Приложение.
Yields:
Контекст приложения.
"""
session = ClientSession()
config = Config(
config={
'sampler': {
'type': 'const',
'param': 1,
},
'local_agent': {
'reporting_host': 'jaeger-agent.monitoring.svc.cluster.local', # noqa: E501
'reporting_port': 6831,
},
'logging': True,
},
service_name='gran_auth',
validate=True,
)
tracer = config.initialize_tracer()
yield {'client_session': session, 'jaeger_tracer': tracer}
await session.close()
app = FastAPI(lifespan=lifespan)
@app.on_event('startup')
async def startup_event():
"""Начало работы приложения."""
app.state.kafka_producer = await start_producer()
app.state.pending_requests = {}
asyncio.create_task(kafka_response_listener(app))
@app.on_event('shutdown')
async def shutdown_event():
"""Окончание работы приложения."""
await stop_producer(app.state.kafka_producer)
app.include_router(token_router.router, prefix=PATH_PREFIX, tags=['auth'])
app.include_router(balance_router.router, prefix=PATH_PREFIX, tags=['balance'])
app.include_router(
verification_router.router,
prefix=PATH_PREFIX,
tags=['verification'],
)
app.include_router(
transactions_router.router,
prefix=PATH_PREFIX,
tags=['transactions'],
)
app.include_router(readiness.router, tags=['readiness'])
metrics_app = make_asgi_app()
app.mount('/metrics', metrics_app)
app.add_middleware(BaseHTTPMiddleware, dispatch=metrics_middleware)
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
app.add_middleware(
BaseHTTPMiddleware,
dispatch=tracing_middleware,
)
if __name__ == '__main__':
import uvicorn # noqa: WPS433
settings = Settings()
uvicorn.run(
app,
host=settings.app_host,
port=settings.app_port,
)