From 6eb2cfed782ee80325badcfa42819f38dc1dbae5 Mon Sep 17 00:00:00 2001 From: Rob Wagner Date: Wed, 30 Oct 2024 23:22:17 -0400 Subject: [PATCH] Update AppState / SharedState --- server/src/main.rs | 4 ++-- server/src/state.rs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 46a22d4..37058e5 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -55,7 +55,7 @@ async fn main() -> Result<(), AppError> { PgPool::connect(&db_url).await? }; - let state = Box::leak(Box::new(Arc::new(AppState::new(db)))); + let state = Arc::new(AppState::new(db)); let addr = { let host = std::env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string()); @@ -65,7 +65,7 @@ async fn main() -> Result<(), AppError> { }; let router = Router::new() - .nest("/api", api_handler(state)) + .nest("/api", api_handler(Arc::clone(&state))) .merge(static_file_handler()); tracing::info!("listening on {}", addr); diff --git a/server/src/state.rs b/server/src/state.rs index 79db973..1e642ed 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use sqlx::PgPool; #[derive(Clone)] @@ -5,7 +7,7 @@ pub struct AppState { pub db: PgPool, } -pub type SharedState = &'static AppState; +pub type SharedState = Arc; impl AppState { pub fn new(db: PgPool) -> Self {