Skip to main content

shared/settings/
server.rs

1use super::{
2    ExtensionSettings, SettingsDeserializeExt, SettingsDeserializer, SettingsSerializeExt,
3    SettingsSerializer,
4};
5use compact_str::ToCompactString;
6use serde::{Deserialize, Serialize};
7use utoipa::ToSchema;
8
9#[derive(Clone, ToSchema, Serialize, Deserialize)]
10pub struct AppSettingsServer {
11    pub max_file_manager_view_size: u64,
12    pub max_file_manager_content_search_size: u64,
13    pub max_file_manager_search_results: u64,
14    pub max_subuser_count: u64,
15    pub max_schedule_step_count: u64,
16    pub max_backup_group_count: u64,
17    pub max_database_instance_database_count: u64,
18    pub max_database_instance_user_count: u64,
19
20    pub allow_overwriting_custom_docker_image: bool,
21    pub allow_viewing_installation_logs: bool,
22    pub allow_acknowledging_installation_failure: bool,
23    pub allow_viewing_transfer_progress: bool,
24
25    pub container_prelude: compact_str::CompactString,
26}
27
28#[async_trait::async_trait]
29impl SettingsSerializeExt for AppSettingsServer {
30    async fn serialize(
31        &self,
32        serializer: SettingsSerializer,
33    ) -> Result<SettingsSerializer, anyhow::Error> {
34        Ok(serializer
35            .write_raw_setting(
36                "max_file_manager_view_size",
37                self.max_file_manager_view_size.to_compact_string(),
38            )
39            .write_raw_setting(
40                "max_file_manager_content_search_size",
41                self.max_file_manager_content_search_size
42                    .to_compact_string(),
43            )
44            .write_raw_setting(
45                "max_file_manager_search_results",
46                self.max_file_manager_search_results.to_compact_string(),
47            )
48            .write_raw_setting(
49                "max_subuser_count",
50                self.max_subuser_count.to_compact_string(),
51            )
52            .write_raw_setting(
53                "max_schedule_step_count",
54                self.max_schedule_step_count.to_compact_string(),
55            )
56            .write_raw_setting(
57                "max_backup_group_count",
58                self.max_backup_group_count.to_compact_string(),
59            )
60            .write_raw_setting(
61                "max_database_instance_database_count",
62                self.max_database_instance_database_count
63                    .to_compact_string(),
64            )
65            .write_raw_setting(
66                "max_database_instance_user_count",
67                self.max_database_instance_user_count.to_compact_string(),
68            )
69            .write_raw_setting(
70                "allow_overwriting_custom_docker_image",
71                self.allow_overwriting_custom_docker_image
72                    .to_compact_string(),
73            )
74            .write_raw_setting(
75                "allow_viewing_installation_logs",
76                self.allow_viewing_installation_logs.to_compact_string(),
77            )
78            .write_raw_setting(
79                "allow_acknowledging_installation_failure",
80                self.allow_acknowledging_installation_failure
81                    .to_compact_string(),
82            )
83            .write_raw_setting(
84                "allow_viewing_transfer_progress",
85                self.allow_viewing_transfer_progress.to_compact_string(),
86            )
87            .write_raw_setting("container_prelude", &*self.container_prelude))
88    }
89}
90
91pub struct AppSettingsServerDeserializer;
92
93#[async_trait::async_trait]
94impl SettingsDeserializeExt for AppSettingsServerDeserializer {
95    async fn deserialize_boxed(
96        &self,
97        mut deserializer: SettingsDeserializer<'_>,
98    ) -> Result<ExtensionSettings, anyhow::Error> {
99        Ok(Box::new(AppSettingsServer {
100            max_file_manager_view_size: deserializer
101                .take_raw_setting("max_file_manager_view_size")
102                .and_then(|s| s.parse().ok())
103                .unwrap_or(10 * 1024 * 1024),
104            max_file_manager_content_search_size: deserializer
105                .take_raw_setting("max_file_manager_content_search_size")
106                .and_then(|s| s.parse().ok())
107                .unwrap_or(5 * 1024 * 1024),
108            max_file_manager_search_results: deserializer
109                .take_raw_setting("max_file_manager_search_results")
110                .and_then(|s| s.parse().ok())
111                .unwrap_or(100),
112            max_subuser_count: deserializer
113                .take_raw_setting("max_subuser_count")
114                .and_then(|s| s.parse().ok())
115                .unwrap_or(25),
116            max_schedule_step_count: deserializer
117                .take_raw_setting("max_schedule_step_count")
118                .and_then(|s| s.parse().ok())
119                .unwrap_or(50),
120            max_backup_group_count: deserializer
121                .take_raw_setting("max_backup_group_count")
122                .and_then(|s| s.parse().ok())
123                .unwrap_or(10),
124            max_database_instance_database_count: deserializer
125                .take_raw_setting("max_database_instance_database_count")
126                .and_then(|s| s.parse().ok())
127                .unwrap_or(10),
128            max_database_instance_user_count: deserializer
129                .take_raw_setting("max_database_instance_user_count")
130                .and_then(|s| s.parse().ok())
131                .unwrap_or(10),
132            allow_overwriting_custom_docker_image: deserializer
133                .take_raw_setting("allow_overwriting_custom_docker_image")
134                .map(|s| s == "true")
135                .unwrap_or(true),
136            allow_viewing_installation_logs: deserializer
137                .take_raw_setting("allow_viewing_installation_logs")
138                .map(|s| s == "true")
139                .unwrap_or(true),
140            allow_acknowledging_installation_failure: deserializer
141                .take_raw_setting("allow_acknowledging_installation_failure")
142                .map(|s| s == "true")
143                .unwrap_or(true),
144            allow_viewing_transfer_progress: deserializer
145                .take_raw_setting("allow_viewing_transfer_progress")
146                .map(|s| s == "true")
147                .unwrap_or(true),
148            container_prelude: deserializer
149                .take_raw_setting("container_prelude")
150                .unwrap_or_else(|| "container@calagopus~".into()),
151        }))
152    }
153}