Skip to main content

autoendpoint/
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
9mod auth;
10mod error;
11mod extractors;
12mod headers;
13mod metrics;
14mod routers;
15mod routes;
16mod server;
17mod settings;
18
19use docopt::Docopt;
20use serde::Deserialize;
21use std::error::Error;
22
23use autopush_common::logging;
24
25const USAGE: &str = "
26Usage: autoendpoint [options]
27
28Options:
29    -h, --help                  Show this message
30    -c, --config=CONFIGFILE     AutoEndpoint configuration file path.
31";
32
33#[derive(Debug, Deserialize)]
34struct Args {
35    flag_config: Option<String>,
36}
37
38#[actix_rt::main]
39async fn main() -> Result<(), Box<dyn Error>> {
40    // Must run before any TLS use: reqwest (outbound HTTPS to push services)
41    // builds its rustls config via the default provider and panics if none is
42    // installed when multiple providers are compiled in; tonic/bigtable adopts
43    // whatever we install here.
44    autopush_common::tls::install_crypto_provider();
45    let args: Args = Docopt::new(USAGE)
46        .and_then(|d| d.deserialize())
47        .unwrap_or_else(|e| e.exit());
48    let settings = settings::Settings::with_env_and_config_file(&args.flag_config)?;
49    let host_port = format!("{}:{}", &settings.host, &settings.port);
50    logging::init_logging(
51        !settings.human_logs,
52        env!("CARGO_PKG_NAME"),
53        env!("CARGO_PKG_VERSION"),
54    )
55    .expect("Logging failed to initialize");
56    debug!("Starting up autoendpoint...");
57
58    let _sentry = sentry::init(sentry::ClientOptions {
59        release: sentry::release_name!(),
60        attach_stacktrace: true,
61        ..autopush_common::sentry::client_options()
62    });
63
64    // Run server...
65    let server = server::Server::with_settings(settings)
66        .await
67        .inspect_err(|e| error!("Server Start Error: {:?}", e))
68        .expect("Could not start server");
69    info!(
70        "Starting autoendpoint on port: {} ({})",
71        host_port,
72        logging::parallelism_banner()
73    );
74    server.await?;
75
76    // Shutdown
77    info!("Shutting down autoendpoint");
78    logging::reset_logging();
79    Ok(())
80}