-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.rs
296 lines (270 loc) · 7.69 KB
/
server.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::{default_path, Cli, API_URL};
use std::path::PathBuf;
use tangram_client as tg;
use url::Url;
/// Manage the server.
#[derive(Debug, clap::Args)]
pub struct Args {
#[clap(subcommand)]
pub command: Command,
}
#[derive(Debug, clap::Subcommand)]
pub enum Command {
Health(HealthArgs),
Run(RunArgs),
Start(StartArgs),
Stop(StopArgs),
}
/// Get the server's health.
#[derive(Debug, clap::Args)]
pub struct HealthArgs {}
/// Run a server.
#[derive(Debug, clap::Args)]
pub struct RunArgs {
/// The url to listen on.
#[clap(long)]
pub url: Option<Url>,
/// The path where the server should store its data. The default is `$HOME/.tangram`.
#[clap(long)]
pub path: Option<PathBuf>,
}
/// Start the server.
#[derive(Debug, clap::Args)]
pub struct StartArgs {}
/// Stop the server.
#[derive(Debug, clap::Args)]
pub struct StopArgs {}
impl Cli {
pub async fn command_server(&self, args: Args) -> tg::Result<()> {
match args.command {
Command::Health(_) => {
let client = self.client().await?;
let health = client.health().await?;
let health = serde_json::to_string_pretty(&health).unwrap();
println!("{health}");
},
Command::Start(_) => {
self.start_server().await?;
},
Command::Stop(_) => {
let client = self.client().await?;
client.stop().await?;
},
Command::Run(args) => {
self.command_server_run(args).await?;
},
}
Ok(())
}
async fn command_server_run(&self, args: RunArgs) -> tg::Result<()> {
// Get the config.
let config = self.config.as_ref();
// Get the path.
let path = args
.path
.or(config.and_then(|config| config.path.clone()))
.unwrap_or(default_path());
// Get the url.
let url = args
.url
.or(config.and_then(|config| config.url.clone()))
.unwrap_or_else(|| {
format!("unix:{}", path.join("socket").display())
.parse()
.unwrap()
});
// Get the file descriptor semaphore size.
let file_descriptor_semaphore_size = config
.and_then(|config| config.advanced.as_ref())
.and_then(|advanced| advanced.file_descriptor_semaphore_size)
.unwrap_or(1024);
// Create the build options.
let enable = config
.and_then(|config| config.build.as_ref())
.and_then(|build| build.enable)
.unwrap_or(true);
let max_concurrency = config
.and_then(|config| config.build.as_ref())
.and_then(|build| build.max_concurrency)
.unwrap_or_else(|| std::thread::available_parallelism().unwrap().get());
let build = tangram_server::options::Build {
enable,
max_concurrency,
};
// Create the database options.
let database = config
.and_then(|config| config.database.as_ref())
.map_or_else(
|| {
tangram_server::options::Database::Sqlite(
tangram_server::options::SqliteDatabase {
max_connections: std::thread::available_parallelism().unwrap().get(),
},
)
},
|database| match database {
crate::config::Database::Sqlite(sqlite) => {
let max_connections = sqlite
.max_connections
.unwrap_or_else(|| std::thread::available_parallelism().unwrap().get());
tangram_server::options::Database::Sqlite(
tangram_server::options::SqliteDatabase { max_connections },
)
},
crate::config::Database::Postgres(postgres) => {
let url = postgres.url.clone();
let max_connections = postgres
.max_connections
.unwrap_or_else(|| std::thread::available_parallelism().unwrap().get());
tangram_server::options::Database::Postgres(
tangram_server::options::PostgresDatabase {
url,
max_connections,
},
)
},
},
);
// Create the file system monitor options
let file_system_monitor = config
.as_ref()
.and_then(|config| config.file_system_monitor.as_ref())
.map_or(
tangram_server::options::FileSystemMonitor { enable: true },
|file_system_monitor| tangram_server::options::FileSystemMonitor {
enable: file_system_monitor.enable,
},
);
// Create the messenger options.
let messenger = config
.and_then(|config| config.messenger.as_ref())
.map_or_else(
|| tangram_server::options::Messenger::Channel,
|messenger| match messenger {
crate::config::Messenger::Channel => {
tangram_server::options::Messenger::Channel
},
crate::config::Messenger::Nats(nats) => {
let url = nats.url.clone();
tangram_server::options::Messenger::Nats(
tangram_server::options::NatsMessenger { url },
)
},
},
);
// Create the oauth options.
let oauth = config
.and_then(|config| config.oauth.as_ref())
.map(|oauth| {
let github =
oauth
.github
.as_ref()
.map(|client| tangram_server::options::OauthClient {
auth_url: client.auth_url.clone(),
client_id: client.client_id.clone(),
client_secret: client.client_secret.clone(),
redirect_url: client.redirect_url.clone(),
token_url: client.token_url.clone(),
});
tangram_server::options::Oauth { github }
})
.unwrap_or_default();
// Create the remote options.
let remotes = config
.and_then(|config| config.remotes.as_ref())
.map(|remotes| {
remotes
.iter()
.map(|remote| {
let url = remote.url.clone();
let client = tg::Builder::new(url).build();
let build_ = remote.build.clone();
let enable = build_
.as_ref()
.and_then(|build| build.enable)
.unwrap_or(false);
let build_ = tangram_server::options::RemoteBuild { enable };
let remote = tangram_server::options::Remote {
client,
build: build_,
};
Ok::<_, tg::Error>(remote)
})
.collect()
})
.transpose()?;
let remotes = if let Some(remotes) = remotes {
remotes
} else {
let build = tangram_server::options::RemoteBuild { enable: false };
let url = Url::parse(API_URL).unwrap();
let client = tg::Builder::new(url).build();
let remote = tangram_server::options::Remote { build, client };
vec![remote]
};
// Get the version.
let version = self.version.clone();
// Create the vfs options.
let vfs = config.and_then(|config| config.vfs.as_ref()).map_or_else(
|| tangram_server::options::Vfs { enable: true },
|vfs| tangram_server::options::Vfs {
enable: vfs.enable.unwrap_or(true),
},
);
// Get the server's advanced options.
let preserve_temp_directories = config
.as_ref()
.and_then(|config| config.advanced.as_ref())
.and_then(|advanced| advanced.preserve_temp_directories)
.unwrap_or(false);
let error_trace_options = config
.and_then(|config| config.advanced.as_ref())
.and_then(|advanced| advanced.error_trace_options.clone())
.unwrap_or_default();
let write_build_logs_to_stderr = config
.and_then(|config| config.advanced.as_ref())
.and_then(|advanced| advanced.write_build_logs_to_stderr)
.unwrap_or(false);
let advanced = tangram_server::options::Advanced {
error_trace_options,
file_descriptor_semaphore_size,
preserve_temp_directories,
write_build_logs_to_stderr,
};
// Create the options.
let options = tangram_server::Options {
advanced,
build,
database,
file_system_monitor,
messenger,
oauth,
path,
remotes,
url,
version,
vfs,
};
// Start the server.
let server = tangram_server::Server::start(options)
.await
.map_err(|source| tg::error!(!source, "failed to start the server"))?;
// Stop the server if an an interrupt signal is received.
tokio::spawn({
let server = server.clone();
async move {
tokio::signal::ctrl_c().await.ok();
server.stop();
tokio::signal::ctrl_c().await.ok();
std::process::exit(130);
}
});
// Join the server.
server
.join()
.await
.map_err(|source| tg::error!(!source, "failed to join the server"))?;
Ok(())
}
}