Skip to content

Commit

Permalink
Merge pull request #7 from snaakey/rename
Browse files Browse the repository at this point in the history
Rename `speed_test_dir` to `assets_path`
  • Loading branch information
SudoDios authored Sep 28, 2024
2 parents a1ea40a + 3707562 commit a74f25d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Compatible with all librespeed clients :
ipinfo_api_key=""
# set directory of speedtest web front to server load on `/`. If it is empty, default web will be returned
speed_test_dir="./assets" # Write without suffix separator
assets_path="./assets" # Write without suffix separator
# password for logging into statistics page, fill this to enable stats page
stats_password=""
Expand Down
4 changes: 2 additions & 2 deletions configs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ base_url="backend"
ipinfo_api_key=""

# set directory of speedtest web front to server load on `/`. If it is empty, default web will be returned
speed_test_dir="./assets" # Write without suffix separator
assets_path="./assets" # Write without suffix separator

# password for logging into statistics page, fill this to enable stats page
stats_password=""
Expand All @@ -34,4 +34,4 @@ database_file="speedtest.db"
# enable and use TLS option; if enable it, you need to prepare certificates and private keys
enable_tls=false
tls_cert_file=""
tls_key_file=""
tls_key_file=""
12 changes: 6 additions & 6 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Cmd {
pub listen_port : Option<u16>,
pub base_url : Option<String>,
pub ipinfo_api_key : Option<String>,
pub speed_test_dir : Option<String>,
pub assets_path : Option<String>,
pub stats_password : Option<String>,
pub database_type : Option<String>,
pub database_hostname : Option<String>,
Expand Down Expand Up @@ -71,8 +71,8 @@ impl Cmd {
.value_parser(value_parser!(String))
)
.arg(
Arg::new("speed-test-dir")
.long("speed-test-dir")
Arg::new("assets-path")
.long("assets-path")
.help("Specify the directory of speedtest web frontend")
.value_parser(value_parser!(String))
)
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Cmd {
let listen_port : Option<u16> = args.get_one::<u16>("listen-port").map(|s| s.to_owned());
let base_url : Option<String> = args.get_one::<String>("base-url").map(|s| s.to_owned());
let ipinfo_api_key : Option<String> = args.get_one::<String>("ipinfo-api-key").map(|s| s.to_owned());
let speed_test_dir : Option<String> = args.get_one::<String>("speed-test-dir").map(|s| s.to_owned());
let assets_path : Option<String> = args.get_one::<String>("assets-path").map(|s| s.to_owned());
let stats_password : Option<String> = args.get_one::<String>("stats-password").map(|s| s.to_owned());
let database_type : Option<String> = args.get_one::<String>("database-type").map(|s| s.to_owned());
let database_hostname : Option<String> = args.get_one::<String>("database-hostname").map(|s| s.to_owned());
Expand All @@ -161,7 +161,7 @@ impl Cmd {
listen_port,
base_url,
ipinfo_api_key,
speed_test_dir,
assets_path,
stats_password,
database_type,
database_hostname,
Expand All @@ -175,4 +175,4 @@ impl Cmd {
}
}

}
}
20 changes: 10 additions & 10 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct ServerConfig {
pub base_url : String,
pub ipinfo_api_key : String,
pub stats_password : String,
pub speed_test_dir : String,
pub assets_path : String,
pub database_type : String,
pub database_hostname : Option<String>,
pub database_name : Option<String>,
Expand All @@ -64,7 +64,7 @@ impl Default for ServerConfig {
base_url: "backend".to_string(),
ipinfo_api_key: "".to_string(),
stats_password: "".to_string(),
speed_test_dir: "".to_string(),
assets_path: "".to_string(),
database_type: "none".to_string(),
database_hostname: None,
database_name: None,
Expand Down Expand Up @@ -189,7 +189,7 @@ fn initialize (mut config: ServerConfig,cmd : Cmd) -> std::io::Result<()> {
config.listen_port.set_if_some(cmd.listen_port);
config.base_url.set_if_some(cmd.base_url);
config.ipinfo_api_key.set_if_some(cmd.ipinfo_api_key);
config.speed_test_dir.set_if_some(cmd.speed_test_dir);
config.assets_path.set_if_some(cmd.assets_path);
config.stats_password.set_if_some(cmd.stats_password);
config.database_type.set_if_some(cmd.database_type);
config.database_hostname.set_if_some(cmd.database_hostname);
Expand All @@ -201,14 +201,14 @@ fn initialize (mut config: ServerConfig,cmd : Cmd) -> std::io::Result<()> {
config.tls_cert_file.set_if_some(cmd.tls_cert_file);
config.tls_key_file.set_if_some(cmd.tls_key_file);
generate_routes(&config.base_url);
if !config.speed_test_dir.is_empty() {
if check_speed_test_dir(&config.speed_test_dir) {
info!("Config speed test directory successfully.")
if !config.assets_path.is_empty() {
if check_assets_path(&config.assets_path) {
info!("Config assets directory successfully.")
} else {
info!("Config speed test directory failed !")
info!("Config assets directory failed !")
}
} else {
info!("Config default speed test directory.")
info!("Config default assets directory.")
}
SERVER_CONFIG.get_or_init(|| config);
//garbage data
Expand All @@ -223,7 +223,7 @@ fn initialize (mut config: ServerConfig,cmd : Cmd) -> std::io::Result<()> {
Ok(())
}

fn check_speed_test_dir (dir : &str) -> bool {
fn check_assets_path (dir : &str) -> bool {
let index_file = format!("{}/index.html",dir);
Path::new(&index_file).exists()
}
Expand All @@ -242,4 +242,4 @@ pub const HEAD_ART : &str = r#"
| | | |_) | | | __/\__ \ |_) | __/ __/ (_| |_____| | \__ \
|_|_|_.__/|_| \___||___/ .__/ \___|\___|\__,_| |_| |___/
|_|
"#;
"#;
6 changes: 3 additions & 3 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn find_remote_ip_addr (conn: &mut TcpStream) -> String {
}

pub fn get_index_file_content(file_name : &str) -> Option<Vec<u8>> {
if SERVER_CONFIG.get()?.speed_test_dir.is_empty() {
if SERVER_CONFIG.get()?.assets_path.is_empty() {
if file_name.contains("servers_list.js") {
Some(generate_server_endpoint())
} else {
Expand All @@ -48,7 +48,7 @@ pub fn get_index_file_content(file_name : &str) -> Option<Vec<u8>> {
Some(Vec::from(file.contents()))
}
} else {
let file_path = format!("{}{}",SERVER_CONFIG.get()?.speed_test_dir,file_name);
let file_path = format!("{}{}",SERVER_CONFIG.get()?.assets_path,file_name);
if let Ok(mut file) = File::open(file_path) {
let mut file_bytes = Vec::new();
if file.read_to_end(&mut file_bytes).is_ok() {
Expand Down Expand Up @@ -108,4 +108,4 @@ macro_rules! make_route {
format!("{}{}",base_url,$a)
}
};
}
}

0 comments on commit a74f25d

Please sign in to comment.