pub struct AppState {
pub db_path: PathBuf,
}Expand description
State shared across every HTTP handler.
Cheap to clone: a single PathBuf per instance. Axum requires the
state type to be Clone so each request handler can get its own
owned copy via the State extractor.
Fields§
§db_path: PathBufAbsolute or resolved path to the logdive index database.
Opened read-only per request; never modified by the server.
Implementations§
Source§impl AppState
impl AppState
Sourcepub fn new(db_path: PathBuf) -> Self
pub fn new(db_path: PathBuf) -> Self
Construct a new AppState for the given database path.
Does not perform any I/O — existence/readability of the file is
checked at startup in main, and each request re-opens the file
read-only via AppState::with_connection.
Sourcepub async fn with_connection<F, T>(&self, f: F) -> Result<T>
pub async fn with_connection<F, T>(&self, f: F) -> Result<T>
Run f on Tokio’s blocking-task pool with a fresh read-only
Indexer over the configured database.
Propagates the closure’s result through as-is. Any error from
opening the database, or a blocking-task join failure (which
happens only if the closure itself panics), is folded into
LogdiveError — handlers map this to an HTTP AppError at the
response boundary.
§Why spawn_blocking
rusqlite calls are synchronous and can do real work (microseconds
for a point lookup, milliseconds for a large LIKE). Running them
directly inside an async handler would block one of Tokio’s worker
threads for the duration of the query, starving other connections.
spawn_blocking hands the work to the dedicated blocking pool,
leaving the worker threads free for other async tasks.