darkwing/server/services/
mod.rs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
pub mod browser_profile_services;
pub mod cloud_file_services;
pub mod config_encryption_services;
pub mod database_encryption_services;
pub mod local_file_services;
pub mod sqlite_services;
pub mod status_services;
pub mod user_services;

use std::sync::Arc;

use browser_profile_services::{
  BrowserProfileService, DynBrowserProfileService,
};
use cloud_file_services::CloudFileService;
use config_encryption_services::{
  ConfigEncryptionService, DynConfigEncryption,
};
use database_encryption_services::{
  DatabaseEncryptionService, DynDatabaseEncryption,
};
use local_file_services::{DynLocalFileService, LocalFileService};
use metrics_exporter_prometheus::PrometheusHandle;
use sqlite_services::{DynSqliteService, SqliteService};
use status_services::StatusService;
use tracing::info;
use user_services::UsersService;

use crate::cache::Cache;
use crate::{
  config::DarkwingConfig, database::Database,
  server::utils::jwt_utils::JwtTokenUtil,
};

use self::cloud_file_services::DynCloudFileService;
use self::status_services::DynStatusService;
use self::user_services::DynUsersService;
use super::utils::jwt_utils::DynJwtUtil;

#[derive(Clone)]
pub struct Services {
  #[allow(unused)]
  pub recorder_handle: PrometheusHandle,
  pub jwt_util: DynJwtUtil,
  pub config_encryption: DynConfigEncryption,
  #[allow(unused)]
  pub database_encryption: DynDatabaseEncryption,
  pub users: DynUsersService,
  pub status: DynStatusService,
  pub cloud_files: DynCloudFileService,
  pub local_files: DynLocalFileService,
  pub browser_profile_service: DynBrowserProfileService,
  pub sqlite: DynSqliteService,
}

impl Services {
  pub async fn new(
    db: Database,
    cache: Cache,
    config: Arc<DarkwingConfig>,
    recorder_handle: PrometheusHandle,
  ) -> Self {
    info!("initializing utility services...");
    let jwt_util = Arc::new(JwtTokenUtil::new(config.clone())) as DynJwtUtil;
    let config_encryption =
      Arc::new(ConfigEncryptionService::new(config.clone()))
        as DynConfigEncryption;
    let sqlite = Arc::new(SqliteService::new()) as DynSqliteService;
    let local_files = Arc::new(LocalFileService::new()) as DynLocalFileService;

    info!("utility services initialized, building feature services...");

    let repository = Arc::new(db);
    let cache = Arc::new(cache);

    let database_encryption = Arc::new(DatabaseEncryptionService::new(
      repository.clone(),
      cache.clone(),
      config.clone(),
    )) as DynDatabaseEncryption;

    let users =
      Arc::new(UsersService::new(repository.clone(), repository.clone()))
        as DynUsersService;

    let cloud_files = Arc::new(CloudFileService::new(config.clone()).await)
      as DynCloudFileService;

    let status = Arc::new(StatusService::new(
      repository.clone(),
      cache.clone(),
      config.clone(),
    )) as DynStatusService;

    let browser_profile_service = Arc::new(BrowserProfileService::new(
      config.clone(),
      repository.clone(),
      repository.clone(),
      repository.clone(),
      repository.clone(),
      database_encryption.clone(),
    )) as DynBrowserProfileService;

    Self {
      recorder_handle,
      jwt_util,
      config_encryption,
      users,
      status,
      cloud_files,
      local_files,
      browser_profile_service,
      sqlite,
      database_encryption,
    }
  }
}