init
Browse files- main.py → app.py +14 -10
main.py → app.py
RENAMED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
-
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.templating import Jinja2Templates
|
| 5 |
import os
|
| 6 |
import requests
|
|
@@ -9,23 +8,28 @@ from dotenv import load_dotenv
|
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 13 |
templates = Jinja2Templates(directory="templates")
|
| 14 |
|
| 15 |
def get_ngrok_tunnels():
|
| 16 |
api_key = os.getenv("NGROK_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 17 |
headers = {
|
| 18 |
"Authorization": f"Bearer {api_key}",
|
| 19 |
"Ngrok-Version": "2"
|
| 20 |
}
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
tunnels = response.json().get("tunnels", [])
|
| 24 |
-
endpoints = [
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
public_url = tunnel.get("public_url", "").replace("tcp://", "")
|
| 28 |
-
endpoints.append(public_url)
|
| 29 |
return endpoints
|
| 30 |
|
| 31 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
from fastapi.responses import HTMLResponse
|
|
|
|
| 3 |
from fastapi.templating import Jinja2Templates
|
| 4 |
import os
|
| 5 |
import requests
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 11 |
templates = Jinja2Templates(directory="templates")
|
| 12 |
|
| 13 |
def get_ngrok_tunnels():
|
| 14 |
api_key = os.getenv("NGROK_API_KEY")
|
| 15 |
+
if not api_key:
|
| 16 |
+
raise HTTPException(status_code=500, detail="Ngrok API key not found.")
|
| 17 |
+
|
| 18 |
headers = {
|
| 19 |
"Authorization": f"Bearer {api_key}",
|
| 20 |
"Ngrok-Version": "2"
|
| 21 |
}
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
response = requests.get("https://api.ngrok.com/tunnels", headers=headers)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
+
except requests.RequestException as e:
|
| 27 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 28 |
+
|
| 29 |
tunnels = response.json().get("tunnels", [])
|
| 30 |
+
endpoints = [tunnel.get("public_url", "").replace("tcp://", "")
|
| 31 |
+
for tunnel in tunnels if tunnel.get("proto") == "tcp"]
|
| 32 |
+
|
|
|
|
|
|
|
| 33 |
return endpoints
|
| 34 |
|
| 35 |
@app.get("/", response_class=HTMLResponse)
|