Skip to content

Commit

Permalink
✨ adds lib and server
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Feb 4, 2024
1 parent 05c57e2 commit 0f9f527
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"

22 changes: 19 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ version = "0.1.0"
authors = ["Christian M <chriamue@gmail.com>"]
edition = "2021"
resolver = "2"
rust-version = "1.71"

[lib]


[profile.release]
opt-level = "s"
Expand All @@ -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"] }
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod server;
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use esp32_rust_example::*;

use anyhow::Ok;
use esp_idf_sys::esp_app_desc;

Expand Down
55 changes: 55 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -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("<h1>Hello, World!</h1>")
}

#[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);
}
}

0 comments on commit 0f9f527

Please sign in to comment.