1
use exitfailure::ExitFailure;
2
use server::config::set_system_config;
3
use std::env;
4

            
5
/// Boot the server the same way the original CLI entrypoint did:
6
/// install drivers, run migrations, load config. Used by both
7
/// binaries so the initialization sequence stays identical.
8
///
9
/// # Errors
10
///
11
/// Returns an `ExitFailure` if `set_system_config` fails for the optional
12
/// `setopt` override. Server-boot failures are handled separately:
13
/// panics from the `boot` join handle surface as task-panic aborts,
14
/// which is the original behaviour the CLI shipped with.
15
///
16
/// # Panics
17
///
18
/// Panics if `server::start().await` returns a handle that panics,
19
/// or whose inner task panics, or whose inner `Result` is `Err`.
20
pub async fn start_server(
21
    database: Option<String>,
22
    setopt: Option<(String, String)>,
23
) -> Result<(), ExitFailure> {
24
    if let Some(db) = database {
25
        unsafe {
26
            env::set_var("DATABASE_URL", db);
27
        }
28
    }
29

            
30
    // `server::start` already returns the boot task's handle; spawning it again
31
    // only added a third Result layer to unwrap.
32
    server::start().await.await??;
33

            
34
    if let Some((field, content)) = setopt {
35
        set_system_config(&field, content.into()).await?;
36
    }
37

            
38
    Ok(())
39
}