addon
This commit is contained in:
56
backend/app/main.py
Normal file → Executable file
56
backend/app/main.py
Normal file → Executable file
@@ -6,11 +6,15 @@ from fastapi import FastAPI, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from contextlib import asynccontextmanager
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.init_db import init_db
|
||||
from app.db.session import get_db
|
||||
from app.api import benchmark, devices, links, docs
|
||||
from app.api.endpoints import peripherals, locations
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -48,6 +52,11 @@ app.include_router(devices.router, prefix=settings.API_PREFIX, tags=["Devices"])
|
||||
app.include_router(links.router, prefix=settings.API_PREFIX, tags=["Links"])
|
||||
app.include_router(docs.router, prefix=settings.API_PREFIX, tags=["Documents"])
|
||||
|
||||
# Peripherals module (if enabled)
|
||||
if settings.PERIPHERALS_MODULE_ENABLED:
|
||||
app.include_router(peripherals.router, prefix=f"{settings.API_PREFIX}/peripherals", tags=["Peripherals"])
|
||||
app.include_router(locations.router, prefix=f"{settings.API_PREFIX}/locations", tags=["Locations"])
|
||||
|
||||
|
||||
# Root endpoint
|
||||
@app.get("/")
|
||||
@@ -100,7 +109,52 @@ async def get_config():
|
||||
"""Get frontend configuration (API token, server URLs, etc.)"""
|
||||
return {
|
||||
"api_token": settings.API_TOKEN,
|
||||
"iperf_server": "10.0.1.97"
|
||||
"iperf_server": "10.0.0.50"
|
||||
}
|
||||
|
||||
def _sqlite_path(url: str) -> str:
|
||||
if url.startswith("sqlite:////"):
|
||||
return url.replace("sqlite:////", "/")
|
||||
if url.startswith("sqlite:///"):
|
||||
return url.replace("sqlite:///", "")
|
||||
return ""
|
||||
|
||||
@app.post(f"{settings.API_PREFIX}/backup")
|
||||
async def backup_databases():
|
||||
"""Create timestamped backups of the main and peripherals databases."""
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
backups = []
|
||||
|
||||
main_db = _sqlite_path(settings.DATABASE_URL)
|
||||
peripherals_db = _sqlite_path(settings.PERIPHERALS_DB_URL)
|
||||
db_paths = {
|
||||
"main": main_db,
|
||||
"peripherals": peripherals_db
|
||||
}
|
||||
|
||||
# Use main DB directory for backups
|
||||
base_dir = os.path.dirname(main_db) if main_db else "/app/data"
|
||||
backup_dir = os.path.join(base_dir, "backups")
|
||||
os.makedirs(backup_dir, exist_ok=True)
|
||||
|
||||
for key, path in db_paths.items():
|
||||
if not path or not os.path.exists(path):
|
||||
continue
|
||||
filename = f"{key}_backup_{timestamp}.db"
|
||||
dest = os.path.join(backup_dir, filename)
|
||||
shutil.copy2(path, dest)
|
||||
backups.append({
|
||||
"name": key,
|
||||
"source": path,
|
||||
"destination": dest,
|
||||
"filename": filename
|
||||
})
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"timestamp": timestamp,
|
||||
"backup_dir": backup_dir,
|
||||
"backups": backups
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user