summaryrefslogtreecommitdiff
path: root/src/server.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.rs')
-rw-r--r--src/server.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/server.rs b/src/server.rs
index 26f67bd..31a03fc 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -1,3 +1,5 @@
+//! Stuff related to the Obnam chunk server.
+
use crate::chunk::DataChunk;
use crate::chunkid::ChunkId;
use crate::chunkmeta::ChunkMeta;
@@ -6,37 +8,50 @@ use std::collections::HashMap;
use std::default::Default;
use std::path::{Path, PathBuf};
+/// Server configuration.
#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct ServerConfig {
+ /// Path to directory where chunks are stored.
pub chunks: PathBuf,
+ /// Address where server is to listen.
pub address: String,
+ /// Path to TLS key.
pub tls_key: PathBuf,
+ /// Path to TLS certificate.
pub tls_cert: PathBuf,
}
+/// Possible errors wittht server configuration.
#[derive(Debug, thiserror::Error)]
pub enum ServerConfigError {
+ /// The chunks directory doesn't exist.
#[error("Directory for chunks {0} does not exist")]
ChunksDirNotFound(PathBuf),
+ /// The TLS certificate doesn't exist.
#[error("TLS certificate {0} does not exist")]
TlsCertNotFound(PathBuf),
+ /// The TLS key doesn't exist.
#[error("TLS key {0} does not exist")]
TlsKeyNotFound(PathBuf),
+ /// Server address is wrong.
#[error("server address can't be resolved")]
BadServerAddress,
+ /// Failed to read configuration file.
#[error("failed to read configuration file {0}: {1}")]
Read(PathBuf, std::io::Error),
+ /// Failed to parse configuration file as YAML.
#[error("failed to parse configuration file as YAML: {0}")]
YamlParse(serde_yaml::Error),
}
impl ServerConfig {
+ /// Read, parse, and check the server configuration file.
pub fn read_config(filename: &Path) -> Result<Self, ServerConfigError> {
let config = match std::fs::read_to_string(filename) {
Ok(config) => config,
@@ -47,6 +62,7 @@ impl ServerConfig {
Ok(config)
}
+ /// Check the configuration.
pub fn check(&self) -> Result<(), ServerConfigError> {
if !self.chunks.exists() {
return Err(ServerConfigError::ChunksDirNotFound(self.chunks.clone()));
@@ -68,17 +84,18 @@ pub struct Created {
}
impl Created {
+ /// Create a new created chunk id.
pub fn new(id: ChunkId) -> Self {
Created { id }
}
+ /// Convert to JSON.
pub fn to_json(&self) -> String {
serde_json::to_string(&self).unwrap()
}
}
/// Result of retrieving a chunk.
-
#[derive(Debug, Serialize)]
pub struct Fetched {
id: ChunkId,
@@ -86,10 +103,12 @@ pub struct Fetched {
}
impl Fetched {
+ /// Create a new id for a fetched chunk.
pub fn new(id: ChunkId, chunk: DataChunk) -> Self {
Fetched { id, chunk }
}
+ /// Convert to JSON.
pub fn to_json(&self) -> String {
serde_json::to_string(&self).unwrap()
}
@@ -102,15 +121,18 @@ pub struct SearchHits {
}
impl SearchHits {
+ /// Insert a new chunk id to search results.
pub fn insert(&mut self, id: ChunkId, meta: ChunkMeta) {
self.map.insert(id.to_string(), meta);
}
+ /// Convert from JSON.
pub fn from_json(s: &str) -> Result<Self, serde_json::Error> {
let map = serde_json::from_str(s)?;
Ok(SearchHits { map })
}
+ /// Convert to JSON.
pub fn to_json(&self) -> String {
serde_json::to_string(&self.map).unwrap()
}