85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
import logging
|
|
import os
|
|
from logging.handlers import RotatingFileHandler
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse
|
|
|
|
from app.routers import ingest, query, documents, prompts, history, chunks, video, ws_asr, test_generate
|
|
from app.core.config import get_settings
|
|
from app.core.sqlite_db import (
|
|
get_prompts_db,
|
|
get_history_db,
|
|
init_prompts_db,
|
|
init_history_db,
|
|
seed_default_profiles,
|
|
)
|
|
|
|
# Configure logging before app initialization
|
|
LOG_DIR = Path(__file__).parent / "log"
|
|
LOG_DIR.mkdir(exist_ok=True)
|
|
|
|
LOG_FILE = LOG_DIR / "backend.log"
|
|
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
|
|
|
# Root logger configuration
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format=LOG_FORMAT,
|
|
handlers=[
|
|
RotatingFileHandler(
|
|
LOG_FILE,
|
|
maxBytes=10 * 1024 * 1024, # 10MB per file
|
|
backupCount=5, # Keep 5 backup files
|
|
encoding="utf-8",
|
|
),
|
|
logging.StreamHandler(), # Also keep console output
|
|
],
|
|
)
|
|
|
|
settings = get_settings()
|
|
app = FastAPI(title="RAG Video Q&A", version="1.0.0")
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(ingest.router, prefix="/api/v1")
|
|
app.include_router(query.router, prefix="/api/v1")
|
|
app.include_router(documents.router, prefix="/api/v1")
|
|
app.include_router(prompts.router)
|
|
app.include_router(history.router)
|
|
app.include_router(chunks.router)
|
|
app.include_router(video.router, prefix="/api/v1")
|
|
app.include_router(ws_asr.router)
|
|
app.include_router(test_generate.router, prefix="/api/v1")
|
|
|
|
_prompts_conn = get_prompts_db()
|
|
init_prompts_db(_prompts_conn)
|
|
seed_default_profiles(_prompts_conn)
|
|
_prompts_conn.close()
|
|
|
|
_history_conn = get_history_db()
|
|
init_history_db(_history_conn)
|
|
_history_conn.close()
|
|
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
return {"status": "ok"}
|
|
|
|
_frontend_dist = Path(__file__).resolve().parent.parent / "frontend" / "dist"
|
|
if _frontend_dist.is_dir():
|
|
@app.get("/{full_path:path}")
|
|
async def serve_frontend(full_path: str):
|
|
path = _frontend_dist / (full_path or "index.html")
|
|
if path.is_file():
|
|
return FileResponse(str(path))
|
|
return FileResponse(str(_frontend_dist / "index.html"))
|