Read config from disk
This commit is contained in:
parent
7f39d8e8b7
commit
a4e9aea1e4
4 changed files with 21 additions and 24 deletions
src
|
@ -85,8 +85,8 @@ pub enum Error {
|
|||
#[error("DDNS update URL is invalid")]
|
||||
DDNSUpdateURLInvalid,
|
||||
|
||||
#[error(transparent)]
|
||||
Toml(#[from] toml::de::Error),
|
||||
#[error("Could not deserialize configuration: `{0}`")]
|
||||
ConfigDeserialization(toml::de::Error),
|
||||
#[error("Could not deserialize collection")]
|
||||
IndexDeserializationError,
|
||||
#[error("Could not serialize collection")]
|
||||
|
|
|
@ -88,13 +88,28 @@ pub struct Manager {
|
|||
|
||||
impl Manager {
|
||||
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: Config = config.try_into()?;
|
||||
let config = {
|
||||
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 {
|
||||
config_file_path: config_file_path.to_owned(),
|
||||
config: Arc::new(RwLock::new(config)),
|
||||
auth_secret,
|
||||
};
|
||||
|
||||
Ok(manager)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
use std::{
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::app::Error;
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
|
@ -37,16 +32,3 @@ pub struct Config {
|
|||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
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::IndexAlbumArtPatternInvalid => APIError::InvalidAlbumArtPattern,
|
||||
|
||||
app::Error::Toml(_) => APIError::Internal,
|
||||
app::Error::ConfigDeserialization(_) => APIError::Internal,
|
||||
app::Error::IndexDeserializationError => APIError::Internal,
|
||||
app::Error::IndexSerializationError => APIError::Internal,
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue