From bb47799a1e825cb323def47bdad4236b147dd9ca Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 23 Aug 2020 21:57:41 +0100 Subject: [PATCH] config read in global --- src/bin/server.rs | 13 ++----------- src/global.rs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index 2a7dd26c..406f79c2 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -66,19 +66,10 @@ fn resource(leaf : CheckedResourceLeaf) -> io::Result { NamedFile::open(format!("{}/{}", template_dir, leaf.safe)) } -const DEFAULT_CONFIG_FILENAME : &str = "server.toml"; - #[throws(StartupError)] fn main() { - { - let config_filename = env::args().nth(1) - .unwrap_or(DEFAULT_CONFIG_FILENAME.to_owned()); - let mut buf = String::new(); - File::open(config_filename)?.read_to_string(&mut buf)?; - let config : ServerConfigSpec = toml::de::from_str(&buf)?; - let config = config.try_into()?; - set_config(config); - }; + let config_filename = env::args().nth(1); + ServerConfig::read(config_filename.as_ref().map(String::as_str))?; load_games()?; diff --git a/src/global.rs b/src/global.rs index 0232c9a8..446ea786 100644 --- a/src/global.rs +++ b/src/global.rs @@ -921,6 +921,8 @@ pub fn client_expire_old_clients() { // ========== server config ========== +const DEFAULT_CONFIG_FILENAME : &str = "server.toml"; + const DEFAULT_SAVE_DIRECTORY : &str = "save"; pub const DEFAULT_COMMAND_SOCKET : &str = "command.socket"; // in save dir // ^ xxx should not be pub @@ -957,10 +959,23 @@ pub fn config() -> Arc { GLOBAL.config.read().unwrap().clone() } -pub fn set_config(config: ServerConfig) { +fn set_config(config: ServerConfig) { *GLOBAL.config.write().unwrap() = Arc::new(config) } +impl ServerConfig { + #[throws(StartupError)] + pub fn read(config_filename: Option<&str>) { + let config_filename = config_filename + .unwrap_or_else(|| DEFAULT_CONFIG_FILENAME); + let mut buf = String::new(); + File::open(config_filename)?.read_to_string(&mut buf)?; + let config : ServerConfigSpec = toml::de::from_str(&buf)?; + let config = config.try_into()?; + set_config(config); + } +} + impl Default for ServerConfig { fn default() -> ServerConfig { let spec : ServerConfigSpec = toml::de::from_str("") -- 2.30.2