Skip to content

Commit

Permalink
cleaned up testing/production detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare authored and johan-bjareholt committed Mar 25, 2020
1 parent 6024d7d commit 5b52961
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
30 changes: 15 additions & 15 deletions aw-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ fn default_address() -> String {
}

fn default_port() -> u16 {
match Environment::active().expect("Failed to get current environment") {
Environment::Production => 5600,
Environment::Development => 5666,
Environment::Staging => panic!("Staging environment not supported"),
if is_production() {
5600
} else {
5666
}
}

fn default_cors() -> Vec<String> {
match Environment::active().expect("Failed to get current environment") {
Environment::Production => Vec::<String>::new(),
Environment::Development => Vec::<String>::new(),
Environment::Staging => panic!("Staging environment not supported"),
}
Vec::<String>::new()
}

pub fn is_production() -> bool {
!is_testing()
}

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

pub fn get_config() -> AWConfig {
let mut config_path = dirs::get_config_dir().unwrap();
match is_testing() {
false => config_path.push("config.toml"),
true => config_path.push("config-testing.toml"),
if is_production() {
config_path.push("config.toml")
} else {
config_path.push("config-testing.toml")
}

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

debug!("Reading config at {:?}", config_path);
let mut rfile =
File::open(config_path.clone()).expect("Failed to open config file for reading");
let mut rfile = File::open(config_path).expect("Failed to open config file for reading");
let mut content = String::new();
rfile
.read_to_string(&mut content)
Expand Down
26 changes: 10 additions & 16 deletions aw-server/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@ use std::path::PathBuf;

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

use crate::config::is_production;
use crate::dirs;

pub fn setup_logger() -> Result<(), fern::InitError> {
let mut logfile_path: PathBuf =
dirs::get_log_dir().expect("Unable to get log dir to store logs in");
fs::create_dir_all(logfile_path.clone()).expect("Unable to create folder for logs");
#[cfg(debug_assertions)]
{
logfile_path.push(
chrono::Local::now()
.format("aw-server-testing_%Y-%m-%dT%H-%M-%S%z.log")
.to_string(),
);
}
#[cfg(not(debug_assertions))]
{
logfile_path.push(
chrono::Local::now()
.format("aw-server_%Y-%m-%dT%H-%M-%S%z.log")
.to_string(),
);
}
logfile_path.push(
chrono::Local::now()
.format(if is_production() {
"aw-server_%Y-%m-%dT%H-%M-%S%z.log"
} else {
"aw-server-testing_%Y-%m-%dT%H-%M-%S%z.log"
})
.to_string(),
);

let colors = ColoredLevelConfig::new()
.debug(Color::White)
Expand Down

0 comments on commit 5b52961

Please sign in to comment.