From b77bfd92d09e880508d79a69d1cd452b92ce877e Mon Sep 17 00:00:00 2001 From: Jason Ribble Date: Thu, 17 Oct 2024 11:41:20 -0700 Subject: [PATCH] feat: write "ok" endpoint The test passes while it's running in the background --- Cargo.lock | 115 +++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 5 +++ src/api/main.rs | 28 ++++++++++++ src/api/mod.rs | 1 + src/lib.rs | 1 + 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/api/main.rs create mode 100644 src/api/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 01e56d6..ffbeb5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -162,6 +162,61 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "axum" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504e3947307ac8326a5437504c517c4b56716c9d98fac0028c2acc7ca47d70ae" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper 1.0.1", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "backtrace" version = "0.3.74" @@ -778,6 +833,12 @@ version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "hyper" version = "1.5.0" @@ -791,6 +852,7 @@ dependencies = [ "http", "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -974,6 +1036,12 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "md-5" version = "0.10.6" @@ -1074,6 +1142,7 @@ dependencies = [ "anyhow", "assert_cmd", "async-trait", + "axum", "chrono", "clap", "dotenvy", @@ -1452,7 +1521,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "sync_wrapper", + "sync_wrapper 1.0.1", "system-configuration", "tokio", "tokio-native-tls", @@ -1557,6 +1626,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" + [[package]] name = "ryu" version = "1.0.18" @@ -1633,6 +1708,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1982,6 +2067,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "sync_wrapper" version = "1.0.1" @@ -2140,6 +2231,28 @@ dependencies = [ "tokio", ] +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 0.1.2", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + [[package]] name = "tower-service" version = "0.3.3" diff --git a/Cargo.toml b/Cargo.toml index 5b0cecf..1ad00f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ authors = ["Jason Ribble "] [dependencies] anyhow = "1.0.86" async-trait = "0.1.80" +axum = "0.7.7" chrono = "0.4.38" clap = { version = "4.5.9", features = ["derive"] } dotenvy = "0.15.0" @@ -29,6 +30,10 @@ reqwest = { version = "0.12", features = ["json"] } name = "nbd-cli" path = "src/cli/main.rs" +[[bin]] +name = "nbd-api" +path = "src/api/main.rs" + [lib] name = "nbd" path = "src/lib.rs" diff --git a/src/api/main.rs b/src/api/main.rs new file mode 100644 index 0000000..a6877e0 --- /dev/null +++ b/src/api/main.rs @@ -0,0 +1,28 @@ +#![allow(dead_code)] +use axum::{routing::get, Router}; + +#[tokio::main] + +async fn main() { + // build our application with a single route + let app = Router::new().route("/ok", get(ok)); + + // run our app with hyper, listening globally on port 8080 + let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); + axum::serve(listener, app).await.unwrap(); +} + +async fn ok() -> String { + "ok".to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn test_ok_endpoint() { + let response = ok().await; + assert_eq!(response, "ok"); + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..d18669a --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1 @@ +mod main; diff --git a/src/lib.rs b/src/lib.rs index 2dab4e8..f787355 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod api; pub mod db; pub mod models; pub mod utils;