services are checked
This commit is contained in:
@@ -12,6 +12,7 @@ from handlers_exception import (
|
||||
from prometheus_fastapi_instrumentator import Instrumentator
|
||||
from prometheus_client import Counter, Histogram
|
||||
|
||||
from service_app.app_runner_init import create_endpoints_from_api_functions
|
||||
|
||||
app = create_app()
|
||||
Instrumentator().instrument(app=app).expose(app=app)
|
||||
@@ -23,12 +24,13 @@ app.add_middleware(
|
||||
"allow_credentials": True,
|
||||
"allow_methods": ["*"],
|
||||
"allow_headers": ["*"],
|
||||
}
|
||||
},
|
||||
)
|
||||
app.add_middleware(AuthHeaderMiddleware)
|
||||
|
||||
app.add_exception_handler(HTTPException, exception_handler_http)
|
||||
app.add_exception_handler(Exception, exception_handler_exception)
|
||||
create_endpoints_from_api_functions(api_app=app)
|
||||
|
||||
# # Define a counter metric
|
||||
# REQUESTS_COUNT = Counter(
|
||||
|
||||
34
service_app/app_runner_init.py
Normal file
34
service_app/app_runner_init.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from databases import EndpointRestriction
|
||||
|
||||
|
||||
def create_endpoints_from_api_functions(api_app):
|
||||
|
||||
for route in api_app.routes:
|
||||
route_path, route_summary = (
|
||||
str(getattr(route, "path")),
|
||||
str(getattr(route, "name")) or "",
|
||||
)
|
||||
# if route_path in Config.INSECURE_PATHS:
|
||||
# continue
|
||||
# print('route_path ', route_path, 'route_summary', route_summary)
|
||||
create_dict = dict(
|
||||
is_confirmed=True,
|
||||
active=True,
|
||||
deleted=False,
|
||||
is_notification_send=True,
|
||||
)
|
||||
methods = [method.lower() for method in getattr(route, "methods")]
|
||||
for route_method in methods:
|
||||
restriction = EndpointRestriction.find_or_create(
|
||||
**dict(
|
||||
endpoint_method=route_method,
|
||||
endpoint_name=route_path,
|
||||
endpoint_desc=route_summary.replace("_", " "),
|
||||
endpoint_function=route_summary,
|
||||
**create_dict,
|
||||
)
|
||||
)
|
||||
if not restriction.is_found:
|
||||
restriction.endpoint_code = f"AR{str(restriction.id).zfill(3)}"
|
||||
restriction.save()
|
||||
return
|
||||
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
def create_app():
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import JSONResponse
|
||||
@@ -9,7 +7,6 @@ def create_app():
|
||||
from api_configs import Config
|
||||
import routers
|
||||
|
||||
|
||||
api_app = FastAPI(title=str(Config.TITLE), default_response_class=JSONResponse)
|
||||
|
||||
@api_app.get("/", include_in_schema=False, summary=str(Config.DESCRIPTION))
|
||||
|
||||
@@ -6,4 +6,4 @@ from .api_exception_handlers.http_exception_handler import (
|
||||
__all__ = [
|
||||
"exception_handler_http",
|
||||
"exception_handler_exception",
|
||||
]
|
||||
]
|
||||
|
||||
@@ -7,21 +7,23 @@ from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
def exception_handler_http(request: Request, exc: HTTPException):
|
||||
print('headers', request.headers)
|
||||
print("headers", request.headers)
|
||||
detail = loads(exc.detail)
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={
|
||||
"Data": detail.get('data', {}),
|
||||
"Error": detail.get('error_case', 'UNKNOWN'),
|
||||
"Message": detail.get('message', 'An error occurred while processing the request')
|
||||
}
|
||||
"Data": detail.get("data", {}),
|
||||
"Error": detail.get("error_case", "UNKNOWN"),
|
||||
"Message": detail.get(
|
||||
"message", "An error occurred while processing the request"
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def exception_handler_exception(request: Request, exc: Exception):
|
||||
print('headers', request.headers)
|
||||
print("headers", request.headers)
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_417_EXPECTATION_FAILED, content={"message": exc.__str__()}
|
||||
status_code=status.HTTP_417_EXPECTATION_FAILED,
|
||||
content={"message": exc.__str__()},
|
||||
)
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from starlette import status
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
|
||||
class MiddlewareLogs:
|
||||
|
||||
@staticmethod
|
||||
@@ -65,7 +66,9 @@ def prepare_response_needs(response, start_time):
|
||||
|
||||
|
||||
def check_if_path_secure(request, insecure_paths) -> bool:
|
||||
return str(getattr(getattr(request, "url", None), "path", None)) not in insecure_paths
|
||||
return (
|
||||
str(getattr(getattr(request, "url", None), "path", None)) not in insecure_paths
|
||||
)
|
||||
|
||||
|
||||
def check_if_token_is_not_valid(request, endpoint_name):
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
|
||||
|
||||
__all__ = []
|
||||
|
||||
@@ -15,7 +15,6 @@ from api_validations.core_response import return_json_response_from_alchemy
|
||||
from databases import (
|
||||
BuildArea,
|
||||
Build,
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@ from fastapi.routing import APIRouter
|
||||
from fastapi.requests import Request
|
||||
|
||||
from api_validations.validations_request import (
|
||||
InsertEmployees,
|
||||
UnBindEmployees2People,
|
||||
BindEmployees2People,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
InsertEmployees,
|
||||
UnBindEmployees2People,
|
||||
BindEmployees2People,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
from api_services.redis.auth_actions.token import parse_token_object_to_dict
|
||||
|
||||
@@ -2,7 +2,9 @@ from fastapi.routing import APIRouter
|
||||
from fastapi.requests import Request
|
||||
|
||||
from api_validations.validations_request import (
|
||||
DepartmentsPydantic, PatchRecord, ListOptions
|
||||
DepartmentsPydantic,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
from api_services.redis.auth_actions.token import parse_token_object_to_dict
|
||||
|
||||
@@ -2,7 +2,10 @@ from fastapi.routing import APIRouter
|
||||
from fastapi.requests import Request
|
||||
|
||||
from api_validations.validations_request import (
|
||||
InsertPerson, UpdateUsers, PatchRecord, ListOptions
|
||||
InsertPerson,
|
||||
UpdateUsers,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
from api_services.redis.auth_actions.token import parse_token_object_to_dict
|
||||
|
||||
@@ -4,7 +4,10 @@ from fastapi.responses import JSONResponse
|
||||
from fastapi.requests import Request
|
||||
|
||||
from api_validations.validations_request import (
|
||||
InsertDecisionBook, UpdateDecisionBook, PatchRecord, ListOptions
|
||||
InsertDecisionBook,
|
||||
UpdateDecisionBook,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
from api_services.redis.auth_actions.token import parse_token_object_to_dict
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
from fastapi.routing import APIRouter
|
||||
from fastapi.requests import Request
|
||||
|
||||
from api_validations.validations_request import UpdateEndpointAccessList, InsertEndpointAccess
|
||||
from api_validations.validations_request import (
|
||||
UpdateEndpointAccessList,
|
||||
InsertEndpointAccess,
|
||||
)
|
||||
|
||||
from api_services.redis.auth_actions.token import parse_token_object_to_dict
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ from fastapi.routing import APIRouter
|
||||
from fastapi.requests import Request
|
||||
|
||||
from api_validations.validations_request import (
|
||||
InsertUsers, UpdateUsers, PatchRecord, ListOptions
|
||||
InsertUsers,
|
||||
UpdateUsers,
|
||||
PatchRecord,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
from api_services.redis.auth_actions.token import parse_token_object_to_dict
|
||||
|
||||
Reference in New Issue
Block a user