diff --git a/Cargo.lock b/Cargo.lock index 4ef37b5..e837b9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,6 +635,7 @@ dependencies = [ "env_logger", "futures", "log", + "mime", "rocket", "ructe", "serde", @@ -1165,6 +1166,7 @@ dependencies = [ "bytecount", "itertools", "md5", + "mime", "nom", ] diff --git a/Cargo.toml b/Cargo.toml index 4ee348e..439bc76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ serde_json = "1" futures = "0.3" strum_macros = "0.24" strum = { version = "0.24", features = ["derive"] } +mime = "0.3" [build-dependencies] -ructe = "0.14" +ructe = {version = "0.14", features = ["mime03"]} diff --git a/src/main.rs b/src/main.rs index ade9ef1..9bcdbdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,18 +16,18 @@ use tokio::time; type AppResult = Result>; -struct Options { - // device: DeviceType, -} +// struct Options { +// // device: DeviceType, +// } #[tokio::main] async fn main() -> AppResult<()> { env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); info!("Starting ir remote"); - let options = Arc::new(Options { - // device: DeviceType::Samsung, - }); + // let options = Arc::new(Options { + // // device: DeviceType::Samsung, + // }); let (outbound_serial_tx, outbound_serial_rx) = channel(100); let (inbound_serial_tx, inbound_serial_rx) = channel(100); @@ -37,6 +37,8 @@ async fn main() -> AppResult<()> { serial_agent::serialport_agent(inbound_serial_tx, outbound_serial_rx).await }); + // TODO: Consume inbound_serial_rx, otherwise it will fill up and eventually be dropped + // let (agent_shutdown_tx, agent_handle): (Sender<()>, JoinHandle>) = { // let (shutdown_tx, shutdown_rx) = channel(1); // ( @@ -48,29 +50,6 @@ async fn main() -> AppResult<()> { // ) // }; - // info!("Starting debugging"); - // let debugging_handle: JoinHandle> = { - // let outbound_serial_tx = outbound_serial_tx.clone(); - - // tokio::task::spawn(async move { - // info!("In debugging handle"); - // time::sleep(Duration::from_secs(10)).await; - // info!("Done waiting"); - - // let msg = format!( - // "{},{},{}\n", - // options.device.device_code(), - // options.device.function_code(&Function::Power), - // 3 - // ); - - // outbound_serial_tx.send(msg).await?; - - // info!("Done with debugging handle"); - // Ok(()) - // }) - // }; - info!("Starting webui"); let webui_handle: rocket::Shutdown = webui::launch(webui::Settings { outbound_serial_tx: outbound_serial_tx.clone(), @@ -78,9 +57,9 @@ async fn main() -> AppResult<()> { .await?; tokio::select!( - r = agent_handle => { - error!("Agent handle ended: {r:?}"); - } + // r = agent_handle => { + // error!("Agent handle ended: {r:?}"); + // } r = webui_handle => { // agent_shutdown_tx.send(()).await?; error!("Webserver handle ended: {r:?}"); @@ -97,5 +76,6 @@ async fn main() -> AppResult<()> { // }), // ); + // TODO: Graceful exit panic!("Ending program"); } diff --git a/src/webui.rs b/src/webui.rs index 42e2c76..5fe60b0 100644 --- a/src/webui.rs +++ b/src/webui.rs @@ -7,16 +7,22 @@ use log::error; use log::info; use log::warn; use rocket::form::Form; +use rocket::fs::relative; +use rocket::fs::FileServer; use rocket::get; use rocket::launch; use rocket::post; use rocket::response::Flash; use rocket::response::Redirect; +use rocket::response::Responder; use rocket::routes; use rocket::uri; use rocket::FromForm; +use rocket::Response; use rocket::State; +use template_responder::StaticResponder; use template_responder::TemplateResponder; +use templates::statics::StaticFile; use tokio::sync::mpsc::Sender; pub struct Settings { @@ -27,9 +33,6 @@ struct WebuiState { outbound_serial_tx: Sender, } -// Static files -// use templates::statics::StaticFile; - #[derive(FromForm, Debug)] struct FunctionForm { function: Function, @@ -111,17 +114,20 @@ async fn index() -> Result { Ok(TemplateResponder::from(buf)) } +#[get("/static/")] +async fn static_get(name: &str) -> impl Responder + '_ { + Some(StaticResponder(StaticFile::get(name)?)) +} + pub async fn launch(settings: Settings) -> AppResult { let state = WebuiState { outbound_serial_tx: settings.outbound_serial_tx, }; let rocket = rocket::build() - // let _rocket = rocket::build() - // .mount("/", routes![index, function, power]) - // .manage(state) - // .launch() - // .await?; - .mount("/", routes![index, function_get, function_post, power]) + .mount( + "/", + routes![index, static_get, function_get, function_post, power], + ) .manage(state) .ignite() .await?; @@ -130,6 +136,4 @@ pub async fn launch(settings: Settings) -> AppResult { tokio::task::spawn(rocket.launch()); Ok(shutdown_handle) - - // Ok(()) } diff --git a/src/webui/template_responder.rs b/src/webui/template_responder.rs index c13e440..b32c31b 100644 --- a/src/webui/template_responder.rs +++ b/src/webui/template_responder.rs @@ -1,5 +1,8 @@ -use rocket::{http::ContentType, response::Responder, Request, Response}; +use super::templates::statics::StaticFile; +use rocket::http::Header; +use rocket::{http::ContentType, response, response::Responder, Request, Response}; use std::io::Cursor; +use log::info; pub struct TemplateResponder(pub Vec); @@ -18,3 +21,16 @@ impl<'r> Responder<'r, 'static> for TemplateResponder { .ok() } } + +pub struct StaticResponder<'o>(pub &'o StaticFile); + +#[rocket::async_trait] +impl<'r, 'o: 'r> Responder<'r, 'o> for StaticResponder<'o> { + fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> { + info!("Mime type: {:?}", self.0.mime.type_().as_str()); + Response::build() + .header(Header::new("Content-Type", self.0.mime.type_().as_str())) + .sized_body(self.0.content.len(), Cursor::new(self.0.content)) + .ok() + } +} diff --git a/static/remote2.svg b/static/remote2.svg index b00a6d0..aeae532 100644 --- a/static/remote2.svg +++ b/static/remote2.svg @@ -1007,49 +1007,49 @@ kl+J8KhFCNZFmyirVvZisdfPLEpgomBYkopyNgQ+rv8A/9k= y="989.05127" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/templates/index.rs.html b/templates/index.rs.html index f3cbdb5..1dc61fb 100644 --- a/templates/index.rs.html +++ b/templates/index.rs.html @@ -8,14 +8,13 @@ Unknown Title - + + - - @@ -1026,54 +1025,59 @@ kl+J8KhFCNZFmyirVvZisdfPLEpgomBYkopyNgQ+rv8A/9k= y="989.05127" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +