Skip to content

Axum middleware shares a pool of database connections #2819

Closed Answered by rico233333333
rico233333333 asked this question in Q&A
Discussion options

You must be logged in to vote

main.rs
axum 0.7.5

use axum::{
    extract::{Request, State},
    middleware::{self, Next},
    response::Response,
    routing::{get, Router},
};
use std::sync::Arc;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;

// Assume DbPool and Config are already defined
#[derive(Clone, Debug)]
struct DbPool {
    // Fields related to the database connection pool
}

#[derive(Clone, Debug)]
struct Config {
    // Fields related to configuration information
}

// Shared state struct, containing database connection pool and configuration information
#[derive(Clone)]
struct SharedState {
    db_pool: Arc<DbPool>,
    config: Arc<Config>,
}

// Example root route handler function
async fn 

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by rico233333333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant