apollo_framework/
lib.rs

1//! The Apollo framework is a toolkit for building stable and robust server software.
2//!
3//! **Apollo** was extracted out of [Jupiter](https://github.com/scireum/jupiter) to make its
4//! core parse usable by other libraries or applications.
5//!
6//! Apollo provides a small **dependency injection helper** called *Platform** along with some
7//! tooling to setup logging, format messages and to react on signals. This is commonly required
8//! to properly run inside a Docker container.
9#![deny(
10    warnings,
11    missing_docs,
12    trivial_casts,
13    trivial_numeric_casts,
14    unused_extern_crates,
15    unused_import_braces,
16    unused_results
17)]
18
19use simplelog::{ConfigBuilder, LevelFilter, SimpleLogger};
20use std::sync::Once;
21
22pub mod average;
23pub mod config;
24pub mod fmt;
25pub mod platform;
26pub mod server;
27pub mod signals;
28
29/// Contains the version of the Apollo framework.
30pub const APOLLO_VERSION: &str = "1.0.3";
31
32/// Contains the git commit hash of the Apollo build being used.
33pub const APOLLO_REVISION: &str = "2254d3eb4cf2a97287c07b0655ad504a58e20fbc";
34
35/// Initializes the logging system.
36///
37/// This uses the simple logger crate with a logging format which is compatible with docker
38/// environments and quite well readable by common tools.
39pub fn init_logging() {
40    static INIT_LOGGING: Once = Once::new();
41    static DATE_FORMAT: &str = "[%Y-%m-%dT%H:%M:%S%.3f]";
42
43    // We need to do this as otherwise the integration tests might crash as the logging system
44    // is initialized several times...
45    INIT_LOGGING.call_once(|| {
46        if let Err(error) = SimpleLogger::init(
47            LevelFilter::Debug,
48            ConfigBuilder::new()
49                .set_time_format_str(DATE_FORMAT)
50                .set_thread_level(LevelFilter::Trace)
51                .set_target_level(LevelFilter::Error)
52                .set_location_level(LevelFilter::Trace)
53                .build(),
54        ) {
55            panic!("Failed to initialize logging system: {}", error);
56        }
57    });
58}
59
60#[cfg(test)]
61mod testing {
62    use std::sync::Mutex;
63
64    lazy_static::lazy_static! {
65        /// Provides a global lock which has to be acquired if a test operates on shared
66        /// resources. This would either be our test port (1503) on which we start our
67        /// local server for integrations tests or the repository which operates on the
68        /// file system. Using this lock, we can still execute all other tests in parallel
69        /// and only block if required.
70        pub static ref SHARED_TEST_RESOURCES: Mutex<()> = Mutex::new(());
71    }
72
73    /// Executes async code within a single threaded tokio runtime.
74    pub fn test_async<F: std::future::Future>(future: F) {
75        use tokio::runtime;
76
77        let rt = runtime::Builder::new_current_thread()
78            .enable_all()
79            .build()
80            .unwrap();
81
82        let _ = rt.block_on(future);
83    }
84}