Spaces:
Runtime error
Runtime error
add/fix: add qdrant_client to AppState and refactor AppState::new
Browse files- src/states/mod.rs +28 -7
src/states/mod.rs
CHANGED
|
@@ -1,27 +1,48 @@
|
|
|
|
|
|
|
|
| 1 |
use deadpool_diesel::{
|
| 2 |
Runtime,
|
| 3 |
postgres::{Manager, Pool},
|
| 4 |
};
|
|
|
|
| 5 |
|
| 6 |
#[derive(Clone)]
|
| 7 |
pub struct AppState {
|
| 8 |
-
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
impl AppState {
|
| 12 |
pub fn new() -> anyhow::Result<Self> {
|
| 13 |
Ok(Self {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
Runtime::Tokio1,
|
| 17 |
-
))
|
| 18 |
-
.build()?,
|
| 19 |
})
|
| 20 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
impl AppState {
|
| 24 |
pub fn pool(&self) -> &Pool {
|
| 25 |
-
&self.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
}
|
|
|
|
| 1 |
+
use std::sync::Arc;
|
| 2 |
+
|
| 3 |
use deadpool_diesel::{
|
| 4 |
Runtime,
|
| 5 |
postgres::{Manager, Pool},
|
| 6 |
};
|
| 7 |
+
use qdrant_client::Qdrant;
|
| 8 |
|
| 9 |
#[derive(Clone)]
|
| 10 |
pub struct AppState {
|
| 11 |
+
diesel_pool: Pool,
|
| 12 |
+
qdrant_client: Arc<Qdrant>,
|
| 13 |
}
|
| 14 |
|
| 15 |
impl AppState {
|
| 16 |
pub fn new() -> anyhow::Result<Self> {
|
| 17 |
Ok(Self {
|
| 18 |
+
diesel_pool: Self::diesel_pool_helper()?,
|
| 19 |
+
qdrant_client: Self::qdrant_client_helper()?,
|
|
|
|
|
|
|
|
|
|
| 20 |
})
|
| 21 |
}
|
| 22 |
+
|
| 23 |
+
fn diesel_pool_helper() -> anyhow::Result<Pool> {
|
| 24 |
+
Pool::builder(Manager::new(
|
| 25 |
+
std::env::var("DATABASE_URL")?,
|
| 26 |
+
Runtime::Tokio1,
|
| 27 |
+
))
|
| 28 |
+
.build()
|
| 29 |
+
.map_err(Into::into)
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
fn qdrant_client_helper() -> anyhow::Result<Arc<Qdrant>> {
|
| 33 |
+
Qdrant::from_url(&std::env::var("QDRANT_URL")?)
|
| 34 |
+
.build()
|
| 35 |
+
.map(Arc::new)
|
| 36 |
+
.map_err(Into::into)
|
| 37 |
+
}
|
| 38 |
}
|
| 39 |
|
| 40 |
impl AppState {
|
| 41 |
pub fn pool(&self) -> &Pool {
|
| 42 |
+
&self.diesel_pool
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
pub fn qdrant_client(&self) -> &Qdrant {
|
| 46 |
+
&self.qdrant_client
|
| 47 |
}
|
| 48 |
}
|