From 0f9f52753289d427c7d71dd048be1622927f2c42 Mon Sep 17 00:00:00 2001 From: Christian M Date: Sun, 4 Feb 2024 14:30:00 +0100 Subject: [PATCH] :sparkles: adds lib and server --- .cargo/config.toml | 3 +-- Cargo.toml | 22 ++++++++++++++++--- src/lib.rs | 1 + src/main.rs | 2 ++ src/server.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/server.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 727a0e4..fd1faa5 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -5,7 +5,7 @@ target = "xtensa-esp32-espidf" linker = "ldproxy" # runner = "espflash --monitor" # Select this runner for espflash v1.x.x runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x -rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 +rustflags = ["--cfg", "mio_unsupported_force_poll_poll", "--cfg", "espidf_time64", "-C", "default-linker-libraries"] [unstable] build-std = ["std", "panic_abort"] @@ -14,4 +14,3 @@ build-std = ["std", "panic_abort"] MCU="esp32" # Note: this variable is not used by the pio builder (`cargo build --features pio`) ESP_IDF_VERSION = "v5.1.2" - diff --git a/Cargo.toml b/Cargo.toml index 2db4aa5..803f3cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,9 @@ version = "0.1.0" authors = ["Christian M "] edition = "2021" resolver = "2" -rust-version = "1.71" + +[lib] + [profile.release] opt-level = "s" @@ -21,13 +23,27 @@ std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] alloc = ["esp-idf-svc/alloc"] nightly = ["esp-idf-svc/nightly"] experimental = ["esp-idf-svc/experimental"] -embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] +embassy = [ + "esp-idf-svc/embassy-sync", + "esp-idf-svc/critical-section", + "esp-idf-svc/embassy-time-driver", +] [dependencies] anyhow = "1.0.79" -log = { version = "0.4", default-features = false } +axum = { version = "0.7", features = ["http1", "json"] } esp-idf-sys = { version = "0.34", features = ["binstart"] } esp-idf-svc = { version = "0.48", default-features = false } +futures = "0.3" +log = { version = "0.4", default-features = false } +mio = { version = "0.8", features = ["log"] } +serde = "1" +tokio = { version = "1", features = ["rt", "net", "io-util"] } +tower-http = { version = "0" } [build-dependencies] embuild = "0.31.3" + +[dev-dependencies] +hyper-util = { version = "0.1", features = ["http1"] } +tower = { version = "0.4", features = ["util"] } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..74f47ad --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod server; diff --git a/src/main.rs b/src/main.rs index 205acea..7a4cbc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use esp32_rust_example::*; + use anyhow::Ok; use esp_idf_sys::esp_app_desc; diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..c9e4f44 --- /dev/null +++ b/src/server.rs @@ -0,0 +1,55 @@ +use axum::{response::Html, routing::get, Router}; +use log::info; +use tokio::net::TcpListener; + +const TCP_LISTENING_PORT: u16 = 80; + +pub async fn server() -> anyhow::Result<()> { + let addr = format!("0.0.0.0:{TCP_LISTENING_PORT}"); + + let app = router(); + + info!("Binding to {addr}..."); + let listener = TcpListener::bind(&addr).await?; + + axum::serve(listener, app).await?; + + Ok(()) +} + +pub fn router() -> Router { + Router::new().route("/", get(handler)) +} + +async fn handler() -> Html<&'static str> { + Html("

Hello, World!

") +} + +#[cfg(test)] +mod tests { + + use super::*; + use axum::{ + body::Body, + http::{Request, StatusCode}, + }; + use tower::util::ServiceExt; + + #[tokio::test] + async fn test_server() { + let server = router(); + + let req = Request::builder() + .method("GET") + .uri("/") + .body(Body::empty()) + .unwrap(); + + let response = server + .oneshot(req) + .await + .expect("failed to receive response"); + + assert_eq!(response.status(), StatusCode::OK); + } +}