diff --git a/Cargo.toml b/Cargo.toml index 744a829..3e31117 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,5 @@ tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] } [profile.release] -debug = true +debug = 1 +#lto = true # Enable link-time optimization diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index ecc2ecb..3943e98 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -1,11 +1,11 @@ use std::{borrow::Cow, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration}; use axum::{ - body::Bytes, + body::{BoxBody, Bytes}, error_handling::HandleErrorLayer, extract::{ContentLengthLimit, Extension}, - http::StatusCode, - response::IntoResponse, + http::{header, StatusCode}, + response::{IntoResponse, Response}, routing::{get_service, post}, Router, }; @@ -63,6 +63,11 @@ async fn main() { traces_sample_rate: 1.0, enable_profiling: true, profiles_sample_rate: 1.0, + environment: Some( + std::env::var("MASTIFF_ENVIRONMENT") + .unwrap_or("development".into()) + .into(), + ), ..Default::default() }, )); @@ -135,16 +140,18 @@ impl State { let threshold = self.threshold; let template = self.template.clone(); - let (matches, query_size) = tokio::task::spawn_blocking(move || { + let Ok((matches, query_size)) = tokio::task::spawn_blocking(move || { if let Some(Sketch::MinHash(mh)) = query.select_sketch(&template) { let counter = db.counter_for_query(mh); let matches = db.matches_from_counter(counter, threshold); - (matches, mh.size() as f64) + Ok((matches, mh.size() as f64)) } else { - todo!() + Err("Could not extract compatible sketch to compare") } }) - .await?; + .await? else { + return Err("Could not extract compatible sketch to compare".into()) + }; let mut csv = vec!["SRA accession,containment".into()]; csv.extend(matches.into_iter().map(|(path, size)| { @@ -157,22 +164,52 @@ impl State { })); Ok(csv) } + + fn parse_sig(&self, raw_data: &[u8]) -> Result { + let sig = Signature::from_reader(raw_data)?.swap_remove(0); + if sig.select_sketch(&self.template).is_none() { + Err(format!( + "Could not extract compatible sketch to compare. Expected k={}", + &self.template.ksize(), + ) + .into()) + } else { + Ok(sig) + } + } } async fn search( ContentLengthLimit(bytes): ContentLengthLimit, // ~5mb Extension(state): Extension, //) -> Result, StatusCode> { -) -> Result { - let sig = parse_sig(&bytes).unwrap(); - let matches = state.search(sig).await.unwrap(); - - Ok(matches.join("\n")) -} - -fn parse_sig(raw_data: &[u8]) -> Result { - let sig = Signature::from_reader(raw_data)?.swap_remove(0); - Ok(sig) +) -> Response { + let sig = match state.parse_sig(&bytes) { + Ok(sig) => sig, + Err(e) => { + return { + ( + StatusCode::BAD_REQUEST, + format!("Error parsing signature: {e}"), + ) + .into_response() + } + } + }; + + match state.search(sig).await { + Ok(matches) => ( + StatusCode::OK, + [(header::CONTENT_TYPE, "text/plain; charset=utf-8")], + matches.join("\n"), + ) + .into_response(), + Err(e) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {e}"), + ) + .into_response(), + } } async fn handle_static_serve_error(error: std::io::Error) -> impl IntoResponse { diff --git a/flake.lock b/flake.lock index 8629e72..35c718a 100644 --- a/flake.lock +++ b/flake.lock @@ -14,11 +14,11 @@ ] }, "locked": { - "lastModified": 1683134812, - "narHash": "sha256-yUiArtneEBCTYt7rOg/tLr1iv4AmjFu5tdGa0OVpjbo=", + "lastModified": 1683505101, + "narHash": "sha256-VBU64Jfu2V4sUR5+tuQS9erBRAe/QEYUxdVMcJGMZZs=", "owner": "ipetkov", "repo": "crane", - "rev": "8708b19627b2dfc2d1ac332b74383b8abdd429f0", + "rev": "7b5bd9e5acb2bb0cfba2d65f34d8568a894cdb6c", "type": "github" }, "original": { @@ -63,11 +63,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1683267615, - "narHash": "sha256-A/zAy9YauwdPut90h6cYC1zgP/WmuW9zmJ+K/c5i6uc=", + "lastModified": 1683777345, + "narHash": "sha256-V2p/A4RpEGqEZussOnHYMU6XglxBJGCODdzoyvcwig8=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "0b6445b611472740f02eae9015150c07c5373340", + "rev": "635a306fc8ede2e34cb3dd0d6d0a5d49362150ed", "type": "github" }, "original": { @@ -95,11 +95,11 @@ ] }, "locked": { - "lastModified": 1683339427, - "narHash": "sha256-CRf4eHfdud0imCZpCGrDC2rbwgJpnQZnb/kIm8D+tJM=", + "lastModified": 1683857898, + "narHash": "sha256-pyVY4UxM6zUX97g6bk6UyCbZGCWZb2Zykrne8YxacRA=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "a61fcd9910229d097ffef92b5a2440065e3b64d5", + "rev": "4e7fba3f37f5e184ada0ef3cf1e4d8ef450f240b", "type": "github" }, "original": {