about summary refs log tree commit diff
path: root/src/main.rs
blob: 39c9c1db4080719639203e9758c70621ce0ca3b2 (plain)
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
use genenetwork4::config::DbMode;

#[tokio::main]
async fn main() {
    let config = genenetwork4::config::Config::load();
    // Startup database probe (`SELECT 1` through the shared pool),
    // BEFORE the listener is bound. `production` treats an unreachable
    // database as a misconfiguration and refuses to start; `dev` starts
    // anyway, loudly, with DB-backed routes answering the typed 503
    // (src/error.rs). The message names both config keys so the refusal
    // is unambiguous.
    if let Err(error) = genenetwork4::db::probe() {
        match config.db_mode {
            DbMode::Production => {
                eprintln!(
                    "FATAL: GN4_DB_MODE=production requires a reachable database, but SQL_URI \
                     is unreachable or unconfigured ({error}); refusing to start."
                );
                std::process::exit(1);
            }
            DbMode::Dev => {
                eprintln!(
                    "database not configured/unreachable — DB-backed routes disabled \
                     (GN4_DB_MODE=dev, SQL_URI unreachable or unconfigured): {error}"
                );
            }
        }
    }
    let bind_ip: std::net::IpAddr = config
        .bind
        .parse()
        .expect("GENENETWORK4_BIND must be an IP address");
    let addr = std::net::SocketAddr::new(bind_ip, config.port);
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    // The connect info (the peer's address) is what POST /get-test-token
    // checks to answer only local callers (src/routes/dev.rs).
    axum::serve(
        listener,
        genenetwork4::app().into_make_service_with_connect_info::<std::net::SocketAddr>(),
    )
    .await
    .unwrap();
}