Skip to content

Commit 77888cd

Browse files
committed
dev: create new settings implementation.
1 parent 2de8181 commit 77888cd

File tree

20 files changed

+390
-25
lines changed

20 files changed

+390
-25
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
/database.db
55
/.idea/
66
/config.toml
7+
/config.toml.old
78
/data.db
89
/.vscode/launch.json
10+
/config.backup/
11+
/config/local.toml

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"cSpell.words": [
33
"byteorder",
4+
"chrono",
45
"hasher",
56
"leechers",
67
"nanos",
8+
"nocapture",
79
"rngs",
810
"Seedable",
911
"thiserror",
1012
"torrust",
11-
"typenum"
13+
"typenum",
14+
"untuple"
1215
],
1316
"[rust]": {
1417
"editor.formatOnSave": true

Cargo.lock

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ async-trait = "0.1"
5959

6060
aquatic_udp_protocol = "0.2"
6161
uuid = { version = "1", features = ["v4"] }
62+
63+
64+
[build-dependencies]
65+
fs_extra = "1.2.0"
66+
#copy_to_output = "2"
67+
glob = "0.3"

build.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::collections::hash_map::DefaultHasher;
2+
use std::fs::{self, create_dir, rename};
3+
use std::hash::{Hash, Hasher};
4+
use std::path::{Path, PathBuf};
5+
use std::{env, panic};
6+
7+
use fs_extra::copy_items;
8+
use fs_extra::dir::CopyOptions;
9+
10+
use crate::config_const::{CONFIG_FOLDER, CONFIG_LOCAL};
11+
12+
pub mod config_const {
13+
pub const CONFIG_FOLDER: &str = "config";
14+
pub const CONFIG_LOCAL: &str = "local";
15+
}
16+
17+
fn main() {
18+
let config_folder = Path::new(CONFIG_FOLDER);
19+
let target_folder = env::var_os("OUT_DIR").map(PathBuf::from).unwrap().join("../../..");
20+
let local_source = config_folder.join(CONFIG_LOCAL).with_extension("toml");
21+
let local_backup_folder = config_folder.with_extension("backup");
22+
let local_backup = local_backup_folder.join(CONFIG_LOCAL);
23+
24+
let t = target_folder.canonicalize();
25+
26+
eprintln!("Target : {target_folder:?} : {t:?}");
27+
28+
// local setting
29+
if local_source.exists() {
30+
if local_backup_folder.exists() {
31+
if !local_backup_folder.is_dir() {
32+
panic!("Exists and is not a Folder!: {local_backup_folder:?}")
33+
}
34+
} else {
35+
create_dir(local_backup_folder).unwrap();
36+
}
37+
38+
let mut hasher = DefaultHasher::new();
39+
40+
fs::read_to_string(&local_source).unwrap().hash(&mut hasher);
41+
42+
rename(
43+
local_source,
44+
local_backup.with_extension(format!("{}-{}", "toml", hasher.finish())),
45+
)
46+
.unwrap();
47+
}
48+
49+
// Re-runs script if any files in config are changed
50+
println!("cargo:rerun-if-changed=config/*");
51+
copy_to_target(config_folder, &target_folder).expect("Could not copy");
52+
}
53+
54+
pub fn copy_to_target(path: &Path, target: &Path) -> Result<u64, fs_extra::error::Error> {
55+
let mut options = CopyOptions::new();
56+
let mut paths = Vec::new();
57+
58+
// Overwrite existing files with same name
59+
options.overwrite = true;
60+
61+
paths.push(path);
62+
match copy_items(&paths, target, &options) {
63+
Ok(r) => Ok(r),
64+
Err(e) => {
65+
let f = path.canonicalize();
66+
let t = target.canonicalize();
67+
eprintln!("Tried to copy from: {f:?}");
68+
eprintln!("Tried to copy to : {t:?}");
69+
Err(e)
70+
}
71+
}
72+
}

config/default.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## This is the default configuration.
2+
## The 'local.toml' is where to store your custom configuration.
3+
4+
## The configurations are loaded in this order (low priorty to high):
5+
## 'config/default.toml'; 'config.toml'; 'config/local.toml'; 'config/override.toml';
6+
## When loading, the 'local.toml' will be overritten with the active configuration!
7+
8+
log_level = "info"
9+
mode = "public"
10+
db_driver = "Sqlite3"
11+
db_path = "data.db"
12+
announce_interval = 120
13+
min_announce_interval = 120
14+
max_peer_timeout = 900
15+
on_reverse_proxy = false
16+
external_ip = "0.0.0.0"
17+
tracker_usage_statistics = true
18+
persistent_torrent_completed_stat = false
19+
inactive_peer_cleanup_interval = 600
20+
remove_peerless_torrents = true
21+
22+
[[udp_trackers]]
23+
enabled = false
24+
bind_address = "0.0.0.0:6969"
25+
26+
[[http_trackers]]
27+
enabled = false
28+
bind_address = "0.0.0.0:6969"
29+
ssl_enabled = false
30+
ssl_cert_path = ""
31+
ssl_key_path = ""
32+
33+
[http_api]
34+
enabled = true
35+
bind_address = "127.0.0.1:1212"
36+
37+
[http_api.access_tokens]
38+
admin = "MyAccessToken"

config/override.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## This is the Override Configuration.
2+
## This will be used when needing to update values for new versions of the software.
3+
## Any values placed here will be loaded and will overrite the local configuration file.

src/config.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl Configuration {
161161

162162
#[cfg(test)]
163163
mod tests {
164+
use crate::config::{Configuration, ConfigurationError};
164165

165166
#[cfg(test)]
166167
fn default_config_toml() -> String {
@@ -205,8 +206,6 @@ mod tests {
205206

206207
#[test]
207208
fn configuration_should_have_default_values() {
208-
use crate::Configuration;
209-
210209
let configuration = Configuration::default();
211210

212211
let toml = toml::to_string(&configuration).expect("Could not encode TOML value");
@@ -216,8 +215,6 @@ mod tests {
216215

217216
#[test]
218217
fn configuration_should_contain_the_external_ip() {
219-
use crate::Configuration;
220-
221218
let configuration = Configuration::default();
222219

223220
assert_eq!(configuration.external_ip, Option::Some(String::from("0.0.0.0")));
@@ -229,8 +226,6 @@ mod tests {
229226

230227
use uuid::Uuid;
231228

232-
use crate::Configuration;
233-
234229
// Build temp config file path
235230
let temp_directory = env::temp_dir();
236231
let temp_file = temp_directory.join(format!("test_config_{}.toml", Uuid::new_v4()));
@@ -275,8 +270,6 @@ mod tests {
275270

276271
#[test]
277272
fn configuration_should_be_loaded_from_a_toml_config_file() {
278-
use crate::Configuration;
279-
280273
let config_file_path = create_temp_config_file_with_default_config();
281274

282275
let configuration = Configuration::load_from_file(&config_file_path).expect("Could not load configuration from file");
@@ -286,8 +279,6 @@ mod tests {
286279

287280
#[test]
288281
fn configuration_error_could_be_displayed() {
289-
use crate::ConfigurationError;
290-
291282
let error = ConfigurationError::TrackerModeIncompatible;
292283

293284
assert_eq!(format!("{}", error), "TrackerModeIncompatible");

src/jobs/http_tracker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use std::sync::Arc;
44
use log::{info, warn};
55
use tokio::task::JoinHandle;
66

7+
use crate::config::HttpTrackerConfig;
78
use crate::tracker::tracker::TorrentTracker;
8-
use crate::{HttpServer, HttpTrackerConfig};
9+
use crate::HttpServer;
910

1011
pub fn start_job(config: &HttpTrackerConfig, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
1112
let bind_addr = config.bind_address.parse::<SocketAddr>().unwrap();

src/jobs/torrent_cleanup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use chrono::Utc;
44
use log::info;
55
use tokio::task::JoinHandle;
66

7+
use crate::config::Configuration;
78
use crate::tracker::tracker::TorrentTracker;
8-
use crate::Configuration;
99

1010
pub fn start_job(config: &Configuration, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
1111
let weak_tracker = std::sync::Arc::downgrade(&tracker);

0 commit comments

Comments
 (0)