2 Commits

Se han modificado 5 ficheros con 50 adiciones y 16 borrados
Unificar vista
  1. +2
    -0
      .env
  2. +41
    -13
      app.py
  3. +4
    -0
      config_env.py
  4. +2
    -1
      requirements.txt
  5. +1
    -2
      routes/reslevis.py

+ 2
- 0
.env Ver fichero

@@ -19,3 +19,5 @@ KEYCLOAK_JWKS_URL=${KEYCLOAK_PROTOCOL_ENDPOINT}/certs
KEYCLOAK_AUTH_URL=${KEYCLOAK_PROTOCOL_ENDPOINT}/auth KEYCLOAK_AUTH_URL=${KEYCLOAK_PROTOCOL_ENDPOINT}/auth
KEYCLOAK_TOKEN_URL=${KEYCLOAK_PROTOCOL_ENDPOINT}/token KEYCLOAK_TOKEN_URL=${KEYCLOAK_PROTOCOL_ENDPOINT}/token


#CORE info
CORE_API_URL=http://localhost:1902

+ 41
- 13
app.py Ver fichero

@@ -35,17 +35,14 @@ from starlette.middleware.cors import CORSMiddleware
from starlette.responses import FileResponse from starlette.responses import FileResponse
from starlette.types import ASGIApp, Message, Receive, Scope, Send from starlette.types import ASGIApp, Message, Receive, Scope, Send



from models.cellular_hardware import cellularHardware from models.cellular_hardware import cellularHardware
from models.cellular_hardwares import cellularHardwares from models.cellular_hardwares import cellularHardwares
from models.call import call, post_call from models.call import call, post_call
from models.calls import calls from models.calls import calls
from models.httpresponse import httpResponse400, httpResponse200, httpResponse500 from models.httpresponse import httpResponse400, httpResponse200, httpResponse500



from fastapi_login import LoginManager from fastapi_login import LoginManager



from core.security import manager, NotAuthenticatedException from core.security import manager, NotAuthenticatedException


from routes import auth as _auth from routes import auth as _auth
@@ -66,10 +63,8 @@ reslevis_router = _reslevis.router
from fastapi import FastAPI, Security from fastapi import FastAPI, Security
from fastapi.security import OAuth2AuthorizationCodeBearer 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 AUTH_URL = config_env.KEYCLOAK_AUTH_URL
@@ -99,6 +94,33 @@ logging.basicConfig(
format="%(levelname)s:%(name)s:%(message)s" 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 = ["*"] ALLOWED_HOSTS = ["*"]


@@ -110,6 +132,17 @@ app.add_middleware(
allow_headers=["*"], 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) @app.exception_handler(NotAuthenticatedException)
def auth_exception_handler(request: Request, exc: 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(contacts_router)
app.include_router(reslevis_router, prefix="/reslevis", tags=["Reslevis"]) app.include_router(reslevis_router, prefix="/reslevis", tags=["Reslevis"])



@app.get("/") @app.get("/")
async def root(): async def root():
#return {"url": "/docs"} #return {"url": "/docs"}
@@ -145,8 +179,6 @@ async def get_documentation():
return get_swagger_ui_html(openapi_url="/openapi.json", title="docs") return get_swagger_ui_html(openapi_url="/openapi.json", title="docs")






@app.post("/majortel/call/", tags=["Majortel"]) @app.post("/majortel/call/", tags=["Majortel"])
async def route_call(active_user=Depends(manager),callerNumber=None, calledNumber=None): async def route_call(active_user=Depends(manager),callerNumber=None, calledNumber=None):
try: 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"}) return JSONResponse(status_code=404, content={"message": "Call not found"})
else: else:
return JSONResponse(status_code=500, content={"message": "Server error"}) return JSONResponse(status_code=500, content={"message": "Server error"})





+ 4
- 0
config_env.py Ver fichero

@@ -4,6 +4,7 @@ from dotenv import load_dotenv


load_dotenv() load_dotenv()


#Keycloak configuration (look in the .env)
SECRET = os.getenv("SECRET") SECRET = os.getenv("SECRET")
KEYCLOAK_AUDIENCE = os.getenv("KEYCLOAK_AUDIENCE") KEYCLOAK_AUDIENCE = os.getenv("KEYCLOAK_AUDIENCE")
KEYCLOAK_SERVER = os.getenv("KEYCLOAK_SERVER") KEYCLOAK_SERVER = os.getenv("KEYCLOAK_SERVER")
@@ -13,3 +14,6 @@ KEYCLOAK_JWKS_URL = os.getenv("KEYCLOAK_JWKS_URL")
KEYCLOAK_AUTH_URL = os.getenv("KEYCLOAK_AUTH_URL") KEYCLOAK_AUTH_URL = os.getenv("KEYCLOAK_AUTH_URL")
KEYCLOAK_TOKEN_URL = os.getenv("KEYCLOAK_TOKEN_URL") KEYCLOAK_TOKEN_URL = os.getenv("KEYCLOAK_TOKEN_URL")


#API PROXYING TO THE CORE
CORE_API_URL = os.getenv("CORE_API_URL", "http://localhost:1902")


+ 2
- 1
requirements.txt Ver fichero

@@ -36,4 +36,5 @@ urllib3==1.22
uvicorn==0.9.0 uvicorn==0.9.0
uvloop==0.13.0 uvloop==0.13.0
vobject==0.9.6.1 vobject==0.9.6.1
websockets==8.0.2
websockets==8.0.2
httpx

+ 1
- 2
routes/reslevis.py Ver fichero

@@ -289,5 +289,4 @@ def updateSubject(item: SubjectItem):
@router.delete("/removeSubject/{subject_id}", tags=["Reslevis"], dependencies=[Depends(get_current_user)]) @router.delete("/removeSubject/{subject_id}", tags=["Reslevis"], dependencies=[Depends(get_current_user)])
def removeSubject(subject_id: str): def removeSubject(subject_id: str):
subject_repo.remove(subject_id) subject_repo.remove(subject_id)
return {"message": "OK"}

return {"message": "OK"}

Cargando…
Cancelar
Guardar