File size: 1,691 Bytes
1664e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import time
import subprocess
from pathlib import Path

def get_current_ngrok_url():
    """Get current ngrok URL from ngrok API"""
    try:
        response = requests.get("http://localhost:4040/api/tunnels")
        if response.status_code == 200:
            data = response.json()
            tunnels = data.get("tunnels", [])
            for tunnel in tunnels:
                if tunnel.get("proto") == "https":
                    return tunnel.get("public_url")
    except:
        pass
    return None

def update_env_file(url):
    """Update .env file with new URL"""
    env_file = Path(".env")
    if env_file.exists():
        with open(env_file, "r") as f:
            lines = f.readlines()
        
        with open(env_file, "w") as f:
            for line in lines:
                if line.startswith("OLLAMA_HOST="):
                    f.write(f"OLLAMA_HOST={url}\n")
                else:
                    f.write(line)

def main():
    """Monitor ngrok URL changes"""
    print("Monitoring ngrok URL changes...")
    last_url = None
    
    while True:
        try:
            current_url = get_current_ngrok_url()
            if current_url and current_url != last_url:
                print(f"Ngrok URL changed: {current_url}")
                update_env_file(current_url)
                last_url = current_url
                print("Environment updated!")
            
            time.sleep(30)  # Check every 30 seconds
        except KeyboardInterrupt:
            print("Monitoring stopped.")
            break
        except Exception as e:
            print(f"Error: {e}")
            time.sleep(30)

if __name__ == "__main__":
    main()