Skip to content

Commit 5b52961

Browse files
ErikBjarejohan-bjareholt
authored andcommitted
cleaned up testing/production detection
1 parent 6024d7d commit 5b52961

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

aw-server/src/config.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ fn default_address() -> String {
4848
}
4949

5050
fn default_port() -> u16 {
51-
match Environment::active().expect("Failed to get current environment") {
52-
Environment::Production => 5600,
53-
Environment::Development => 5666,
54-
Environment::Staging => panic!("Staging environment not supported"),
51+
if is_production() {
52+
5600
53+
} else {
54+
5666
5555
}
5656
}
5757

5858
fn default_cors() -> Vec<String> {
59-
match Environment::active().expect("Failed to get current environment") {
60-
Environment::Production => Vec::<String>::new(),
61-
Environment::Development => Vec::<String>::new(),
62-
Environment::Staging => panic!("Staging environment not supported"),
63-
}
59+
Vec::<String>::new()
60+
}
61+
62+
pub fn is_production() -> bool {
63+
!is_testing()
6464
}
6565

66-
fn is_testing() -> bool {
66+
pub fn is_testing() -> bool {
6767
match Environment::active().expect("Failed to get current environment") {
6868
Environment::Production => false,
6969
Environment::Development => true,
@@ -73,9 +73,10 @@ fn is_testing() -> bool {
7373

7474
pub fn get_config() -> AWConfig {
7575
let mut config_path = dirs::get_config_dir().unwrap();
76-
match is_testing() {
77-
false => config_path.push("config.toml"),
78-
true => config_path.push("config-testing.toml"),
76+
if is_production() {
77+
config_path.push("config.toml")
78+
} else {
79+
config_path.push("config-testing.toml")
7980
}
8081

8182
/* If there is no config file, create a new config file with default values but every value is
@@ -98,8 +99,7 @@ pub fn get_config() -> AWConfig {
9899
}
99100

100101
debug!("Reading config at {:?}", config_path);
101-
let mut rfile =
102-
File::open(config_path.clone()).expect("Failed to open config file for reading");
102+
let mut rfile = File::open(config_path).expect("Failed to open config file for reading");
103103
let mut content = String::new();
104104
rfile
105105
.read_to_string(&mut content)

aw-server/src/logging.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,22 @@ use std::path::PathBuf;
33

44
use fern::colors::{Color, ColoredLevelConfig};
55

6+
use crate::config::is_production;
67
use crate::dirs;
78

89
pub fn setup_logger() -> Result<(), fern::InitError> {
910
let mut logfile_path: PathBuf =
1011
dirs::get_log_dir().expect("Unable to get log dir to store logs in");
1112
fs::create_dir_all(logfile_path.clone()).expect("Unable to create folder for logs");
12-
#[cfg(debug_assertions)]
13-
{
14-
logfile_path.push(
15-
chrono::Local::now()
16-
.format("aw-server-testing_%Y-%m-%dT%H-%M-%S%z.log")
17-
.to_string(),
18-
);
19-
}
20-
#[cfg(not(debug_assertions))]
21-
{
22-
logfile_path.push(
23-
chrono::Local::now()
24-
.format("aw-server_%Y-%m-%dT%H-%M-%S%z.log")
25-
.to_string(),
26-
);
27-
}
13+
logfile_path.push(
14+
chrono::Local::now()
15+
.format(if is_production() {
16+
"aw-server_%Y-%m-%dT%H-%M-%S%z.log"
17+
} else {
18+
"aw-server-testing_%Y-%m-%dT%H-%M-%S%z.log"
19+
})
20+
.to_string(),
21+
);
2822

2923
let colors = ColoredLevelConfig::new()
3024
.debug(Color::White)

0 commit comments

Comments
 (0)