1use actix_web::dev;
6use actix_web::web::Data;
7use chrono::{DateTime, Utc};
8use parking_lot::{Mutex, RwLock};
9
10use crate::Result;
11
12pub struct GlobalState {
14 #[cfg(feature = "memorydb")]
15 pub memorydb: std::sync::Arc<dyn crate::memorydb::interface::MemoryDB>,
17
18 #[cfg(feature = "config")]
19 pub config: config::Config,
21
22 #[cfg(feature = "logger")]
23 pub logger: Option<crate::logger::Logger>,
25
26 #[cfg(feature = "i18n")]
27 pub locale: crate::i18n::Locale,
29
30 pub server: ServerHandle,
32}
33
34impl GlobalState {
35 pub fn build(self) -> Data<Self> {
37 Data::new(self)
38 }
39}
40
41#[derive(Default)]
43pub struct ServerHandle {
44 inner: Mutex<Option<dev::ServerHandle>>,
45
46 pub running: RwLock<bool>,
48 pub start_time: RwLock<DateTime<Utc>>,
50 pub stop_time: RwLock<Option<DateTime<Utc>>>,
52}
53
54impl ServerHandle {
55 pub async fn start(&self, server: dev::Server) -> Result<()> {
57 *self.inner.lock() = Some(server.handle());
58 *self.running.write() = true;
59 *self.start_time.write() = Utc::now();
60
61 server.await.map_err(Into::into)
62 }
63
64 pub fn stop(&self, graceful: bool) {
68 *self.running.write() = false;
69 *self.stop_time.write() = Some(Utc::now());
70 if let Some(handle) = self.inner.lock().as_ref() {
71 #[allow(clippy::let_underscore_future)]
72 let _ = handle.stop(graceful);
73 }
74 }
75}