From 7611176a8e3ce8355724a934366e5d2cd2ad3015 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sat, 6 Jan 2024 22:23:36 +0100 Subject: [PATCH] Avoid load data for each worker + fix Clippy warning --- Dockerfile | 2 +- src/app/models/mod.rs | 7 +++++-- src/main.rs | 6 +++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 644d00a..c6a6a6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,7 @@ COPY --from=builder /app/data_src /app/data_src WORKDIR /app # Set any required env variables and -ENV RUST_LOG="info" +ENV RUST_LOG="warn" ENV APP_ENVIRONMENT="production" ENV LEPTOS_SITE_ADDR="0.0.0.0:8080" ENV LEPTOS_SITE_ROOT="site" diff --git a/src/app/models/mod.rs b/src/app/models/mod.rs index 9375f3d..73f805a 100644 --- a/src/app/models/mod.rs +++ b/src/app/models/mod.rs @@ -18,7 +18,7 @@ cfg_if::cfg_if! { pub fn new() -> anyhow::Result { let posts = crate::app::utils::data_src::get_all::("posts")? .into_iter() - .map(|post| Arc::new(post)) + .map(Arc::new) .collect::>(); let mut posts_by_slug : HashMap> = HashMap::new(); @@ -29,11 +29,14 @@ cfg_if::cfg_if! { for tag in &post.metadata.tags { let tag = tag.to_lowercase(); - let posts = posts_by_tag.entry(tag).or_insert(Vec::new()); + let posts = posts_by_tag.entry(tag).or_default(); posts.push(post.clone()); } } + log::info!("Loaded {} posts", posts.len()); + log::info!("Found {} tags", posts_by_tag.len()); + Ok(Self { posts, posts_by_slug, diff --git a/src/main.rs b/src/main.rs index fac6503..fb799b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,14 @@ cfg_if! { async fn main() -> std::io::Result<()> { // Configure logger SimpleLogger::new() + .with_level(log::LevelFilter::Info) .env() .init() .unwrap(); + // Load required data + let data = web::Data::::new(models::Data::new().unwrap()); + // Setting this to None means we'll be using cargo-leptos and its env vars. let conf = get_configuration(None).await.unwrap(); @@ -31,7 +35,7 @@ cfg_if! { let site_root = &leptos_options.site_root; let routes = &routes; App::new() - .app_data(web::Data::new(app::models::Data::new().unwrap())) // Must panic if data can't be loaded + .app_data(web::Data::clone(&data)) // Must panic if data can't be loaded .route("/api/{tail:.*}", leptos_actix::handle_server_fns()) .leptos_routes(leptos_options.to_owned(), routes.to_owned(), || view! { }) .service(Files::new("/", site_root))