Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move dirpath and password to extensions.rs #3

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"port": 19194,
"workers": 2
},
"dir_path": ".",
"password": "$Xme-Ef9r[@EsqF"
"extensions": {
"dir_path": ".",
"password": "$Xme-Ef9r[@EsqF"
}
}
33 changes: 24 additions & 9 deletions generated/.spacepls.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Extensions": {
"description": "Used to store crucial information like password and dir path",
"properties": {
"dir_path": {
"default": null,
"description": "The directory path to store files.",
"type": ["string", "null"]
},
"password": {
"default": null,
"description": "Password",
"type": ["string", "null"]
}
},
"type": "object"
},
"Proxy": {
"properties": {
"url": {
Expand Down Expand Up @@ -113,15 +129,13 @@
}
},
"properties": {
"dir_path": {
"default": null,
"description": "The directory path to store files.",
"type": ["string", "null"]
},
"password": {
"default": null,
"description": "Password",
"type": ["string", "null"]
"extensions": {
"allOf": [
{
"$ref": "#/definitions/Extensions"
}
],
"description": "Used to store crucial information like password and dir path"
},
"server": {
"allOf": [
Expand All @@ -142,6 +156,7 @@
"description": "Dictates how SpacePls should handle upstream requests/responses. Tuning upstream can improve performance and reliability for connections."
}
},
"required": ["extensions"],
"title": "Config",
"type": "object"
}
10 changes: 5 additions & 5 deletions src/blueprint/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ impl TryFrom<&Config> for Blueprint {
fn try_from(config: &Config) -> Result<Self, Self::Error> {
let server = Server::try_from(config)?;
let upstream = Upstream::try_from(config)?;
let dir_path = config
.dir_path
.clone()
.context("No dir path found in config, use config reader to read config instead.")?;
let password = config.password.clone().context(
let dir_path =
config.extensions.dir_path.clone().context(
"No dir path found in config, use config reader to read config instead.",
)?;
let password = config.extensions.password.clone().context(
"No password for the files provided. Use config reader to read config instead.",
)?;
Ok(Self {
Expand Down
16 changes: 6 additions & 10 deletions src/config/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::extensions::Extensions;
use crate::config::server::Server;
use crate::config::Upstream;
use derive_setters::Setters;
Expand All @@ -19,27 +20,22 @@ pub struct Config {
#[serde(default)]
pub upstream: Upstream,

/// The directory path to store files.
#[serde(default)]
pub dir_path: Option<String>,
/// Password
#[serde(default)]
pub password: Option<String>,
/// Used to store crucial information like
/// password and dir path
pub extensions: Extensions,
}

impl Config {
/// Merge configs, preferring the right side.
pub fn merge_right(self, other: &Self) -> Self {
let server = self.server.merge_right(other.server.clone());
let upstream = self.upstream.merge_right(other.upstream.clone());
let dir_path = other.clone().dir_path.or(self.dir_path);
let password = other.clone().password.or(self.password);
let extensions = self.extensions.merge_right(other.extensions.clone());

Self {
server,
upstream,
dir_path,
password,
extensions,
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/config/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use derive_setters::Setters;
use serde::{Deserialize, Serialize};

#[derive(
Serialize, Deserialize, Clone, Debug, Default, Setters, PartialEq, Eq, schemars::JsonSchema,
)]
/// Used to store crucial information like
/// password and dir path
pub struct Extensions {
/// The directory path to store files.
#[serde(default)]
pub dir_path: Option<String>,
/// Password
#[serde(default)]
pub password: Option<String>,
}

impl Extensions {
pub(crate) fn merge_right(self, other: Extensions) -> Extensions {
let dir_path = other.clone().dir_path.or(self.dir_path);
let password = other.clone().password.or(self.password);
Self { dir_path, password }
}
}
1 change: 1 addition & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use config::*;
pub use server::*;
pub use upstream::*;
mod config;
mod extensions;
pub mod reader;
mod server;
mod upstream;
Expand Down
6 changes: 3 additions & 3 deletions src/config/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ impl ConfigReader {
})
}
async fn resolve(mut config: Config, parent_dir: Option<&Path>) -> Config {
let dir = config.dir_path.unwrap_or("spacepls".to_string());
let dir = config.extensions.dir_path.unwrap_or("spacepls".to_string());
let dir = Self::resolve_path(&dir, parent_dir);
config.dir_path = Some(dir);
config.password = Some("$Xme-Ef9r[@EsqF".to_string()); // randomly generated password
config.extensions.dir_path = Some(dir);
config.extensions.password = Some("$Xme-Ef9r[@EsqF".to_string()); // randomly generated password

config
}
Expand Down