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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::path::Path;
#[cfg(feature = "encryption")]
use bonsaidb_core::document::KeyId;
use bonsaidb_core::{permissions::Permissions, schema::Schema};
#[cfg(feature = "compression")]
use bonsaidb_local::config::Compression;
use bonsaidb_local::config::{Builder, KeyValuePersistence, StorageConfiguration};
#[cfg(feature = "encryption")]
use bonsaidb_local::vault::AnyVaultKeyStorage;
#[derive(Debug, Clone)]
#[must_use]
#[non_exhaustive]
pub struct ServerConfiguration {
pub server_name: String,
pub client_simultaneous_request_limit: usize,
pub request_workers: usize,
pub storage: StorageConfiguration,
pub default_permissions: DefaultPermissions,
pub authenticated_permissions: DefaultPermissions,
#[cfg(feature = "acme")]
pub acme: AcmeConfiguration,
}
impl ServerConfiguration {
pub fn server_name(mut self, server_name: impl Into<String>) -> Self {
self.server_name = server_name.into();
self
}
pub const fn client_simultaneous_request_limit(mut self, request_limit: usize) -> Self {
self.client_simultaneous_request_limit = request_limit;
self
}
pub const fn request_workers(mut self, workers: usize) -> Self {
self.request_workers = workers;
self
}
pub fn default_permissions<P: Into<DefaultPermissions>>(
mut self,
default_permissions: P,
) -> Self {
self.default_permissions = default_permissions.into();
self
}
pub fn authenticated_permissions<P: Into<DefaultPermissions>>(
mut self,
authenticated_permissions: P,
) -> Self {
self.authenticated_permissions = authenticated_permissions.into();
self
}
#[cfg(feature = "acme")]
pub fn acme_contact_email(mut self, contact_email: impl Into<String>) -> Self {
self.acme.contact_email = Some(contact_email.into());
self
}
#[cfg(feature = "acme")]
pub fn acme_directory(mut self, directory: impl Into<String>) -> Self {
self.acme.directory = directory.into();
self
}
}
impl Default for ServerConfiguration {
fn default() -> Self {
Self {
server_name: String::from("bonsaidb"),
client_simultaneous_request_limit: 16,
request_workers: 16,
storage: bonsaidb_local::config::StorageConfiguration::default(),
default_permissions: DefaultPermissions::Permissions(Permissions::default()),
authenticated_permissions: DefaultPermissions::Permissions(Permissions::default()),
#[cfg(feature = "acme")]
acme: AcmeConfiguration::default(),
}
}
}
#[cfg(feature = "acme")]
mod acme {
#[derive(Debug, Clone)]
pub struct AcmeConfiguration {
pub contact_email: Option<String>,
pub directory: String,
}
impl Default for AcmeConfiguration {
fn default() -> Self {
Self {
contact_email: None,
directory: LETS_ENCRYPT_PRODUCTION_DIRECTORY.to_string(),
}
}
}
pub use async_acme::acme::{LETS_ENCRYPT_PRODUCTION_DIRECTORY, LETS_ENCRYPT_STAGING_DIRECTORY};
}
#[cfg(feature = "acme")]
pub use acme::*;
#[derive(Debug, Clone)]
pub enum DefaultPermissions {
AllowAll,
Permissions(Permissions),
}
impl From<DefaultPermissions> for Permissions {
fn from(permissions: DefaultPermissions) -> Self {
match permissions {
DefaultPermissions::Permissions(permissions) => permissions,
DefaultPermissions::AllowAll => Self::allow_all(),
}
}
}
impl From<Permissions> for DefaultPermissions {
fn from(permissions: Permissions) -> Self {
Self::Permissions(permissions)
}
}
impl Builder for ServerConfiguration {
fn with_schema<S: Schema>(mut self) -> Result<Self, bonsaidb_local::Error> {
self.storage.register_schema::<S>()?;
Ok(self)
}
fn path<P: AsRef<Path>>(mut self, path: P) -> Self {
self.storage.path = Some(path.as_ref().to_owned());
self
}
fn memory_only(mut self) -> Self {
self.storage.memory_only = true;
self
}
fn unique_id(mut self, unique_id: u64) -> Self {
self.storage.unique_id = Some(unique_id);
self
}
#[cfg(feature = "encryption")]
fn vault_key_storage<VaultKeyStorage: AnyVaultKeyStorage>(
mut self,
key_storage: VaultKeyStorage,
) -> Self {
self.storage.vault_key_storage = Some(std::sync::Arc::new(key_storage));
self
}
#[cfg(feature = "encryption")]
fn default_encryption_key(mut self, key: KeyId) -> Self {
self.storage.default_encryption_key = Some(key);
self
}
#[cfg(feature = "compression")]
fn default_compression(mut self, compression: Compression) -> Self {
self.storage.default_compression = Some(compression);
self
}
fn tasks_worker_count(mut self, worker_count: usize) -> Self {
self.storage.workers.worker_count = worker_count;
self
}
fn check_view_integrity_on_open(mut self, check: bool) -> Self {
self.storage.views.check_integrity_on_open = check;
self
}
fn key_value_persistence(mut self, persistence: KeyValuePersistence) -> Self {
self.storage.key_value_persistence = persistence;
self
}
}