Read config from disk
This commit is contained in:
parent
7f39d8e8b7
commit
a4e9aea1e4
4 changed files with 21 additions and 24 deletions
|
@ -85,8 +85,8 @@ pub enum Error {
|
||||||
#[error("DDNS update URL is invalid")]
|
#[error("DDNS update URL is invalid")]
|
||||||
DDNSUpdateURLInvalid,
|
DDNSUpdateURLInvalid,
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error("Could not deserialize configuration: `{0}`")]
|
||||||
Toml(#[from] toml::de::Error),
|
ConfigDeserialization(toml::de::Error),
|
||||||
#[error("Could not deserialize collection")]
|
#[error("Could not deserialize collection")]
|
||||||
IndexDeserializationError,
|
IndexDeserializationError,
|
||||||
#[error("Could not serialize collection")]
|
#[error("Could not serialize collection")]
|
||||||
|
|
|
@ -88,13 +88,28 @@ pub struct Manager {
|
||||||
|
|
||||||
impl Manager {
|
impl Manager {
|
||||||
pub async fn new(config_file_path: &Path, auth_secret: auth::Secret) -> Result<Self, Error> {
|
pub async fn new(config_file_path: &Path, auth_secret: auth::Secret) -> Result<Self, Error> {
|
||||||
let config = storage::Config::default(); // TODO read from disk!!
|
let config = {
|
||||||
let config: Config = config.try_into()?;
|
if tokio::fs::try_exists(config_file_path)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Error::Io(config_file_path.to_owned(), e))?
|
||||||
|
{
|
||||||
|
let config_content = tokio::fs::read_to_string(config_file_path)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Error::Io(config_file_path.to_owned(), e))?;
|
||||||
|
let config = toml::de::from_str::<storage::Config>(&config_content)
|
||||||
|
.map_err(Error::ConfigDeserialization)?;
|
||||||
|
config.try_into()?
|
||||||
|
} else {
|
||||||
|
Config::default()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let manager = Self {
|
let manager = Self {
|
||||||
config_file_path: config_file_path.to_owned(),
|
config_file_path: config_file_path.to_owned(),
|
||||||
config: Arc::new(RwLock::new(config)),
|
config: Arc::new(RwLock::new(config)),
|
||||||
auth_secret,
|
auth_secret,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(manager)
|
Ok(manager)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,7 @@
|
||||||
use std::{
|
use std::path::PathBuf;
|
||||||
io::Read,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::app::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -37,16 +32,3 @@ pub struct Config {
|
||||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
pub users: Vec<User>,
|
pub users: Vec<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn from_path(path: &Path) -> Result<Self, Error> {
|
|
||||||
let mut config_file =
|
|
||||||
std::fs::File::open(path).map_err(|e| Error::Io(path.to_owned(), e))?;
|
|
||||||
let mut config_file_content = String::new();
|
|
||||||
config_file
|
|
||||||
.read_to_string(&mut config_file_content)
|
|
||||||
.map_err(|e| Error::Io(path.to_owned(), e))?;
|
|
||||||
let config = toml::de::from_str::<Self>(&config_file_content)?;
|
|
||||||
Ok(config)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ impl From<app::Error> for APIError {
|
||||||
app::Error::DDNSUpdateURLInvalid => APIError::InvalidDDNSURL,
|
app::Error::DDNSUpdateURLInvalid => APIError::InvalidDDNSURL,
|
||||||
app::Error::IndexAlbumArtPatternInvalid => APIError::InvalidAlbumArtPattern,
|
app::Error::IndexAlbumArtPatternInvalid => APIError::InvalidAlbumArtPattern,
|
||||||
|
|
||||||
app::Error::Toml(_) => APIError::Internal,
|
app::Error::ConfigDeserialization(_) => APIError::Internal,
|
||||||
app::Error::IndexDeserializationError => APIError::Internal,
|
app::Error::IndexDeserializationError => APIError::Internal,
|
||||||
app::Error::IndexSerializationError => APIError::Internal,
|
app::Error::IndexSerializationError => APIError::Internal,
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue