Skip to content

Commit

Permalink
refactor file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Oct 24, 2024
1 parent 7ca6282 commit c515224
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 95 deletions.
2 changes: 0 additions & 2 deletions src/env/heapless.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use heapless::String;

#[derive(Debug)]
//Wrapper around heapless::String to allow for TryFrom impl
pub struct HeaplessString<const N: usize>(String<N>);

impl<const N: usize> HeaplessString<N> {
Expand Down Expand Up @@ -57,5 +56,4 @@ impl<const N: usize> TryInto<heapless::String<N>> for HeaplessString<N> {

Ok(self.0)
}

}
93 changes: 0 additions & 93 deletions src/http.rs

This file was deleted.

50 changes: 50 additions & 0 deletions src/http/html.rs
Original file line number Diff line number Diff line change
@@ -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###"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Charizhard</title>
<link rel="icon" type="image/png" href="data:image/png;base64,{favicon}">
</head>
<body>
<form action="#" method="post">
<label for="user-input">Enter something:</label>
<input type="text" id="user-input" name="user-input">
<button type="submit">Submit</button>
</form>
</body>
</html>
"###
)
}

pub fn submit() -> String {

let favicon_data = include_bytes!("../../favicon.ico");
let favicon = BASE64_STANDARD.encode(favicon_data);

format!(
r###"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Charizhard</title>
<link rel="icon" type="image/png" href="data:image/png;base64,{favicon}">
</head>
<body>
Issou
</body>
</html>
"###
)
}
45 changes: 45 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -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))
}
File renamed without changes.

0 comments on commit c515224

Please sign in to comment.