diff --git a/Cargo.lock b/Cargo.lock index 1652e11..98e8e38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1191,6 +1191,8 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8156733e27020ea5c684db5beac5d1d611e1272ab17901a49466294b84fc217e" dependencies = [ + "axum-core", + "http", "itoa", "maud_macros", ] diff --git a/Cargo.toml b/Cargo.toml index 7bc3b56..bbdbfd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ axum = { version = "0.8.8", features = ["macros"] } chrono = "0.4.44" headless_chrome = "1.0.21" image = { version = "0.25.9", features = ["png"] } -maud = "0.27.0" +maud = { version = "0.27.0", features = ["axum"] } tempfile = "3.26.0" tokio = { version = "1.49.0", features = ["fs", "rt", "tracing"] } tower-http = { version = "0.6.8", features = ["fs", "trace"] } diff --git a/kindle.html b/kindle.html deleted file mode 100644 index 1bf6a91..0000000 --- a/kindle.html +++ /dev/null @@ -1 +0,0 @@ -
Hello banana
It is 2026-02-26 16:33:19.778042362 UTC
\ No newline at end of file diff --git a/kindle.png b/kindle.png deleted file mode 100644 index 7453f55..0000000 Binary files a/kindle.png and /dev/null differ diff --git a/src/main.rs b/src/main.rs index 0dbea02..b32e109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ +use axum::extract::State; use axum::Router; use axum::response::{IntoResponse, Response}; use axum::routing::get; use image::DynamicImage; use image::codecs::png::{PngDecoder, PngEncoder}; -use maud::html; +use maud::{Markup,html}; use std::io::{Cursor, Write}; use tokio::net::TcpListener; @@ -28,34 +29,38 @@ where } } +#[derive(Clone)] +struct AppState { + browser: Option, +} + #[axum::debug_handler] -async fn gen_png() -> Result { +async fn dashboard() -> Result { let now = chrono::prelude::Utc::now().to_string(); let markup = html! { body { div { "Hello banana" } div { (format!("It is {}", now)) } + style { + "body { + width: 600px; + height: 800px; + border: 1px solid black; + margin: auto; + }" + } } }; + Ok(markup) +} - let mut tempfile = tempfile::Builder::new() - .suffix(".html") - .rand_bytes(5) - .tempfile()?; - - tempfile - .write_all(markup.into_string().as_bytes()) - .expect("unable to write"); - - tempfile.as_file().sync_all()?; - - let launch = headless_chrome::browser::LaunchOptions::default_builder() - .window_size(Some((600, 939))) - .build()?; - let browser = headless_chrome::Browser::new(launch)?; - let tab = browser.new_tab()?; +async fn gen_png(State(state): State) -> Result { + if state.browser.is_none() { + return Err(AppError(anyhow::Error::msg("no headless chrome"))); + } + let tab = state.browser.expect("chrome not available").new_tab()?; let view = tab - .navigate_to(&format!("file://{}", tempfile.path().to_string_lossy()))? + .navigate_to("http://localhost:7777/")? .wait_for_element("body")? .get_box_model()? .margin_viewport(); @@ -80,10 +85,15 @@ async fn gen_png() -> Result { )) } async fn serve(port: &u32) -> Result<(), AppError> { + let launch = headless_chrome::browser::LaunchOptions::default_builder() + .window_size(Some((600, 939))) + .build()?; + let browser = headless_chrome::Browser::new(launch).ok(); + let app = Router::new() + .route("/", get(dashboard)) .route("/kindle.png", get(gen_png)) - //.nest_service("/kindle.png", gen_png()?) - .layer(tower_http::trace::TraceLayer::new_for_http()); + .with_state(AppState{browser}); let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).await?; println!("Starting axum on 0.0.0.0:{}...", port);