cli_core/startup.rs
1use exitfailure::ExitFailure;
2use server::config::set_config;
3use std::env;
4use tokio::runtime::Handle;
5
6/// Boot the server the same way the original CLI entrypoint did:
7/// install drivers, run migrations, load config. Used by both
8/// binaries so the initialization sequence stays identical.
9///
10/// # Errors
11///
12/// Returns an `ExitFailure` if `set_config` fails for the optional
13/// `setopt` override. Server-boot failures are handled separately:
14/// panics from the `boot` join handle surface as task-panic aborts,
15/// which is the original behaviour the CLI shipped with.
16///
17/// # Panics
18///
19/// Panics if `server::start().await` returns a handle that panics,
20/// or whose inner task panics, or whose inner `Result` is `Err`.
21/// The three `.unwrap()` calls unwind a `JoinHandle → Result →
22/// Result` triple set up by the legacy `server::start` API; surface
23/// area matches the previous CLI bootstrap one-for-one.
24pub async fn start_server(
25 database: Option<String>,
26 setopt: Option<(String, String)>,
27) -> Result<(), ExitFailure> {
28 if let Some(db) = database {
29 unsafe {
30 env::set_var("DATABASE_URL", db);
31 }
32 }
33
34 Handle::current()
35 .spawn(server::start().await)
36 .await
37 .unwrap()
38 .unwrap()
39 .unwrap();
40
41 if let Some((field, content)) = setopt {
42 set_config(&field, content.into()).await?;
43 }
44
45 Ok(())
46}