mkv

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

lib.rs (1532B)


      1 pub mod db;
      2 pub mod error;
      3 pub mod hasher;
      4 pub mod rebalance;
      5 pub mod rebuild;
      6 pub mod server;
      7 
      8 use std::sync::Arc;
      9 use std::time::Duration;
     10 
     11 const DEFAULT_BODY_LIMIT: usize = 256 * 1024 * 1024; // 256 MB
     12 
     13 pub struct Args {
     14     pub db_path: String,
     15     pub volumes: Vec<String>,
     16     pub replicas: usize,
     17     pub voltimeout: Duration,
     18 }
     19 
     20 pub fn build_app(args: &Args) -> axum::Router {
     21     if args.replicas > args.volumes.len() {
     22         eprintln!(
     23             "Error: replication factor ({}) exceeds number of volumes ({})",
     24             args.replicas,
     25             args.volumes.len()
     26         );
     27         std::process::exit(1);
     28     }
     29 
     30     if let Some(parent) = std::path::Path::new(&args.db_path).parent() {
     31         std::fs::create_dir_all(parent).unwrap_or_else(|e| {
     32             eprintln!("Failed to create database directory: {e}");
     33             std::process::exit(1);
     34         });
     35     }
     36 
     37     let state = server::AppState {
     38         db: db::Db::new(&args.db_path),
     39         volumes: Arc::new(args.volumes.clone()),
     40         replicas: args.replicas,
     41         voltimeout: args.voltimeout,
     42         http: reqwest::Client::new(),
     43     };
     44 
     45     axum::Router::new()
     46         .route("/", axum::routing::get(server::list_keys))
     47         .route(
     48             "/{*key}",
     49             axum::routing::get(server::get_key)
     50                 .put(server::put_key)
     51                 .delete(server::delete_key)
     52                 .head(server::head_key),
     53         )
     54         .layer(axum::extract::DefaultBodyLimit::max(DEFAULT_BODY_LIMIT))
     55         .with_state(state)
     56 }