chiark / gitweb /
config read in global
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 23 Aug 2020 20:57:41 +0000 (21:57 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 23 Aug 2020 20:57:41 +0000 (21:57 +0100)
src/bin/server.rs
src/global.rs

index 2a7dd26cb507874bafb4a0ff1fe6fd3c23f6a1e3..406f79c2332da78f3dd43a28ff2b81655a46dc76 100644 (file)
@@ -66,19 +66,10 @@ fn resource(leaf : CheckedResourceLeaf) -> io::Result<NamedFile> {
   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()?;
 
index 0232c9a83d745eea585bf3d3c6596b681de4b6b8..446ea7860cbbe0b41ee43b44f1e270ebb3485991 100644 (file)
@@ -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<ServerConfig> {
   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("")