mkv/src/lib.rs
2026-03-07 17:27:54 +01:00

53 lines
1.4 KiB
Rust

pub mod db;
pub mod error;
pub mod hasher;
pub mod rebalance;
pub mod rebuild;
pub mod server;
use std::sync::Arc;
const DEFAULT_BODY_LIMIT: usize = 256 * 1024 * 1024; // 256 MB
pub struct Args {
pub db_path: String,
pub volumes: Vec<String>,
pub replicas: usize,
}
pub fn build_app(args: &Args) -> axum::Router {
if args.replicas > args.volumes.len() {
eprintln!(
"Error: replication factor ({}) exceeds number of volumes ({})",
args.replicas,
args.volumes.len()
);
std::process::exit(1);
}
if let Some(parent) = std::path::Path::new(&args.db_path).parent() {
std::fs::create_dir_all(parent).unwrap_or_else(|e| {
eprintln!("Failed to create database directory: {e}");
std::process::exit(1);
});
}
let state = server::AppState {
db: db::Db::new(&args.db_path),
volumes: Arc::new(args.volumes.clone()),
replicas: args.replicas,
http: reqwest::Client::new(),
};
axum::Router::new()
.route("/", axum::routing::get(server::list_keys))
.route(
"/{*key}",
axum::routing::get(server::get_key)
.put(server::put_key)
.delete(server::delete_key)
.head(server::head_key),
)
.layer(axum::extract::DefaultBodyLimit::max(DEFAULT_BODY_LIMIT))
.with_state(state)
}