Skip to content

Commit

Permalink
Update AppState / SharedState
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwayne committed Oct 31, 2024
1 parent 7c267f5 commit 6eb2cfe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion server/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::sync::Arc;

use sqlx::PgPool;

#[derive(Clone)]
pub struct AppState {
pub db: PgPool,
}

pub type SharedState = &'static AppState;
pub type SharedState = Arc<AppState>;

impl AppState {
pub fn new(db: PgPool) -> Self {
Expand Down

0 comments on commit 6eb2cfe

Please sign in to comment.