From fe680c3dbef579d41b2e32dc5b33dc01c3c23edd Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 16 Feb 2021 09:20:28 +0200 Subject: refactor: move server config into src/server.rs --- src/bin/obnam-server.rs | 50 +++---------------------------------------------- src/server.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs index 0e9d4e6..d4513fa 100644 --- a/src/bin/obnam-server.rs +++ b/src/bin/obnam-server.rs @@ -4,11 +4,12 @@ use obnam::chunk::DataChunk; use obnam::chunkid::ChunkId; use obnam::chunkmeta::ChunkMeta; use obnam::indexedstore::IndexedStore; -use serde::{Deserialize, Serialize}; +use obnam::server::{Config, ConfigError}; +use serde::Serialize; use std::collections::HashMap; use std::default::Default; use std::net::{SocketAddr, ToSocketAddrs}; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::sync::Arc; use structopt::StructOpt; use tokio::sync::Mutex; @@ -82,51 +83,6 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -#[derive(Debug, Deserialize, Clone)] -pub struct Config { - pub chunks: PathBuf, - pub address: String, - pub tls_key: PathBuf, - pub tls_cert: PathBuf, -} - -#[derive(Debug, thiserror::Error)] -enum ConfigError { - #[error("Directory for chunks {0} does not exist")] - ChunksDirNotFound(PathBuf), - - #[error("TLS certificate {0} does not exist")] - TlsCertNotFound(PathBuf), - - #[error("TLS key {0} does not exist")] - TlsKeyNotFound(PathBuf), - - #[error("server address can't be resolved")] - BadServerAddress, -} - -impl Config { - pub fn read_config(filename: &Path) -> anyhow::Result { - let config = std::fs::read_to_string(filename)?; - let config: Config = serde_yaml::from_str(&config)?; - config.check()?; - Ok(config) - } - - pub fn check(&self) -> anyhow::Result<()> { - if !self.chunks.exists() { - return Err(ConfigError::ChunksDirNotFound(self.chunks.clone()).into()); - } - if !self.tls_cert.exists() { - return Err(ConfigError::TlsCertNotFound(self.tls_cert.clone()).into()); - } - if !self.tls_key.exists() { - return Err(ConfigError::TlsKeyNotFound(self.tls_key.clone()).into()); - } - Ok(()) - } -} - pub async fn create_chunk( store: Arc>, meta: String, diff --git a/src/server.rs b/src/server.rs index 1b2dc29..01a6958 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,6 +4,52 @@ use crate::chunkmeta::ChunkMeta; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::default::Default; +use std::path::{Path, PathBuf}; + +#[derive(Debug, Deserialize, Clone)] +pub struct Config { + pub chunks: PathBuf, + pub address: String, + pub tls_key: PathBuf, + pub tls_cert: PathBuf, +} + +#[derive(Debug, thiserror::Error)] +pub enum ConfigError { + #[error("Directory for chunks {0} does not exist")] + ChunksDirNotFound(PathBuf), + + #[error("TLS certificate {0} does not exist")] + TlsCertNotFound(PathBuf), + + #[error("TLS key {0} does not exist")] + TlsKeyNotFound(PathBuf), + + #[error("server address can't be resolved")] + BadServerAddress, +} + +impl Config { + pub fn read_config(filename: &Path) -> anyhow::Result { + let config = std::fs::read_to_string(filename)?; + let config: Config = serde_yaml::from_str(&config)?; + config.check()?; + Ok(config) + } + + pub fn check(&self) -> anyhow::Result<()> { + if !self.chunks.exists() { + return Err(ConfigError::ChunksDirNotFound(self.chunks.clone()).into()); + } + if !self.tls_cert.exists() { + return Err(ConfigError::TlsCertNotFound(self.tls_cert.clone()).into()); + } + if !self.tls_key.exists() { + return Err(ConfigError::TlsKeyNotFound(self.tls_key.clone()).into()); + } + Ok(()) + } +} /// Result of creating a chunk. #[derive(Debug, Serialize)] -- cgit v1.2.1