postgresqlrustrust-axum

Deadpool-postgres connection


I'm using axum + deadpool-postgres to connect to database. In all examples I saw (like in deadpool-repo) people using axum's .with_state() with Pool. I've created async function that returns Pool, so it doesn't even compile. And I found only one solution - use Extension for this.

Any suggestions?

Thanks in advance.

UPD: code sample with error mismatched types expected unit type '()' found struct 'deadpool::managed::Pool<Manager>'

//main.rs
let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi())
        .nest("/test", test_routes())
        .with_state(database_pool().await) //here is the error
        .split_for_parts();
...
//test.rs
pub fn test_routes() -> OpenApiRouter<()> {
    OpenApiRouter::new()
    .routes(routes!(test_post_1, test_post_2))
}

Solution

  • If you convert the () from the generic of the OpenApiRouter in test_routes() to Pool<Manager>, it makes the with_state(...) expect Pool<Manager> instead of ().

    // main.rs
    let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi())
            .nest("/test", test_routes())
            .with_state(database_pool().await)
            .split_for_parts();
    
    //test.rs
    pub fn test_routes() -> OpenApiRouter<deadpool::managed::Pool<Manager>> {
        OpenApiRouter::new()
        .routes(routes!(test_post_1, test_post_2))
    }