Avoid load data for each worker + fix Clippy warning

This commit is contained in:
Florian RICHER 2024-01-06 22:23:36 +01:00
parent 921658075f
commit 7611176a8e
3 changed files with 11 additions and 4 deletions

View file

@ -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"

View file

@ -18,7 +18,7 @@ cfg_if::cfg_if! {
pub fn new() -> anyhow::Result<Self> {
let posts = crate::app::utils::data_src::get_all::<Post>("posts")?
.into_iter()
.map(|post| Arc::new(post))
.map(Arc::new)
.collect::<Vec<_>>();
let mut posts_by_slug : HashMap<String, Arc<Post>> = 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,

View file

@ -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::<models::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! { <App/> })
.service(Files::new("/", site_root))