-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_app.py
More file actions
71 lines (56 loc) · 1.79 KB
/
Copy pathdesktop_app.py
File metadata and controls
71 lines (56 loc) · 1.79 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Linux SSD Reader - Native macOS Desktop Application Launcher.
Spawns background FastAPI server and opens a native WebKit window.
"""
import os
import sys
import time
import socket
import threading
import urllib.request
import uvicorn
import webview
from server import app
def find_free_port(start_port: int = 8080) -> int:
for p in range(start_port, start_port + 100):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
if s.connect_ex(("127.0.0.1", p)) != 0:
return p
return start_port
def run_server(port: int, server_holder: dict):
config = uvicorn.Config(app, host="127.0.0.1", port=port, log_level="warning")
server = uvicorn.Server(config)
server_holder["server"] = server
server.run()
def main():
port = find_free_port(8080)
server_holder = {}
# Start background FastAPI server thread
server_thread = threading.Thread(target=run_server, args=(port, server_holder), daemon=True)
server_thread.start()
# Wait for server to become responsive
url = f"http://127.0.0.1:{port}"
for _ in range(60):
try:
req = urllib.request.Request(f"{url}/api/status")
with urllib.request.urlopen(req, timeout=0.5) as res:
if res.status == 200:
break
except Exception:
time.sleep(0.1)
# Open native macOS Cocoa WebKit window
window = webview.create_window(
title="Linux SSD Explorer",
url=url,
width=1320,
height=880,
min_size=(1000, 680),
text_select=True
)
# Start macOS GUI event loop
webview.start(debug=False)
# Clean shutdown on window close
if "server" in server_holder:
server_holder["server"].should_exit = True
if __name__ == "__main__":
main()