-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
190 lines (153 loc) · 7.41 KB
/
run.py
File metadata and controls
190 lines (153 loc) · 7.41 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
ProtoPost launcher — zero-setup entry point for desktop users.
Run with:
python run.py # dev / direct Python
./ProtoPost-linux # compiled PyInstaller binary
./ProtoPost-macos
ProtoPost-windows.exe
Configuration priority (first match wins):
1. .env file next to executable → loaded via python-dotenv, wizard skipped
2. PORT already in environment → cloud / Docker / shell export, wizard skipped
3. init_config.json exists → previous wizard run, re-used silently
4. None of the above → interactive first-run wizard
"""
import json
import os
import sys
import threading
import webbrowser
import uvicorn
# When running as a PyInstaller bundle, _MEIPASS holds all bundled files.
# We must add it to sys.path so `import backend` resolves correctly.
if getattr(sys, "_MEIPASS", None):
sys.path.insert(0, sys._MEIPASS)
# ---------------------------------------------------------------------------
# Path helpers
# ---------------------------------------------------------------------------
def resource_path(rel: str) -> str:
"""
Resolve a path to a bundled read-only asset (e.g. frontend/).
When running inside a PyInstaller one-file bundle, assets are extracted to
sys._MEIPASS. When running as plain Python, resolve relative to this file.
"""
meipass = getattr(sys, "_MEIPASS", None)
if meipass:
return os.path.join(meipass, rel)
root = os.path.dirname(os.path.abspath(__file__))
return os.path.join(root, rel)
def data_path(filename: str) -> str:
"""
Resolve a path for runtime data files (emails.db, config.json,
init_config.json).
Always resolves next to the executable / run.py, never inside the
PyInstaller temp bundle, so data persists across launches.
"""
return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), filename)
# ---------------------------------------------------------------------------
# Skip-condition 1: .env file present
# ---------------------------------------------------------------------------
_env_file = data_path(".env")
if os.path.isfile(_env_file):
try:
from dotenv import load_dotenv
load_dotenv(_env_file)
except ImportError:
# python-dotenv not installed — parse manually so we have no hard dep
with open(_env_file, encoding="utf-8") as _f:
for _line in _f:
_line = _line.strip()
if _line and not _line.startswith("#") and "=" in _line:
_k, _, _v = _line.partition("=")
os.environ.setdefault(_k.strip(), _v.strip())
port = int(os.environ.get("PORT", 8000))
host = os.environ.get("HOST", "0.0.0.0")
auth_token = os.environ.get("AUTH_TOKEN", "")
_skip_wizard = True
# ---------------------------------------------------------------------------
# Skip-condition 2: PORT already in environment (cloud / Docker / CI)
# ---------------------------------------------------------------------------
elif "PORT" in os.environ:
port = int(os.environ["PORT"])
host = os.environ.get("HOST", "0.0.0.0")
auth_token = os.environ.get("AUTH_TOKEN", "")
_skip_wizard = True
# ---------------------------------------------------------------------------
# Normal flow: check for existing init_config.json or run wizard
# ---------------------------------------------------------------------------
else:
_init_cfg_path = data_path("init_config.json")
if os.path.isfile(_init_cfg_path):
# Silent reload from previous wizard run
with open(_init_cfg_path, encoding="utf-8") as _f:
_cfg = json.load(_f)
port = int(_cfg.get("port", 8000))
host = _cfg.get("host", "127.0.0.1")
auth_token = _cfg.get("auth_token") or ""
else:
# ── First-run wizard ──────────────────────────────────────────────
print()
print(" ╔══════════════════════════════════════╗")
print(" ║ Welcome to ProtoPost! First Setup ║")
print(" ╚══════════════════════════════════════╝")
print(' This runs once. Delete init_config.json to reconfigure.')
print()
# Port prompt
while True:
raw = input(" Which port should ProtoPost run on? [default: 8000]: ").strip()
if raw == "":
port = 8000
break
try:
port = int(raw)
if 1024 <= port <= 65535:
break
print(" Please enter a port between 1024 and 65535.")
except ValueError:
print(" Please enter a valid integer.")
# Auth token prompt
print()
print(" ─────────────────────────────────────────────")
print(" Auth Token (optional)")
print(" Protects the dashboard and all /api/* endpoints with a Bearer token.")
print(" Leave blank for no authentication — fine for local use.")
print(" Tip: generate one with: openssl rand -hex 16")
print(" ─────────────────────────────────────────────")
raw_token = input(" Auth token [leave blank to skip]: ").strip()
auth_token = raw_token if raw_token else ""
host = "127.0.0.1"
# Persist for future launches
_cfg_data = {
"port": port,
"host": host,
"auth_token": raw_token if raw_token else None,
}
with open(_init_cfg_path, "w", encoding="utf-8") as _f:
json.dump(_cfg_data, _f, indent=2)
print()
print(" \u2713 Saved to init_config.json")
print(f" \u2713 Starting ProtoPost on port {port}...")
print(" \u2713 Your browser will open automatically.")
print()
_skip_wizard = False # (not actually used below, just for clarity)
# ---------------------------------------------------------------------------
# Inject resolved config into the environment for the backend
#
# setdefault ensures we never overwrite values already present (e.g. a .env
# file or the host platform — those take precedence).
# ---------------------------------------------------------------------------
os.environ.setdefault("PORT", str(port))
os.environ.setdefault("HOST", host)
os.environ.setdefault("AUTH_TOKEN", auth_token)
# Point the backend at data files next to the executable
os.environ.setdefault("DATABASE_PATH", data_path("emails.db"))
os.environ.setdefault("CONFIG_PATH", data_path("config.json"))
# ---------------------------------------------------------------------------
# Auto-open browser
# ---------------------------------------------------------------------------
threading.Timer(1.5, lambda: webbrowser.open(f"http://localhost:{port}")).start()
# ---------------------------------------------------------------------------
# Start server
# Import app AFTER env vars are set so backend initialises with correct values
# ---------------------------------------------------------------------------
from backend.main import app as _app # noqa: E402
uvicorn.run(_app, host=host, port=port, reload=False)