From c515224c50d76d46dbd7f0573f343f7dcc0045cd Mon Sep 17 00:00:00 2001 From: _index Date: Thu, 24 Oct 2024 10:32:49 +0200 Subject: [PATCH] refactor file structure --- src/env/heapless.rs | 2 - src/http.rs | 93 ------------------------------------ src/http/html.rs | 50 +++++++++++++++++++ src/http/mod.rs | 45 +++++++++++++++++ src/{wifi.rs => wifi/mod.rs} | 0 5 files changed, 95 insertions(+), 95 deletions(-) delete mode 100644 src/http.rs create mode 100644 src/http/html.rs create mode 100644 src/http/mod.rs rename src/{wifi.rs => wifi/mod.rs} (100%) diff --git a/src/env/heapless.rs b/src/env/heapless.rs index cc2915c..9d05417 100644 --- a/src/env/heapless.rs +++ b/src/env/heapless.rs @@ -1,7 +1,6 @@ use heapless::String; #[derive(Debug)] -//Wrapper around heapless::String to allow for TryFrom impl pub struct HeaplessString(String); impl HeaplessString { @@ -57,5 +56,4 @@ impl TryInto> for HeaplessString { Ok(self.0) } - } \ No newline at end of file diff --git a/src/http.rs b/src/http.rs deleted file mode 100644 index e19298c..0000000 --- a/src/http.rs +++ /dev/null @@ -1,93 +0,0 @@ -use esp_idf_svc::http::server::{EspHttpServer, Configuration as HttpServerConfig, Method}; -use esp_idf_svc::mdns::EspMdns; -use base64::prelude::BASE64_STANDARD; -use anyhow::Error; -use base64::Engine; - -fn index_html() -> String { - - let favicon_data = include_bytes!("../favicon.ico"); - let favicon = BASE64_STANDARD.encode(favicon_data); - - format!( - r###" - - - - - Charizhard - - - -
- - - -
- - - "### - ) -} - -fn submit() -> String { - - let favicon_data = include_bytes!("../favicon.ico"); - let favicon = BASE64_STANDARD.encode(favicon_data); - - format!( - r###" - - - - - Charizhard - - - - Issou - - - "### - ) -} - -#[allow(unused_must_use)] -pub fn start_http_server() -> anyhow::Result<(EspHttpServer<'static>, EspMdns)> { - - - let http_config = HttpServerConfig { - http_port: 80, - https_port: 443, - ..Default::default() - }; - - let mut http_server = EspHttpServer::new(&http_config)?; - - http_server.fn_handler("/", Method::Get, |request| { - - let html = index_html(); - - let mut response = request.into_ok_response()?; - - response.write(html.as_bytes())?; - Ok::<(), Error>(()) - })?; - - http_server.fn_handler("/#", Method::Get, |request| { - - let html = submit(); - - let mut response = request.into_ok_response()?; - - response.write(html.as_bytes())?; - Ok::<(), Error>(()) - }); - - let mut mdns = EspMdns::take()?; - mdns.set_hostname("charizhard")?; - mdns.add_service(Some("_http"), "_tcp", "80", 60, &[])?; - mdns.add_service(Some("_https"), "_tcp", "443", 60, &[])?; - - Ok((http_server, mdns)) -} \ No newline at end of file diff --git a/src/http/html.rs b/src/http/html.rs new file mode 100644 index 0000000..bd0c049 --- /dev/null +++ b/src/http/html.rs @@ -0,0 +1,50 @@ +use base64::Engine; +use base64::prelude::BASE64_STANDARD; + +pub fn index_html() -> String { + + let favicon_data = include_bytes!("../../favicon.ico"); + let favicon = BASE64_STANDARD.encode(favicon_data); + + format!( + r###" + + + + + Charizhard + + + +
+ + + +
+ + + "### + ) +} + +pub fn submit() -> String { + + let favicon_data = include_bytes!("../../favicon.ico"); + let favicon = BASE64_STANDARD.encode(favicon_data); + + format!( + r###" + + + + + Charizhard + + + + Issou + + + "### + ) +} \ No newline at end of file diff --git a/src/http/mod.rs b/src/http/mod.rs new file mode 100644 index 0000000..43ab432 --- /dev/null +++ b/src/http/mod.rs @@ -0,0 +1,45 @@ +use esp_idf_svc::http::server::{EspHttpServer, Configuration as HttpServerConfig, Method}; +use esp_idf_svc::mdns::EspMdns; +use anyhow::Error; + +mod html; + +#[allow(unused_must_use)] +pub fn start_http_server() -> anyhow::Result<(EspHttpServer<'static>, EspMdns)> { + + + let http_config = HttpServerConfig { + http_port: 80, + https_port: 443, + ..Default::default() + }; + + let mut http_server = EspHttpServer::new(&http_config)?; + + http_server.fn_handler("/", Method::Get, |request| { + + let html = html::index_html(); + + let mut response = request.into_ok_response()?; + + response.write(html.as_bytes())?; + Ok::<(), Error>(()) + })?; + + http_server.fn_handler("/#", Method::Get, |request| { + + let html = html::submit(); + + let mut response = request.into_ok_response()?; + + response.write(html.as_bytes())?; + Ok::<(), Error>(()) + }); + + let mut mdns = EspMdns::take()?; + mdns.set_hostname("charizhard")?; + mdns.add_service(Some("_http"), "_tcp", "80", 60, &[])?; + mdns.add_service(Some("_https"), "_tcp", "443", 60, &[])?; + + Ok((http_server, mdns)) +} \ No newline at end of file diff --git a/src/wifi.rs b/src/wifi/mod.rs similarity index 100% rename from src/wifi.rs rename to src/wifi/mod.rs