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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#[macro_use] extern crate log; extern crate config; extern crate flexi_logger; extern crate getopts; extern crate hyper; extern crate hyper_router; mod configuration; mod logging; mod monitoring; use getopts::Options; use std::env; pub use configuration::{ConfigProvider, Configuration, ConfigurationPreferences}; pub use logging::LoggingConfig; pub use monitoring::MonitoringConfig; pub struct ApplicationBuilder { config_prefs: ConfigurationPreferences, logging_config: LoggingConfig, monitoring_config: MonitoringConfig, } impl ApplicationBuilder { pub fn default() -> ApplicationBuilder { ApplicationBuilder { config_prefs: ConfigurationPreferences::default(), logging_config: LoggingConfig::default(), monitoring_config: MonitoringConfig::default(), } } pub fn with_config_preferences( mut self, config_prefs: ConfigurationPreferences, ) -> ApplicationBuilder { self.config_prefs = config_prefs; self } pub fn with_logging_config(mut self, logging_config: LoggingConfig) -> ApplicationBuilder { self.logging_config = logging_config; self } pub fn with_monitoring_config( mut self, monitoring_config: MonitoringConfig, ) -> ApplicationBuilder { self.monitoring_config = monitoring_config; self } pub fn build(self) -> Application { Application::start(&self) } } pub trait LifeCycle { fn shutdown(&self); } pub struct Application { pub configuration: configuration::Configuration, } impl Application { fn start(app_builder: &ApplicationBuilder) -> Application { let config = Configuration::read_configuration(&app_builder.config_prefs); logging::start_logging(&app_builder.logging_config); monitoring::start_beacon(&app_builder.monitoring_config); Application { configuration: config, } } pub fn start_from_cli() -> Application { let args: Vec<String> = env::args().collect(); let mut options = Options::new(); options.optopt( "c", "config", "fully qualified path to the configuration file", "CONFIG", ); options.optopt( "s", "secrets", "fully qualified path to the file that contains secrets", "SECRETS", ); let matches = options .parse(&args[1..]) .expect("Unable to read command line"); let config_prefs = ConfigurationPreferences { config_file: matches.opt_str("config"), secrets_file: matches.opt_str("secrets"), prefix: None, separator: None, }; let config = Configuration::read_configuration(&config_prefs); let log_config: LoggingConfig = match config.get_value("logging") { Ok(v) => v, Err(_) => LoggingConfig::default(), }; let montior_config: MonitoringConfig = match config.get_value("monitoring") { Ok(v) => v, Err(_) => MonitoringConfig::default(), }; logging::start_logging(&log_config); monitoring::start_beacon(&montior_config); Application { configuration: config, } } }