diff --git a/Cargo.toml b/Cargo.toml index c315a5b..b08d54b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,12 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.70" -axum = { version = "0.6.12", features = ["tokio", "multipart"] } -axum-macros = "0.3.7" -log = "0.4.17" -pretty_env_logger = "0.4.0" -reqwest = { version = "0.11.16", features = ["json", "multipart"] } -serde = { version = "1.0.159", features = ["derive"] } -serde_json = "1.0.95" +axum = { version = "0.6.12", features = ["tokio"] } tokio = { version = "1.27.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 4560d9c..472dd26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,9 @@ -use axum::body::Bytes; -use axum::extract::{Multipart, Query}; -use axum::http::StatusCode; -use axum::response::{IntoResponse, Response}; -use axum::routing::post; -use axum::{Json, Router}; -use axum_macros::debug_handler; -use serde::{Deserialize, Serialize}; +use axum::{routing::get, Router}; #[tokio::main] async fn main() { - pretty_env_logger::init(); // build our application with a single route - let app = Router::new().route("/", post(upload_file)); + let app = Router::new().route("/", get(|| async { "Hello, World!" })); // run it with hyper on localhost:3000 axum::Server::bind(&"0.0.0.0:6679".parse().unwrap()) @@ -19,122 +11,3 @@ async fn main() { .await .unwrap(); } - -#[derive(Deserialize)] -struct Options { - threshold: String, -} - -#[derive(Serialize, Deserialize)] -#[serde(untagged)] -enum WrappedResponse { - Tags(Vec), - Error(String), -} - -// Make our own error that wraps `anyhow::Error`. -struct AppError(anyhow::Error); - -// Tell axum how to convert `AppError` into a response. -impl IntoResponse for AppError { - fn into_response(self) -> Response { - println!("amogus"); - ( - StatusCode::INTERNAL_SERVER_ERROR, - format!("Something went wrong: {}", self.0), - ) - .into_response() - } -} - -// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into -// `Result<_, AppError>`. That way you don't need to do that manually. -impl From for AppError -where - E: Into, -{ - fn from(err: E) -> Self { - Self(err.into()) - } -} - -fn try_thing() -> Result<(), anyhow::Error> { - anyhow::bail!("it failed!") -} - -async fn test_handler() -> Result<(), AppError> { - try_thing()?; - Ok(()) -} - -async fn send_image_to_dd( - file_contents: Vec, - file_name: String, - file_mime_type: &str, - options: &Options, -) -> anyhow::Result { - let part = reqwest::multipart::Part::bytes(file_contents) - .file_name(file_name) - .mime_str(file_mime_type) - .unwrap(); - let form = reqwest::multipart::Form::new().part("file", part); - - log::debug!("calling dd"); - - let resp = reqwest::Client::new() - .post("http://localhost:4443") - .multipart(form) - .header("authorization", "Bearer 123") - .query(&[("threshold", options.threshold.clone())]) - .send() - .await?; - - let body = resp.text().await?; - log::info!("body: {}", &body); - let json_response: WrappedResponse = serde_json::from_str(&body)?; - - log::debug!("called!"); - Ok(json_response) -} - -#[debug_handler] -async fn upload_file( - options: Query, - mut multipart: Multipart, -) -> Result<(StatusCode, Json), AppError> { - let mut maybe_file_contents: Option = None; - let mut maybe_file_type: Option = None; - let mut maybe_file_name: Option = None; - - while let Some(field) = multipart.next_field().await.unwrap() { - let name = field.name().unwrap().to_string(); - let content_type = field.content_type().unwrap().to_string(); - let filename = field.file_name().unwrap().to_string(); - let data = field.bytes().await.unwrap(); - - log::info!("Length of `{}` is {} bytes", name, data.len()); - if name == "file" { - maybe_file_contents = Some(data); - maybe_file_type = Some(content_type); - maybe_file_name = Some(filename); - } - } - - if let Some(file_contents) = maybe_file_contents { - let json_response = send_image_to_dd( - file_contents.to_vec(), - maybe_file_name.unwrap(), - &maybe_file_type.unwrap(), - &options, - ) - .await?; - Ok((StatusCode::OK, Json(json_response))) - } else { - Ok(( - StatusCode::INTERNAL_SERVER_ERROR, - Json(WrappedResponse::Error( - "no file found in request".to_string(), - )), - )) - } -}