File size: 1,600 Bytes
90537f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import uvicorn
import os
import socket


def get_ip_address():
    """Get the local IP address of the machine."""
    try:
        # Create a socket connection to an external server
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # Doesn't need to be reachable
        s.connect(("8.8.8.8", 80))
        ip_address = s.getsockname()[0]
        s.close()
        return ip_address
    except Exception as e:
        print(f"Error getting IP address: {e}")
        return "127.0.0.1"  # Return localhost if there's an error


if __name__ == "__main__":
    # Create static/images directory if it doesn't exist
    os.makedirs("app/static/images", exist_ok=True)

    # Check for force reset flag

    # Get the IP address
    ip_address = get_ip_address()

    # Display access information
    print("\n" + "=" * 50)

    print(f"Access from other devices at: http://{ip_address}:8000")
    print("=" * 50 + "\n")

    # Get port from environment variable (for Render deployment) or default to 8000
    port = int(os.environ.get("PORT", 8000))

    # Check if running in production (Render sets this)
    is_production = os.environ.get("RENDER") is not None

    if is_production:
        print(f"Starting production server on port {port}")
        # Production mode - no reload, bind to 0.0.0.0
        uvicorn.run("app.main:app", host="0.0.0.0", port=port, reload=False)
    else:
        # Development mode - Run the application on your IP address
        # Using 0.0.0.0 allows connections from any IP
        uvicorn.run("app.main:app", host="0.0.0.0", port=port, reload=True)