Merge dev to main: Security improvements and comprehensive test infrastructure #39
+34
-21
@@ -10,6 +10,7 @@ from routers import docker_router, gitea, files, terminal, system, chat_summary,
|
|||||||
import auth_service as auth_module
|
import auth_service as auth_module
|
||||||
import config
|
import config
|
||||||
from rbac import require_page
|
from rbac import require_page
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import httpx
|
import httpx
|
||||||
@@ -21,8 +22,40 @@ from config import TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, CORS_ORIGINS, SECRET_KE
|
|||||||
|
|
||||||
_tz_cst = timezone(timedelta(hours=8))
|
_tz_cst = timezone(timedelta(hours=8))
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
# Startup
|
||||||
|
print("=== Application Startup ===")
|
||||||
|
|
||||||
|
# Initialize OPC database
|
||||||
|
from db import opc_db
|
||||||
|
try:
|
||||||
|
await opc_db.init_db()
|
||||||
|
print("OPC database initialized")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to initialize OPC database: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
# Start agent executor service
|
||||||
|
from services import agent_executor
|
||||||
|
try:
|
||||||
|
asyncio.create_task(agent_executor.start_executor())
|
||||||
|
print("Agent executor service started")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to start agent executor: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
asyncio.create_task(monitor_containers())
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
# Shutdown
|
||||||
|
print("=== Application Shutdown ===")
|
||||||
|
|
||||||
limiter = Limiter(key_func=get_remote_address)
|
limiter = Limiter(key_func=get_remote_address)
|
||||||
app = FastAPI(title="NAS Dashboard")
|
app = FastAPI(title="NAS Dashboard", lifespan=lifespan)
|
||||||
app.state.limiter = limiter
|
app.state.limiter = limiter
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
@@ -111,26 +144,6 @@ async def monitor_containers():
|
|||||||
|
|
||||||
await asyncio.sleep(60)
|
await asyncio.sleep(60)
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup_event():
|
|
||||||
# Initialize OPC database
|
|
||||||
from db import opc_db
|
|
||||||
try:
|
|
||||||
await opc_db.init_db()
|
|
||||||
logging.getLogger(__name__).info("OPC database initialized")
|
|
||||||
except Exception as e:
|
|
||||||
logging.getLogger(__name__).error("Failed to initialize OPC database: %s", e)
|
|
||||||
|
|
||||||
# Start agent executor service
|
|
||||||
from services import agent_executor
|
|
||||||
try:
|
|
||||||
asyncio.create_task(agent_executor.start_executor())
|
|
||||||
logging.getLogger(__name__).info("Agent executor service started")
|
|
||||||
except Exception as e:
|
|
||||||
logging.getLogger(__name__).error("Failed to start agent executor: %s", e)
|
|
||||||
|
|
||||||
asyncio.create_task(monitor_containers())
|
|
||||||
|
|
||||||
@app.get("/api/health")
|
@app.get("/api/health")
|
||||||
async def health():
|
async def health():
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|||||||
@@ -55,9 +55,10 @@ def build_host_command(host_config: dict) -> str | None:
|
|||||||
quoted_container = shlex.quote(container)
|
quoted_container = shlex.quote(container)
|
||||||
quoted_session = shlex.quote(session_name)
|
quoted_session = shlex.quote(session_name)
|
||||||
quoted_marker = shlex.quote(PERSISTENCE_STATUS_PREFIX)
|
quoted_marker = shlex.quote(PERSISTENCE_STATUS_PREFIX)
|
||||||
|
# Use login shell to ensure env vars are loaded from .bashrc
|
||||||
attach_cmd = (
|
attach_cmd = (
|
||||||
f"{docker_bin} exec -it {quoted_container} "
|
f"{docker_bin} exec -it {quoted_container} "
|
||||||
f"tmux new-session -A -s {quoted_session}"
|
f"bash -l -c 'cd /repos/nas-tools && tmux new-session -A -s {quoted_session}'"
|
||||||
)
|
)
|
||||||
|
|
||||||
script = " ".join([
|
script = " ".join([
|
||||||
|
|||||||
Reference in New Issue
Block a user