blob: ab4b9e8bd1f4ba0a4e9ff0902a33185beff55251 (
plain)
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
61
62
63
|
//! Essential data passed to routes. Used in axum's [State][axum-state]
//!
//! The state object holds essential data passed to every route that needs
//! access to connectors or the database or things of such nature.
//!
//! The main struct here is [SplormState]
//!
//! [axum-state]: https://docs.rs/axum/latest/axum/extract/struct.State.html
use std::sync::Arc;
use somethings::DatabaseHandle;
use tokio::sync::{Mutex, MutexGuard};
use crate::{config::Settings, connector::GathererConnector};
/// Main state structure.
///
/// Holds the state for the application. It is cheap to clone as all
/// of it's fields are wrapped in an [Arc].
#[derive(Clone)]
pub struct SplormState {
connectors: ConnectorsState,
dbh: DatabaseHandle,
}
impl SplormState {
pub fn new(connectors: ConnectorsState, settings: &Settings) -> Self {
Self {
connectors,
dbh: DatabaseHandle::open(settings.database_path().as_std_path()).unwrap(),
}
}
pub async fn gatherer_thread<'s>(&'s self) -> MutexGuard<'s, GathererConnector> {
self.connectors.gatherer.lock().await
}
pub fn database(&self) -> &DatabaseHandle {
&self.dbh
}
pub async fn init_database(&self) {
knowing::create_table(self.database()).await.unwrap();
}
}
/// Connectors to outside services.
///
/// Routes access this structure if they want to call services not within
/// splorm itself. Connectors are wrapped in `Arc<Mutex<Connector>>` as needed.
#[derive(Clone)]
pub struct ConnectorsState {
gatherer: Arc<Mutex<GathererConnector>>,
}
impl ConnectorsState {
pub fn new(gatherer: GathererConnector) -> Self {
Self {
gatherer: Arc::new(Mutex::new(gatherer)),
}
}
}
|