-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |