use std::path::PathBuf; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use crate::db::Db; #[derive(Clone)] pub struct AppState { pub db: Db, /// Canonicalized server root folder. pub root: PathBuf, /// Whether we sit behind a TLS-terminating reverse proxy. pub https: bool, } #[derive(Debug, thiserror::Error)] #[error("{1}")] pub struct ApiError(pub StatusCode, pub String); impl ApiError { pub fn new(status: StatusCode, msg: impl Into) -> Self { Self(status, msg.into()) } } impl IntoResponse for ApiError { fn into_response(self) -> Response { ( self.0, axum::Json(serde_json::json!({ "error": self.1 })), ) .into_response() } } impl From for ApiError { fn from(e: rusqlite::Error) -> Self { tracing::error!(error = %e, "database error"); ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, "internal error") } }