1use actix_web::dev;
2use actix_web::web::Data;
3use chrono::{DateTime, Utc};
4use parking_lot::{Mutex, RwLock};
5
6use crate::Result;
7
8pub struct GlobalState {
9 #[cfg(feature = "memorydb")]
10 pub memorydb: std::sync::Arc<dyn crate::memorydb::interface::MemoryDB>,
11
12 #[cfg(feature = "config")]
13 pub config: config::Config,
14
15 #[cfg(feature = "logger")]
16 pub logger: Option<crate::logger::Logger>,
18
19 #[cfg(feature = "i18n")]
20 pub locale: crate::i18n::Locale,
21
22 pub server: ServerHandle,
24}
25
26impl GlobalState {
27 pub fn build(self) -> Data<Self> {
28 Data::new(self)
29 }
30}
31
32#[derive(Default)]
33pub struct ServerHandle {
34 inner: Mutex<Option<dev::ServerHandle>>,
35
36 pub running: RwLock<bool>,
38 pub start_time: RwLock<DateTime<Utc>>,
40 pub stop_time: RwLock<Option<DateTime<Utc>>>,
42}
43
44impl ServerHandle {
45 pub async fn start(&self, server: dev::Server) -> Result<()> {
47 *self.inner.lock() = Some(server.handle());
48 *self.running.write() = true;
49 *self.start_time.write() = Utc::now();
50
51 server.await.map_err(Into::into)
52 }
53
54 pub fn stop(&self, graceful: bool) {
56 *self.running.write() = false;
57 *self.stop_time.write() = Some(Utc::now());
58 #[allow(clippy::let_underscore_future)]
59 let _ = self.inner.lock().as_ref().unwrap().stop(graceful);
60 }
61}