diff --git a/__pycache__/settings_dev.cpython-39.pyc b/__pycache__/settings_dev.cpython-39.pyc index dc11585..9f353c3 100644 Binary files a/__pycache__/settings_dev.cpython-39.pyc and b/__pycache__/settings_dev.cpython-39.pyc differ diff --git a/app/__pycache__/main.cpython-39.pyc b/app/__pycache__/main.cpython-39.pyc index 9d04cd2..304f03f 100644 Binary files a/app/__pycache__/main.cpython-39.pyc and b/app/__pycache__/main.cpython-39.pyc differ diff --git a/app/main.py b/app/main.py index 5acf70a..550337f 100644 --- a/app/main.py +++ b/app/main.py @@ -4,15 +4,42 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from settings_dev import settings import requests from fastapi import FastAPI, HTTPException, status +from fastapi.middleware.cors import CORSMiddleware +from starlette.middleware.base import BaseHTTPMiddleware +from starlette.responses import JSONResponse from .voipms_client import update_did_routing, get_forwardings from .database import Session from .models import Call +# class AuthMiddleware(BaseHTTPMiddleware): +# async def dispatch(self, request, call_next): +# auth_header = request.headers.get('authorization') +# expected_token = f"Bearer {settings.voipms_api_token}" +# if auth_header != expected_token: +# return JSONResponse(status_code=401, content={"detail": "Unauthorized"}) +# response = await call_next(request) +# return response + app = FastAPI( title="EAMCO VoIP.ms Controller", description="An API to manage routing for a VoIP.ms DID.", version="1.0.0", ) + +app.add_middleware( + CORSMiddleware, + allow_origins=settings.origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +@app.get("/", tags=["General"]) +def read_root(): + """A simple root endpoint to confirm the API is running.""" + return {"message": f"Welcome to the VoIP.ms API for DID: {settings.target_did}"} + +# app.add_middleware(AuthMiddleware) @app.get("/test/forwardings") def test_get_forwardings(): try: @@ -23,6 +50,8 @@ def test_get_forwardings(): } except HTTPException as e: raise e + + @app.get("/test/did") def test_did_info(): params = { @@ -40,11 +69,7 @@ def test_did_info(): return {"error": f"Failed to connect to VoIP.ms API: {str(e)}", "params": params} except Exception as e: return {"error": f"Unexpected error: {str(e)}", "params": params} -@app.get("/", tags=["General"]) -def read_root(): - """A simple root endpoint to confirm the API is running.""" - return {"message": f"Welcome to the VoIP.ms API for DID: {settings.target_did}"} - + @app.post("/route/sip", status_code=status.HTTP_200_OK, tags=["DID Routing"]) def route_to_sip_account(): @@ -123,4 +148,4 @@ def route_to_cellphone_2(): "voipms_response": result } except HTTPException as e: - raise e \ No newline at end of file + raise e diff --git a/settings_dev.py b/settings_dev.py index 9bab9fe..7f5ed4c 100644 --- a/settings_dev.py +++ b/settings_dev.py @@ -53,6 +53,9 @@ class ApplicationConfig: # VoIP.ms API endpoint voipms_api_url = os.environ.get('VOIPMS_API_URL', "https://voip.ms/api/v1/rest.php") + # API Token for Frontend Authentication + voipms_api_token = os.environ.get('VOIPMS_API_TOKEN') + # Create a single instance of the settings to be used throughout the app settings = ApplicationConfig()