Skip to content

Commit 224f7c2

Browse files
authored
Merge pull request #2 from sumaro2101/develop
New features
2 parents 979f806 + 6558e99 commit 224f7c2

32 files changed

Lines changed: 7028 additions & 13 deletions

.env.sample

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ DB_PORT=5432
1414
RMQ_HOST=rabbitmq
1515
RMQ_PORT=5672
1616
RABBITMQ_DEFAULT_USER=guest
17-
RABBITMQ_DEFAULT_PASS=guest
17+
RABBITMQ_DEFAULT_PASS=guest
18+
# ==================ORIGINS==================
19+
CURRENT_ORIGIN=http://locahost:8080
20+
# ==================GRAFANA==================
21+
GF_SECURITY_ADMIN_USER=admin
22+
GF_SECURITY_ADMIN_PASSWORD=admin
23+
# ==================CADDY==================
24+
ADMIN_USER=admin
25+
ADMIN_PASSWORD=admin

api_v1/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .routers import router
1+
from .routers import register_routers
22

3-
__all__ = ('router',)
3+
4+
__all__ = ('register_routers',)

api_v1/exeptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from starlette.exceptions import HTTPException
2+
3+
4+
class ValidationError(HTTPException):
5+
"""
6+
Исключение вызванное проблемами с валидацией
7+
"""
8+
9+
pass

api_v1/routers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
from fastapi import APIRouter
1+
from fastapi import FastAPI
22

3+
from config import settings
4+
from api_v1.users.views import router as users
35

4-
router = APIRouter(prefix='/api/v1')
6+
7+
def register_routers(app: FastAPI) -> None:
8+
app.include_router(
9+
router=users,
10+
prefix=settings.API_PREFIX,
11+
)

api_v1/users/views.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from fastapi import APIRouter, Depends, status
2+
3+
from sqlalchemy.ext.asyncio import AsyncSession
4+
5+
from config import db_connection
6+
from api_v1.exeptions import ValidationError
7+
8+
9+
router = APIRouter(prefix='/users',
10+
tags=['Users'],
11+
)
12+
13+
14+
@router.get(path='/get',
15+
description='Test end point',
16+
)
17+
async def get_user(
18+
session: AsyncSession = Depends(db_connection.session_geter),
19+
):
20+
session = session
21+
raise ValidationError(status_code=status.HTTP_400_BAD_REQUEST,
22+
detail=dict(some='Some is wrong'))

app_includes/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .logs_errors import register_errors
2+
from .middlewares import register_middlewares
3+
from .prometheus import register_prometheus
4+
5+
6+
__all__ = ('register_errors',
7+
'register_middlewares',
8+
'register_prometheus',
9+
)

app_includes/logs_errors.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import json
2+
from fastapi import FastAPI, Request
3+
from fastapi.responses import JSONResponse
4+
from fastapi.exceptions import HTTPException
5+
from starlette.exceptions import HTTPException as StarletteHTTPException
6+
7+
from http import HTTPStatus
8+
9+
from config.setup_logs.logging import logger
10+
from api_v1.exeptions import ValidationError
11+
12+
13+
def register_errors(app: FastAPI) -> None:
14+
@app.exception_handler(ValidationError)
15+
async def validation_error_handler(
16+
request: Request,
17+
exc: ValidationError,
18+
):
19+
"""
20+
Логирование всех ValidationError
21+
"""
22+
logger.opt(exception=True).warning(exc)
23+
response = dict(
24+
status=False,
25+
error_code=exc.status_code,
26+
message=exc.detail,
27+
)
28+
return JSONResponse(response)
29+
30+
@app.exception_handler(HTTPException)
31+
async def http_error_handler(
32+
request: Request,
33+
exc: HTTPException,
34+
):
35+
"""
36+
Логирование всех HTTPException
37+
"""
38+
logger.opt(exception=True).warning(exc)
39+
response = dict(
40+
status=False,
41+
error_code=exc.status_code,
42+
message=exc.detail,
43+
)
44+
return JSONResponse(response)
45+
46+
@app.exception_handler(Exception)
47+
async def error_handler(
48+
request: Request,
49+
exc: Exception,
50+
):
51+
"""
52+
Логирование всех Exception
53+
"""
54+
logger.exception(exc)
55+
response = dict(
56+
status=False,
57+
error_code=500,
58+
message=HTTPStatus(500).phrase,
59+
)
60+
return JSONResponse(response)
61+
62+
@app.exception_handler(StarletteHTTPException)
63+
async def validation_error_handler(
64+
request: Request,
65+
exc: StarletteHTTPException,
66+
):
67+
"""
68+
Логирование всех StarletteHTTPException
69+
"""
70+
logger.opt(exception=True).warning(exc)
71+
response = dict(
72+
status=False,
73+
error_code=exc.status_code,
74+
message=exc.detail,
75+
)
76+
return JSONResponse(response)

app_includes/middlewares.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from fastapi.middleware.cors import CORSMiddleware
2+
from fastapi import FastAPI
3+
4+
from config import settings
5+
6+
7+
def register_middlewares(app: FastAPI) -> None:
8+
"""
9+
Регистрация middleware
10+
"""
11+
app.add_middleware(
12+
CORSMiddleware,
13+
allow_origins=[
14+
settings.CURRENT_ORIGIN,
15+
],
16+
allow_credentials=True,
17+
allow_methods=['*'],
18+
allow_headers=['*'],
19+
)

app_includes/prometheus.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from fastapi import FastAPI
2+
from prometheus_fastapi_instrumentator import Instrumentator
3+
4+
5+
def register_prometheus(app: FastAPI) -> None:
6+
"""
7+
Регистрация Промитеуса
8+
"""
9+
Instrumentator().instrument(app=app).expose(app=app)

0 commit comments

Comments
 (0)