|
|
|
@@ -35,17 +35,14 @@ from starlette.middleware.cors import CORSMiddleware |
|
|
|
from starlette.responses import FileResponse |
|
|
|
from starlette.types import ASGIApp, Message, Receive, Scope, Send |
|
|
|
|
|
|
|
|
|
|
|
from models.cellular_hardware import cellularHardware |
|
|
|
from models.cellular_hardwares import cellularHardwares |
|
|
|
from models.call import call, post_call |
|
|
|
from models.calls import calls |
|
|
|
from models.httpresponse import httpResponse400, httpResponse200, httpResponse500 |
|
|
|
|
|
|
|
|
|
|
|
from fastapi_login import LoginManager |
|
|
|
|
|
|
|
|
|
|
|
from core.security import manager, NotAuthenticatedException |
|
|
|
|
|
|
|
from routes import auth as _auth |
|
|
|
@@ -66,10 +63,8 @@ reslevis_router = _reslevis.router |
|
|
|
from fastapi import FastAPI, Security |
|
|
|
from fastapi.security import OAuth2AuthorizationCodeBearer |
|
|
|
|
|
|
|
#AUTH_URL = "https://10.251.0.30:10002/realms/API.Server.local/protocol/openid-connect/auth" |
|
|
|
#AUTH_URL = "https://192.168.1.3:10002/realms/API.Server.local/protocol/openid-connect/auth" |
|
|
|
#TOKEN_URL = "https://10.251.0.30:10002/realms/API.Server.local/protocol/openid-connect/token" |
|
|
|
#TOKEN_URL = "https://192.168.1.3:10002/realms/API.Server.local/protocol/openid-connect/token" |
|
|
|
#Proxy al CORE ResLevis |
|
|
|
import httpx |
|
|
|
|
|
|
|
|
|
|
|
AUTH_URL = config_env.KEYCLOAK_AUTH_URL |
|
|
|
@@ -99,6 +94,33 @@ logging.basicConfig( |
|
|
|
format="%(levelname)s:%(name)s:%(message)s" |
|
|
|
) |
|
|
|
|
|
|
|
#ResLevis CORE Proxying |
|
|
|
CORE_BASE_URL = config_env.CORE_API_URL.rstrip("/") |
|
|
|
|
|
|
|
HOP_BY_HOP = { |
|
|
|
"connection", "keep-alive", "proxy-authenticate", "proxy-authorization", |
|
|
|
"te", "trailers", "transfer-encoding", "upgrade", |
|
|
|
} |
|
|
|
|
|
|
|
def _filter_headers(headers: dict) -> dict: |
|
|
|
return {k: v for k, v in headers.items() if k.lower() not in HOP_BY_HOP} |
|
|
|
|
|
|
|
async def _forward_to_core(request: Request, body: bytes) -> Response: |
|
|
|
url = f"{CORE_BASE_URL}{request.url.path}" |
|
|
|
async with httpx.AsyncClient(timeout=30.0) as client: |
|
|
|
resp = await client.request( |
|
|
|
request.method, |
|
|
|
url, |
|
|
|
params=request.query_params, |
|
|
|
content=body, |
|
|
|
headers=_filter_headers(dict(request.headers)), |
|
|
|
) |
|
|
|
return Response( |
|
|
|
content=resp.content, |
|
|
|
status_code=resp.status_code, |
|
|
|
headers=_filter_headers(dict(resp.headers)), |
|
|
|
media_type=resp.headers.get("content-type"), |
|
|
|
) |
|
|
|
|
|
|
|
ALLOWED_HOSTS = ["*"] |
|
|
|
|
|
|
|
@@ -110,6 +132,17 @@ app.add_middleware( |
|
|
|
allow_headers=["*"], |
|
|
|
) |
|
|
|
|
|
|
|
#ResLevis CORE middleware |
|
|
|
@app.middleware("http") |
|
|
|
async def local_then_core(request: Request, call_next): |
|
|
|
# only proxy CRUD for Reslevis (change prefix or methods if needed) |
|
|
|
if request.url.path.startswith("/reslevis/") and request.method in {"POST", "PUT", "DELETE", "PATCH"}: |
|
|
|
body = await request.body() # raw body preserved |
|
|
|
local_resp = await call_next(request) # local storage runs here |
|
|
|
if local_resp.status_code >= 400: |
|
|
|
return local_resp # stop if local failed |
|
|
|
return await _forward_to_core(request, body) |
|
|
|
return await call_next(request) |
|
|
|
|
|
|
|
@app.exception_handler(NotAuthenticatedException) |
|
|
|
def auth_exception_handler(request: Request, exc: NotAuthenticatedException): |
|
|
|
@@ -126,6 +159,7 @@ app.include_router(majornet_router) |
|
|
|
app.include_router(contacts_router) |
|
|
|
app.include_router(reslevis_router, prefix="/reslevis", tags=["Reslevis"]) |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
|
async def root(): |
|
|
|
#return {"url": "/docs"} |
|
|
|
@@ -145,8 +179,6 @@ async def get_documentation(): |
|
|
|
return get_swagger_ui_html(openapi_url="/openapi.json", title="docs") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/majortel/call/", tags=["Majortel"]) |
|
|
|
async def route_call(active_user=Depends(manager),callerNumber=None, calledNumber=None): |
|
|
|
try: |
|
|
|
@@ -361,7 +393,3 @@ async def majortel_calls_id_delete(call_id,active_user=Depends(manager)): |
|
|
|
return JSONResponse(status_code=404, content={"message": "Call not found"}) |
|
|
|
else: |
|
|
|
return JSONResponse(status_code=500, content={"message": "Server error"}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|