76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
"""FastAPI HTMX app for EPUB search."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from typing import TYPE_CHECKING, Annotated
|
|
|
|
import typer
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
from sqlalchemy.orm import Session
|
|
|
|
from python.common import configure_logger
|
|
from python.ebook_search.api.bm25_tasks import cancel_bm25_refresh
|
|
from python.ebook_search.api.routes import register_admin_routes, register_page_routes, register_search_routes
|
|
from python.ebook_search.api.web import STATIC_DIR
|
|
from python.ebook_search.bm25_corpus import ensure_bm25_corpus
|
|
from python.ebook_search.config import load_config
|
|
from python.orm.common import get_postgres_engine
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import AsyncIterator
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
|
"""Manage application startup and shutdown resources."""
|
|
logger.info("ebook_search_startup")
|
|
app.state.engine = get_postgres_engine(name="RICHIE")
|
|
with Session(app.state.engine) as session:
|
|
ensure_bm25_corpus(session, app.state.config)
|
|
try:
|
|
yield
|
|
finally:
|
|
logger.info("ebook_search_shutdown")
|
|
cancel_bm25_refresh(app)
|
|
app.state.engine.dispose()
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""Create the EPUB search web app."""
|
|
app = FastAPI(title="EPUB Search", lifespan=lifespan)
|
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
|
app.state.config = load_config()
|
|
logger.info(
|
|
"ebook_search_config_loaded top_k=%s embedding_model=%s rerank_enabled=%s answer_enabled=%s library_paths=%s",
|
|
app.state.config.top_k,
|
|
app.state.config.embedding_model,
|
|
app.state.config.rerank.enabled,
|
|
app.state.config.answer_enabled,
|
|
len(app.state.config.library_paths),
|
|
)
|
|
register_page_routes(app)
|
|
register_search_routes(app)
|
|
register_admin_routes(app)
|
|
return app
|
|
|
|
|
|
def serve(
|
|
host: Annotated[str, typer.Option("--host", "-h", help="Host to bind to")] = "127.0.0.1",
|
|
port: Annotated[int, typer.Option("--port", "-p", help="Port to bind to")] = 8070,
|
|
log_level: Annotated[str, typer.Option("--log-level", "-l", help="Log level")] = "INFO",
|
|
) -> None:
|
|
"""Start the EPUB search server."""
|
|
configure_logger(log_level)
|
|
uvicorn.run(create_app(), host=host, port=port)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
typer.run(serve)
|