autoconnect/
main.rs

1#![warn(rust_2018_idioms)]
2
3#[global_allocator]
4static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
5
6#[macro_use]
7extern crate slog_scope;
8
9use std::{env, time::Duration, vec::Vec};
10
11use actix_http::HttpService;
12use actix_server::Server;
13use actix_service::map_config;
14use actix_web::dev::AppConfig;
15use docopt::Docopt;
16use serde::Deserialize;
17
18use autoconnect_settings::{AppState, Settings};
19use autoconnect_web::{build_app, config, config_router};
20use autopush_common::{
21    db::spawn_pool_periodic_reporter,
22    errors::{ApcErrorKind, Result},
23    logging,
24};
25
26const USAGE: &str = "
27Usage: autoconnect [options]
28
29Options:
30    -h, --help                          Show this message.
31    -c, --config=CONFIGFILE             Connection configuration file path.
32";
33
34#[derive(Debug, Deserialize)]
35struct Args {
36    flag_config: Option<String>,
37}
38
39#[actix_web::main]
40async fn main() -> Result<()> {
41    env_logger::init();
42    let args: Args = Docopt::new(USAGE)
43        .and_then(|d| d.deserialize())
44        .unwrap_or_else(|e| e.exit());
45    let mut filenames = Vec::new();
46    if let Some(config_filename) = args.flag_config {
47        filenames.push(config_filename);
48    }
49    let settings =
50        Settings::with_env_and_config_files(&filenames).map_err(ApcErrorKind::ConfigError)?;
51    logging::init_logging(
52        !settings.human_logs,
53        env!("CARGO_PKG_NAME"),
54        env!("CARGO_PKG_VERSION"),
55    )
56    .expect("Logging failed to initialize");
57    debug!("Starting up autoconnect...");
58
59    // Sentry requires the environment variable "SENTRY_DSN".
60    if env::var("SENTRY_DSN")
61        .unwrap_or_else(|_| "".to_owned())
62        .is_empty()
63    {
64        print!("SENTRY_DSN not set. Logging disabled.");
65    }
66
67    let _guard = sentry::init(sentry::ClientOptions {
68        release: sentry::release_name!(),
69        session_mode: sentry::SessionMode::Request, // new session per request
70        auto_session_tracking: true,
71        ..autopush_common::sentry::client_options()
72    });
73
74    let port = settings.port;
75    let router_port = settings.router_port;
76    let actix_max_connections = settings.actix_max_connections;
77    let actix_workers = settings.actix_workers;
78    let app_state = AppState::from_settings(settings)?;
79    app_state.init_and_spawn_megaphone_updater().await?;
80    spawn_pool_periodic_reporter(
81        Duration::from_secs(10),
82        app_state.db.clone(),
83        app_state.metrics.clone(),
84    );
85
86    info!(
87        "Starting autoconnect on port: {} router_port: {} ({})",
88        port,
89        router_port,
90        logging::parallelism_banner()
91    );
92
93    let router_app_state = app_state.clone();
94    let mut builder = Server::build()
95        .bind("autoconnect", ("0.0.0.0", port), move || {
96            let app = build_app!(app_state, config);
97            HttpService::build()
98                // XXX: AppConfig::default() does *not* have correct values
99                // https://github.com/actix/actix-web/issues/3180
100                .finish(map_config(app, |_| AppConfig::default()))
101                .tcp()
102        })?
103        .bind("autoconnect-router", ("0.0.0.0", router_port), move || {
104            let app = build_app!(router_app_state, config_router);
105            HttpService::build()
106                // XXX:
107                .finish(map_config(app, |_| AppConfig::default()))
108                .tcp()
109        })?;
110    if let Some(max_connections) = actix_max_connections {
111        builder = builder.max_concurrent_connections(max_connections);
112    }
113    if let Some(workers) = actix_workers {
114        builder = builder.workers(workers);
115    }
116    builder.run().await?;
117
118    info!("Shutting down autoconnect");
119    Ok(())
120}