-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
100 lines (68 loc) · 2.68 KB
/
Copy pathserver.py
File metadata and controls
100 lines (68 loc) · 2.68 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
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
import uvicorn
ROOT_DIR = Path(__file__).resolve().parent
ENV_FILE = ROOT_DIR / ".env"
def load_local_env(env_file: Path = ENV_FILE) -> None:
if not env_file.is_file():
return
for raw_line in env_file.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip().strip("\"'")
if key:
os.environ.setdefault(key, value)
BACKEND_DIR = ROOT_DIR / "backend"
AUTOSTART_DISABLED_FILE = ROOT_DIR / ".dev" / "server.autostart.disabled"
def frontend_dist_dir() -> Path:
return Path(os.getenv("PHOTOMAP_FRONTEND_DIST_DIR", ROOT_DIR / "frontend" / "dist"))
def configure_import_path() -> None:
backend_path = str(BACKEND_DIR)
if backend_path not in sys.path:
sys.path.insert(0, backend_path)
def run_migrations() -> None:
subprocess.run(
[sys.executable, "-m", "alembic", "-c", "alembic.ini", "upgrade", "head"],
check=True,
cwd=BACKEND_DIR,
)
def recover_photo_media_quarantine() -> dict[str, int]:
configure_import_path()
from sqlmodel import Session
from app.db.session import engine
from app.services.photo_media_quarantine import recover_photo_media_quarantines
with Session(engine) as session:
return recover_photo_media_quarantines(session)
def create_app():
configure_import_path()
from app.main import app
from app.runtime import mount_frontend_dist
from app.services.frontend_seo import build_frontend_seo_metadata
return mount_frontend_dist(app, frontend_dist_dir(), build_frontend_seo_metadata)
def main() -> int:
if AUTOSTART_DISABLED_FILE.exists():
print(f"PhotoMap server nie startuje: autostart wylaczony ({AUTOSTART_DISABLED_FILE}).")
return 0
load_local_env()
try:
runtime_app = create_app()
except FileNotFoundError as exc:
print(exc)
print("Zbuduj frontend: cd frontend && npm run build")
return 1
run_migrations()
recovery = recover_photo_media_quarantine()
if recovery["restored"] or recovery["discarded"]:
print("Photo media quarantine recovery: " f"restored={recovery['restored']} discarded={recovery['discarded']}")
host = os.getenv("PHOTOMAP_SERVER_HOST", "127.0.0.1")
port = int(os.getenv("PHOTOMAP_SERVER_PORT", "8000"))
uvicorn.run(runtime_app, host=host, port=port, lifespan="off", server_header=False)
return 0
if __name__ == "__main__":
raise SystemExit(main())