summaryrefslogtreecommitdiff
path: root/src/server.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-02-16 07:21:38 +0000
committerLars Wirzenius <liw@liw.fi>2021-02-16 07:21:38 +0000
commitf9f047dc18763bd37d2cf6cbd47769f7e91f976b (patch)
tree048af34bd737968f39a58eaa880b9d5fcf5768f6 /src/server.rs
parentbb1e21495292e2d64446344581e02f4ebb9d3668 (diff)
parentfe680c3dbef579d41b2e32dc5b33dc01c3c23edd (diff)
downloadobnam2-f9f047dc18763bd37d2cf6cbd47769f7e91f976b.tar.gz
Merge branch 'server-config' into 'main'
refactor: move server config into src/server.rs See merge request larswirzenius/obnam!96
Diffstat (limited to 'src/server.rs')
-rw-r--r--src/server.rs46
1 files changed, 46 insertions, 0 deletions
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<Config> {
+ 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)]