neon_arch commited on
Commit
4b4dc28
1 Parent(s): 26b5907

✨ feat: add control check to avoid reinitializing logging & fix tests

Browse files
Files changed (3) hide show
  1. src/bin/websurfx.rs +1 -1
  2. src/config/parser.rs +17 -9
  3. tests/index.rs +2 -2
src/bin/websurfx.rs CHANGED
@@ -15,7 +15,7 @@ use websurfx::{config::parser::Config, run};
15
  #[actix_web::main]
16
  async fn main() -> std::io::Result<()> {
17
  // Initialize the parsed config file.
18
- let config = Config::parse().unwrap();
19
 
20
  log::info!(
21
  "started server on port {} and IP {}",
 
15
  #[actix_web::main]
16
  async fn main() -> std::io::Result<()> {
17
  // Initialize the parsed config file.
18
+ let config = Config::parse(true).unwrap();
19
 
20
  log::info!(
21
  "started server on port {} and IP {}",
src/config/parser.rs CHANGED
@@ -54,12 +54,17 @@ impl Config {
54
  /// A function which parses the config.lua file and puts all the parsed options in the newly
55
  /// constructed Config struct and returns it.
56
  ///
 
 
 
 
 
57
  /// # Error
58
  ///
59
  /// Returns a lua parse error if parsing of the config.lua file fails or has a syntax error
60
  /// or io error if the config.lua file doesn't exists otherwise it returns a newly constructed
61
  /// Config struct with all the parsed config options from the parsed config file.
62
- pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
63
  Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
64
  let globals = context.globals();
65
 
@@ -72,14 +77,17 @@ impl Config {
72
  let debug: bool = globals.get::<_, bool>("debug")?;
73
  let logging:bool= globals.get::<_, bool>("logging")?;
74
 
75
- // Initializing logging middleware with level set to default or info.
76
- let mut log_level: LevelFilter = LevelFilter::Off;
77
- if logging && debug == false {
78
- log_level = LevelFilter::Info;
79
- } else if debug {
80
- log_level = LevelFilter::Trace;
81
- };
82
- env_logger::Builder::new().filter(None, log_level).init();
 
 
 
83
 
84
  let threads: u8 = if parsed_threads == 0 {
85
  let total_num_of_threads:usize = available_parallelism()?.get() /2;
 
54
  /// A function which parses the config.lua file and puts all the parsed options in the newly
55
  /// constructed Config struct and returns it.
56
  ///
57
+ /// # Arguments
58
+ ///
59
+ /// * `logging_initialized` - It takes a boolean which ensures that the logging doesn't get
60
+ /// initialized twice.
61
+ ///
62
  /// # Error
63
  ///
64
  /// Returns a lua parse error if parsing of the config.lua file fails or has a syntax error
65
  /// or io error if the config.lua file doesn't exists otherwise it returns a newly constructed
66
  /// Config struct with all the parsed config options from the parsed config file.
67
+ pub fn parse(logging_initialized: bool) -> Result<Self, Box<dyn std::error::Error>> {
68
  Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
69
  let globals = context.globals();
70
 
 
77
  let debug: bool = globals.get::<_, bool>("debug")?;
78
  let logging:bool= globals.get::<_, bool>("logging")?;
79
 
80
+ // Check whether logging has not been initialized before.
81
+ if logging_initialized {
82
+ // Initializing logging middleware with level set to default or info.
83
+ let mut log_level: LevelFilter = LevelFilter::Off;
84
+ if logging && debug == false {
85
+ log_level = LevelFilter::Info;
86
+ } else if debug {
87
+ log_level = LevelFilter::Trace;
88
+ };
89
+ env_logger::Builder::new().filter(None, log_level).init();
90
+ }
91
 
92
  let threads: u8 = if parsed_threads == 0 {
93
  let total_num_of_threads:usize = available_parallelism()?.get() /2;
tests/index.rs CHANGED
@@ -8,7 +8,7 @@ fn spawn_app() -> String {
8
  // Binding to port 0 will trigger the OS to assign a port for us.
9
  let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
10
  let port = listener.local_addr().unwrap().port();
11
- let config = Config::parse().unwrap();
12
  let server = run(listener, config).expect("Failed to bind address");
13
 
14
  tokio::spawn(server);
@@ -36,7 +36,7 @@ async fn test_index() {
36
  assert_eq!(res.status(), 200);
37
 
38
  let handlebars = handlebars();
39
- let config = Config::parse().unwrap();
40
  let template = handlebars.render("index", &config.style).unwrap();
41
  assert_eq!(res.text().await.unwrap(), template);
42
  }
 
8
  // Binding to port 0 will trigger the OS to assign a port for us.
9
  let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
10
  let port = listener.local_addr().unwrap().port();
11
+ let config = Config::parse(true).unwrap();
12
  let server = run(listener, config).expect("Failed to bind address");
13
 
14
  tokio::spawn(server);
 
36
  assert_eq!(res.status(), 200);
37
 
38
  let handlebars = handlebars();
39
+ let config = Config::parse(false).unwrap();
40
  let template = handlebars.render("index", &config.style).unwrap();
41
  assert_eq!(res.text().await.unwrap(), template);
42
  }