Skip to content

Commit

Permalink
remove hocon
Browse files Browse the repository at this point in the history
  • Loading branch information
timzaak committed Jul 1, 2024
1 parent 355983e commit 389d6fd
Show file tree
Hide file tree
Showing 29 changed files with 387 additions and 986 deletions.
474 changes: 221 additions & 253 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/bin/main.rs"
spa-server = { path = "../server" }

# web request
reqwest = { version = "0.11.27", features = ["json", "blocking", "multipart", "stream", "rustls-tls"], default-features = false }
reqwest = { version = "0.12", features = ["json", "blocking", "multipart", "stream", "rustls-tls"], default-features = false }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "io-std", "sync", "time", "tokio-macros", "test-util"] }
futures = "0.3"

Expand All @@ -35,7 +35,6 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

#config
hocon = "0.9"
toml = "0.8.14"
toml_edit = "0.22.14"
serde = { version = "1.0", features = ["derive"] }
Expand Down
10 changes: 0 additions & 10 deletions client/client_config_default.conf

This file was deleted.

80 changes: 2 additions & 78 deletions client/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use anyhow::anyhow;
use hocon::HoconLoader;
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;
use toml_edit::DocumentMut;
use tracing::warn;

//const ENV_CONFIG: &str = include_str!("../client_config_env.conf");

#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct Config {
Expand All @@ -30,58 +26,7 @@ fn env_opt(key: &str) -> Option<String> {

impl Config {
pub fn load(config_dir: Option<PathBuf>) -> anyhow::Result<Config> {
if config_dir.as_ref().is_some_and(|f| f.display().to_string().ends_with(".conf")) {
Self::load_hocon(config_dir)
} else {
Self::load_toml(config_dir)
}
}

fn load_hocon(config_dir: Option<PathBuf>) -> anyhow::Result<Self> {
warn!("config format: conf would not support in future, please use toml");
let mut conf_loader = HoconLoader::new();
let config_dir = config_dir.or_else(|| {
std::env::current_exe().ok().and_then(|p| {
p.parent().and_then(|p| {
let p = p.join("config.conf");
if p.exists() {
Some(p)
} else {
None
}
})
})
});

if let Some(config_dir) = config_dir {
conf_loader = conf_loader.load_file(config_dir)?;
}

let conf = conf_loader.hocon()?;
// Hocon has a problem, new will override old even new is error.
// and environment variable will be empty string if not exists.
let admin_server = AdminServerConfig {
address: conf["server"]["address"]
.as_string()
.or(env_opt("SPA_SERVER_ADDRESS"))
.ok_or(anyhow!("server.address could not get"))?,
auth_token: conf["server"]["auth_token"]
.as_string()
.or(env_opt("SPA_SERVER_AUTH_TOKEN"))
.ok_or(anyhow!("server.auth_token could not get"))?,
};
let uploading_config = UploadConfig {
parallel: conf["upload"]["parallel"]
.as_string()
.or(env_opt("SPA_UPLOAD_PARALLEL"))
.and_then(|x| x.parse::<u32>().ok())
.unwrap_or(3),
};
let config: Config = Config {
server: admin_server,
upload: uploading_config,
};
Ok(config)
Self::load_toml(config_dir)
}
fn load_toml(config_dir: Option<PathBuf>) -> anyhow::Result<Self> {
let mut conf = DocumentMut::new();
Expand Down Expand Up @@ -156,26 +101,5 @@ pub(crate) mod test {
let c = Config::load(None);
assert!(c.is_err());
}
#[test]
fn load_env_equal() {
init_env();
assert_eq!(
Config::load_hocon(None).unwrap(),
Config::load_toml(None).unwrap()
);
}
fn get_project_path() -> PathBuf {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let path = path.parent().unwrap();
path.to_owned()
}
#[test]
fn load_file_equal() {
remove_env();
let path = get_project_path().join("client");
assert_eq!(
Config::load_hocon(Some(path.join("client_config_default.conf"))).unwrap(),
Config::load_toml(Some(path.join("client_config_default.toml"))).unwrap()
);
}

}
3 changes: 3 additions & 0 deletions docs/develop/change-log.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Change Log
### Version 2.4.0
-
- conf: remove hocon config

### Version 2.3.0

Expand Down
4 changes: 3 additions & 1 deletion docs/guide/break-changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Break Changes
## V2.3.0
## V2.4.0
* remove hocon config format, change to toml.
## V2.3.0(2024-07-01)
* spa-server: `https.http_redirect_to_https` move to `http.redirect_https`, and value is bool.
## V2.2.2(2024-06-18)
* spa-server: `http_redirect_to_https` convert from bool to u32.
Expand Down
22 changes: 1 addition & 21 deletions docs/guide/spa-client-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,4 @@ auth_token = "token"
# [upload]
## default value is:3
# parallel = 3
```

## Hocon Format Config

**Attention: hocon format would not support in the future.**

```hocon
# admin server address and auth
server {
# required
address: "http://127.0.0.1:9000"
# required
auth_token: "token"
}
# uploading file thread number.
upload {
# optional, default value is 3.
parallel: 3
}
```
```
5 changes: 0 additions & 5 deletions docs/guide/spa-server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,3 @@ addr = "0.0.0.0"
# extension_names = ['icon', 'gif', 'jpg', 'jpeg', 'png', 'js']

```

## Hocon Format Config

**Attention: hocon format would not support in the future.**
the format is like Toml. you could check it from toml format.
1 change: 0 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
delay_timer = "0.11.6" # dashmap is same

#config
hocon = "0.9"
toml = { version = "0.8.14" }
serde = { version = "1.0", features = ["derive"] }
serde_repr = "0.1"
Expand Down
34 changes: 1 addition & 33 deletions server/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{bail, Context};
use hocon::de::wrappers::Serde;
use serde::Deserialize;
use small_acme::LetsEncrypt;
use std::time::Duration;
Expand Down Expand Up @@ -27,11 +26,7 @@ impl Config {
pub fn load() -> anyhow::Result<Self> {
let config_path = env::var("SPA_CONFIG").unwrap_or(CONFIG_PATH.to_string());

let config = if config_path.ends_with(".conf") {
Self::load_hocon(&config_path)
} else {
Self::load_toml(&config_path)
}?;
let config = Self::load_toml(&config_path)?;
if config.http.is_none() && config.https.is_none() {
bail!("should set http or https server config")
}
Expand Down Expand Up @@ -68,15 +63,6 @@ impl Config {
Ok(config)
}

fn load_hocon(path: &str) -> anyhow::Result<Self> {
warn!("config format: conf would not support in future, please use toml");
let load_file = hocon::HoconLoader::new()
.load_file(path)
.with_context(|| format!("can not read config file: {path}"))?;
load_file
.resolve::<Config>()
.with_context(|| "parse config file error")
}
fn load_toml(path: &str) -> anyhow::Result<Self> {
let config = fs::read_to_string(path)
.with_context(|| format!("can not read config file: {path}"))?;
Expand Down Expand Up @@ -198,7 +184,6 @@ fn default_max_size() -> u64 {

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct ClientCacheItem {
#[serde(deserialize_with = "Serde::<Duration>::with")]
pub expire: Duration,
pub extension_names: Vec<String>,
}
Expand Down Expand Up @@ -235,7 +220,6 @@ pub fn get_host_path_from_domain(domain: &str) -> (&str, &str) {

#[cfg(test)]
mod test {
use crate::config::Config;
use std::env;
use std::path::PathBuf;

Expand All @@ -244,20 +228,4 @@ mod test {
let path = path.parent().unwrap();
path.to_owned()
}
#[test]
fn test_hocon_toml_is_same() {
let path = get_project_path();
env::set_var(
"SPA_CONFIG",
path.join("config.release.conf").display().to_string(),
);

let hocon_config = Config::load().unwrap();
env::set_var(
"SPA_CONFIG",
path.join("config.release.toml").display().to_string(),
);
let toml_config = Config::load().unwrap();
assert_eq!(hocon_config, toml_config);
}
}
1 change: 1 addition & 0 deletions server/src/domain_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ mod test {
#[ignore]
#[test]
fn test_domain_storage_get_domain_info() {
//TODO: fix config path
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../test/config.test.conf");
env::set_var("SPA_CONFIG", path.display().to_string());
let mut config = Config::load().unwrap();
Expand Down
13 changes: 0 additions & 13 deletions server/tests/config_test.rs

This file was deleted.

28 changes: 0 additions & 28 deletions server/tests/data/test1.conf

This file was deleted.

Loading

0 comments on commit 389d6fd

Please sign in to comment.