Upload main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
import importlib.util
|
| 3 |
from fastapi import FastAPI
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
if not app_file.exists():
|
| 9 |
raise RuntimeError("app.py not found in project root; expected FastAPI app instance in app.py")
|
| 10 |
spec = importlib.util.spec_from_file_location("root_app_module", app_file)
|
|
@@ -18,4 +21,35 @@ def load_fastapi_app() -> FastAPI:
|
|
| 18 |
raise RuntimeError("`app` in app.py is not a FastAPI instance")
|
| 19 |
return fastapi_app
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
import importlib.util
|
| 3 |
from fastapi import FastAPI
|
| 4 |
+
from fastapi.responses import HTMLResponse
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
|
| 7 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 8 |
+
|
| 9 |
+
def load_backend_app() -> FastAPI:
|
| 10 |
+
app_file = BASE_DIR / "app.py"
|
| 11 |
if not app_file.exists():
|
| 12 |
raise RuntimeError("app.py not found in project root; expected FastAPI app instance in app.py")
|
| 13 |
spec = importlib.util.spec_from_file_location("root_app_module", app_file)
|
|
|
|
| 21 |
raise RuntimeError("`app` in app.py is not a FastAPI instance")
|
| 22 |
return fastapi_app
|
| 23 |
|
| 24 |
+
backend_app = load_backend_app()
|
| 25 |
+
|
| 26 |
+
app = FastAPI(title="Crypto Monitor Shell")
|
| 27 |
+
|
| 28 |
+
# Mount original FastAPI app under /api so all existing routes are preserved.
|
| 29 |
+
app.mount("/api", backend_app)
|
| 30 |
+
|
| 31 |
+
# Optionally serve /static if directory exists (for CSS/JS assets).
|
| 32 |
+
static_dir = BASE_DIR / "static"
|
| 33 |
+
if static_dir.is_dir():
|
| 34 |
+
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
| 35 |
+
|
| 36 |
+
@app.get("/", response_class=HTMLResponse)
|
| 37 |
+
async def serve_index():
|
| 38 |
+
"""Serve the HTML dashboard as the root UI.
|
| 39 |
+
|
| 40 |
+
Tries a few common locations for index.html.
|
| 41 |
+
"""
|
| 42 |
+
candidates = [
|
| 43 |
+
BASE_DIR / "index.html",
|
| 44 |
+
BASE_DIR / "INDEX.html",
|
| 45 |
+
BASE_DIR / "templates" / "index.html",
|
| 46 |
+
BASE_DIR / "templates" / "INDEX.html",
|
| 47 |
+
]
|
| 48 |
+
for path in candidates:
|
| 49 |
+
if path.is_file():
|
| 50 |
+
return HTMLResponse(path.read_text(encoding="utf-8"))
|
| 51 |
+
# Fallback: minimal JSON so app is not broken even if index is missing.
|
| 52 |
+
return HTMLResponse(
|
| 53 |
+
"<h1>Crypto Monitor backend is running</h1><p>index.html not found in project root.</p>",
|
| 54 |
+
status_code=200,
|
| 55 |
+
)
|