-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
338 lines (270 loc) · 10.8 KB
/
app.py
File metadata and controls
338 lines (270 loc) · 10.8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
SyncServer broker: stores schematics in named pools, exposes HTTP + WebSocket for
Paper plugin sync, optional webhooks from the plugin, and scheduled backups.
Configuration: conf.txt next to this file (or path from SCHEMSYNC_CONF).
Precedence for each setting: environment variable > conf.txt > built-in default.
Pool model
----------
A "pool" is a named group of clients that share the same schematic set.
Pools are created automatically on first use (any authenticated request).
Schematics are stored under data/schematics/<pool>/...
Backups are written to data/backups/<pool>/schematics-YYYYMMDD-HHMMSS.zip
WebSocket connections are scoped to a single pool; broadcasts only reach that pool.
"""
from __future__ import annotations
import asyncio
import os
import re
import time
import zipfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import Annotated, Optional
from fastapi import Depends, FastAPI, File, Form, Header, HTTPException, UploadFile, WebSocket, WebSocketDisconnect
from fastapi.responses import FileResponse, Response
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
_POOL_RE = re.compile(r"^[A-Za-z0-9_-]{1,64}$")
def _strip_inline_comment(value: str) -> str:
value = value.strip()
if "#" in value:
value = value.split("#", 1)[0].strip()
return value
def _load_conf_txt(path: Path) -> dict[str, str]:
out: dict[str, str] = {}
if not path.is_file():
return out
for raw_line in path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, rest = line.partition("=")
key = key.strip().lower()
val = _strip_inline_comment(rest)
if key:
out[key] = val
return out
def _conf_path() -> Path:
custom = os.environ.get("SCHEMSYNC_CONF", "").strip()
if custom:
return Path(custom).expanduser().resolve()
return (Path(__file__).resolve().parent / "conf.txt").resolve()
def _pick(conf: dict[str, str], env_name: str, conf_keys: tuple[str, ...], default: str) -> str:
env_val = os.environ.get(env_name)
if env_val is not None and str(env_val).strip() != "":
return str(env_val).strip()
for ck in conf_keys:
if ck in conf:
return conf[ck]
return default
@dataclass(frozen=True)
class BrokerSettings:
host: str
port: int
data_dir: Path
api_key: str
backup_hours: float
conf_path: Path
def load_settings() -> BrokerSettings:
conf_path = _conf_path()
conf = _load_conf_txt(conf_path)
host = _pick(conf, "SCHEMSYNC_HOST", ("host", "bind", "listen_host"), "0.0.0.0")
port_s = _pick(conf, "SCHEMSYNC_PORT", ("port",), "8756")
try:
port = int(port_s)
except ValueError:
port = 8756
data_raw = _pick(conf, "SCHEMSYNC_DATA", ("data_dir", "data", "datadir"), "data")
data_dir = Path(data_raw).expanduser()
if not data_dir.is_absolute():
data_dir = (Path(__file__).resolve().parent / data_dir).resolve()
api_key = _pick(conf, "SCHEMSYNC_API_KEY", ("api_key", "apikey", "key"), "change-me-in-production")
backup_s = _pick(conf, "SCHEMSYNC_BACKUP_HOURS", ("backup_hours", "backup_interval_hours"), "12")
try:
backup_hours = float(backup_s)
except ValueError:
backup_hours = 12.0
return BrokerSettings(
host=host,
port=port,
data_dir=data_dir,
api_key=api_key,
backup_hours=backup_hours,
conf_path=conf_path,
)
SETTINGS = load_settings()
DATA_DIR = SETTINGS.data_dir
SCHEMATICS_ROOT = DATA_DIR / "schematics"
BACKUPS_ROOT = DATA_DIR / "backups"
API_KEY = SETTINGS.api_key
BACKUP_INTERVAL_HOURS = SETTINGS.backup_hours
# ---------------------------------------------------------------------------
# Auth & path helpers
# ---------------------------------------------------------------------------
def require_api_key(x_api_key: Annotated[Optional[str], Header()] = None) -> None:
if not API_KEY:
return
if not x_api_key or x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid or missing X-API-Key")
def validate_pool(pool: str) -> str:
if not _POOL_RE.match(pool):
raise HTTPException(status_code=400, detail="Pool name must be 1–64 alphanumeric/hyphen/underscore characters")
return pool
def safe_relative_path(raw: str) -> Path:
p = Path(raw.replace("\\", "/")).as_posix()
parts = Path(p).parts
if ".." in parts or p.startswith("/"):
raise HTTPException(status_code=400, detail="Invalid path")
return Path(p)
def pool_schematics_dir(pool: str) -> Path:
d = SCHEMATICS_ROOT / pool
d.mkdir(parents=True, exist_ok=True)
return d
def pool_backups_dir(pool: str) -> Path:
d = BACKUPS_ROOT / pool
d.mkdir(parents=True, exist_ok=True)
return d
def _mtime_ms(path: Path) -> int:
return int(path.stat().st_mtime * 1000)
# ---------------------------------------------------------------------------
# FastAPI app + WebSocket state
# ---------------------------------------------------------------------------
app = FastAPI(title="SyncServer", version="2.0.0")
# pool name → set of connected WebSocket clients
_ws_pools: dict[str, set[WebSocket]] = {}
def _pool_clients(pool: str) -> set[WebSocket]:
if pool not in _ws_pools:
_ws_pools[pool] = set()
return _ws_pools[pool]
async def _broadcast(pool: str, msg: dict) -> None:
clients = _pool_clients(pool)
dead: list[WebSocket] = []
for ws in list(clients):
try:
await ws.send_json(msg)
except Exception:
dead.append(ws)
for ws in dead:
clients.discard(ws)
async def broadcast_schematic(pool: str, path: str) -> None:
await _broadcast(pool, {"event": "schematic_updated", "path": path.replace("\\", "/")})
async def broadcast_repo_resync(pool: str) -> None:
await _broadcast(pool, {"event": "repo_resync"})
# ---------------------------------------------------------------------------
# Startup / backup loop
# ---------------------------------------------------------------------------
@app.on_event("startup")
async def startup() -> None:
SCHEMATICS_ROOT.mkdir(parents=True, exist_ok=True)
BACKUPS_ROOT.mkdir(parents=True, exist_ok=True)
asyncio.create_task(backup_loop())
async def backup_loop() -> None:
while True:
await asyncio.sleep(max(3600.0, BACKUP_INTERVAL_HOURS * 3600.0))
try:
ts = time.strftime("%Y%m%d-%H%M%S")
if not SCHEMATICS_ROOT.is_dir():
continue
for pool_dir in SCHEMATICS_ROOT.iterdir():
if not pool_dir.is_dir():
continue
pool = pool_dir.name
out = pool_backups_dir(pool) / f"schematics-{ts}.zip"
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as zf:
for f in pool_dir.rglob("*"):
if f.is_file():
zf.write(f, arcname=f.relative_to(pool_dir).as_posix())
except Exception:
pass
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@app.get("/health")
def health() -> dict:
pools = [d.name for d in SCHEMATICS_ROOT.iterdir() if d.is_dir()] if SCHEMATICS_ROOT.is_dir() else []
return {"ok": True, "conf": str(SETTINGS.conf_path), "pools": sorted(pools)}
@app.get("/v1/{pool}/list", dependencies=[Depends(require_api_key)])
def list_schematics(pool: str) -> list:
validate_pool(pool)
schematics_dir = pool_schematics_dir(pool)
items: list[dict] = []
for f in schematics_dir.rglob("*"):
if f.is_file():
rel = f.relative_to(schematics_dir).as_posix()
items.append({"path": rel, "mtime_ms": _mtime_ms(f), "size": f.stat().st_size})
return items
@app.get("/v1/{pool}/file/{full_path:path}", dependencies=[Depends(require_api_key)])
def get_file(pool: str, full_path: str) -> FileResponse:
validate_pool(pool)
schematics_dir = pool_schematics_dir(pool)
rel = safe_relative_path(full_path)
target = (schematics_dir / rel).resolve()
if not str(target).startswith(str(schematics_dir.resolve())) or not target.is_file():
raise HTTPException(status_code=404, detail="Not found")
return FileResponse(
target,
media_type="application/octet-stream",
headers={"X-Mtime-Ms": str(_mtime_ms(target))},
)
@app.post("/v1/{pool}/upload", dependencies=[Depends(require_api_key)])
async def upload(
pool: str,
path: Annotated[str, Form()],
file: Annotated[UploadFile, File()],
x_client_mtime_ms: Annotated[Optional[str], Header()] = None,
x_skip_ws_broadcast: Annotated[Optional[str], Header()] = None,
) -> dict:
validate_pool(pool)
schematics_dir = pool_schematics_dir(pool)
rel = safe_relative_path(path)
dest = (schematics_dir / rel).resolve()
if not str(dest).startswith(str(schematics_dir.resolve())):
raise HTTPException(status_code=400, detail="Invalid path")
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(await file.read())
if x_client_mtime_ms:
try:
ms = int(x_client_mtime_ms) / 1000.0
os.utime(dest, (ms, ms))
except Exception:
pass
p = rel.as_posix()
if x_skip_ws_broadcast != "1":
await broadcast_schematic(pool, p)
return {"ok": True, "pool": pool, "path": p}
@app.post("/v1/{pool}/notify/repo-resync", dependencies=[Depends(require_api_key)])
async def notify_repo_resync(pool: str) -> dict:
validate_pool(pool)
pool_schematics_dir(pool) # ensure pool dir exists
await broadcast_repo_resync(pool)
return {"ok": True, "pool": pool}
@app.post("/v1/{pool}/hook/plugin", dependencies=[Depends(require_api_key)])
async def hook_plugin(pool: str, body: dict) -> Response:
validate_pool(pool)
path = str(body.get("path", "")).replace("\\", "/")
if path:
await broadcast_schematic(pool, path)
return Response(status_code=204)
@app.websocket("/v1/{pool}/ws")
async def websocket_endpoint(pool: str, ws: WebSocket) -> None:
if not _POOL_RE.match(pool):
await ws.close(code=4400)
return
token = ws.query_params.get("token")
if API_KEY and token != API_KEY:
await ws.close(code=4401)
return
await ws.accept()
clients = _pool_clients(pool)
clients.add(ws)
try:
while True:
await ws.receive_text()
except WebSocketDisconnect:
pass
finally:
clients.discard(ws)
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host=SETTINGS.host, port=SETTINGS.port, reload=False)