axum_mongodb/
mongodb_server.rs1use crate::NewWithDb;
2use axum::{async_trait, extract::FromRequestParts};
3use mongodb::Database;
4use std::convert::Infallible;
5
6#[derive(Debug, Clone)]
15pub struct MongoDbServer<T>
16where
17 T: Clone,
18{
19 pub db: Database,
20 pub servers: T,
21}
22
23#[async_trait]
24impl<T> NewWithDb for MongoDbServer<T>
25where
26 T: Clone + NewWithDb,
27{
28 async fn new(db: Database) -> Self {
29 Self {
30 servers: T::new(db.clone()).await,
31 db,
32 }
33 }
34}
35
36#[async_trait]
37impl<S, T> FromRequestParts<S> for MongoDbServer<T>
38where
39 S: Send + Sync,
40 T: Clone + Send + Sync + 'static,
41{
42 type Rejection = Infallible;
43 async fn from_request_parts(
44 parts: &mut axum::http::request::Parts,
45 _state: &S,
46 ) -> Result<Self, Self::Rejection> {
47 let dbs = parts
48 .extensions
49 .get::<Self>()
50 .expect("can not get MongoDbServer");
51
52 Ok(dbs.clone())
53 }
54}