File size: 1,598 Bytes
6240f6b
 
 
 
 
 
6b23458
68fc6df
6240f6b
 
 
 
 
68fc6df
6240f6b
 
 
a8c4fcf
6240f6b
 
19e1360
68fc6df
6240f6b
 
 
 
1b4cc22
a8c4fcf
eb8926a
6240f6b
 
a8c4fcf
6240f6b
 
19e1360
1b4cc22
79bbee8
 
 
 
 
 
 
6b23458
 
a8c4fcf
 
 
6240f6b
 
 
 
 
 
68fc6df
 
 
 
6240f6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::sync::Arc;

use deadpool_diesel::{
    Runtime,
    postgres::{Manager, Pool},
};
use qdrant_client::Qdrant;
use translators::GoogleTranslator;

#[derive(Clone)]
pub struct AppState {
    diesel_pool: Pool,
    qdrant_client: Arc<Qdrant>,
    translator: GoogleTranslator,
}

impl AppState {
    pub async fn new() -> anyhow::Result<Self> {
        Ok(Self {
            diesel_pool: Self::diesel_pool_helper()?,
            qdrant_client: Self::qdrant_client_helper().await?,
            translator: GoogleTranslator::default(),
        })
    }

    fn diesel_pool_helper() -> anyhow::Result<Pool> {
        tracing::debug!("initializing database connection pool");
        Ok(Pool::builder(Manager::new(
            std::env::var("DATABASE_URL").expect("`DATABASE_URL` environment variable must be set"),
            Runtime::Tokio1,
        ))
        .build()?)
    }

    async fn qdrant_client_helper() -> anyhow::Result<Arc<Qdrant>> {
        tracing::debug!("initializing qdrant client");
        let client = Qdrant::from_url(
            &std::env::var("QDRANT_URL").expect("`QDRANT_URL` environment variable must be set"),
        )
        .api_key(
            std::env::var("QDRANT_API_KEY")
                .expect("`QDRANT_API_KEY` environment variable must be set"),
        )
        .build()?;
        Ok(Arc::new(client))
    }

    pub fn diesel_pool(&self) -> &Pool {
        &self.diesel_pool
    }

    pub fn qdrant_client(&self) -> &Qdrant {
        &self.qdrant_client
    }

    pub fn translator(&self) -> &GoogleTranslator {
        &self.translator
    }
}