main
py 38 lines 1.25 KB
Raw
1 from helpers.api import ApiHandler, Request, Response
2 from helpers import dotenv, runtime
3 from helpers.tunnel_manager import TunnelManager
4 import requests
5
6
7 class TunnelProxy(ApiHandler):
8 async def process(self, input: dict, request: Request) -> dict | Response:
9 return await process(input)
10
11 async def process(input: dict) -> dict | Response:
12 # Get configuration from environment
13 tunnel_api_port = (
14 runtime.get_arg("tunnel_api_port")
15 or int(dotenv.get_dotenv_value("TUNNEL_API_PORT", 0))
16 or 55520
17 )
18
19 # first verify the service is running:
20 service_ok = False
21 try:
22 response = requests.post(f"http://localhost:{tunnel_api_port}/", json={"action": "health"})
23 if response.status_code == 200:
24 service_ok = True
25 except Exception as e:
26 service_ok = False
27
28 # forward this request to the tunnel service if OK
29 if service_ok:
30 try:
31 response = requests.post(f"http://localhost:{tunnel_api_port}/", json=input)
32 return response.json()
33 except Exception as e:
34 return {"error": str(e)}
35 else:
36 # forward to API handler directly
37 from api.tunnel import process as local_process
38 return await local_process(input)