-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
54 lines (43 loc) · 1.46 KB
/
run.py
File metadata and controls
54 lines (43 loc) · 1.46 KB
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
import os
import socket
import threading
import time
import webbrowser
from configparser import ConfigParser
from pathlib import Path
from dotenv import load_dotenv
from app import create_app
# Load environment variables from .env file
load_dotenv()
def load_server_config():
"""Load server host and port from shotbuddy.cfg if it exists."""
cfg_path = Path(__file__).with_name("shotbuddy.cfg")
host = None
port = None
if cfg_path.exists():
parser = ConfigParser()
parser.read(cfg_path)
if parser.has_section("server"):
host = parser.get("server", "host", fallback=None)
port = parser.getint("server", "port", fallback=None)
return host, port
app = create_app()
if __name__ == "__main__":
cfg_host, cfg_port = load_server_config()
host = os.environ.get("SHOTBUDDY_HOST", cfg_host or "127.0.0.1")
port = int(os.environ.get("SHOTBUDDY_PORT", cfg_port or 5001))
debug = os.environ.get("SHOTBUDDY_DEBUG", "0").lower() in {"1", "true", "yes"}
def _open_browser_when_ready(url):
while True:
try:
with socket.create_connection((host, port), timeout=1):
break
except OSError:
time.sleep(0.1)
webbrowser.open_new(url)
threading.Thread(
target=_open_browser_when_ready,
args=(f"http://{host}:{port}/",),
daemon=True,
).start()
app.run(debug=debug, host=host, port=port)