-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6af1624
commit 5a8eb21
Showing
5 changed files
with
59 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,58 @@ | ||
use nanoserde::{Toml, TomlParser}; | ||
|
||
#[derive(Debug)] | ||
pub struct Config { | ||
pub port: u16, | ||
pub tls: CfgTLS, | ||
} | ||
|
||
pub struct CfgTLS { | ||
pub enable: bool, | ||
pub cert: String, | ||
pub key: String, | ||
} | ||
|
||
pub fn config() -> Config { | ||
let cfg = TomlParser::parse(&match std::fs::read_to_string("config.toml") { | ||
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { | ||
std::fs::write("config.toml", "").unwrap(); | ||
String::new() | ||
} | ||
Ok(cfg) => cfg, | ||
Err(e) => { | ||
panic!("Config file could not be read: {}", e); | ||
let cfg = TomlParser::parse( | ||
&match std::fs::read_to_string("config.toml") { | ||
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { | ||
std::fs::write("config.toml", "").unwrap(); | ||
String::new() | ||
} | ||
Ok(cfg) => cfg, | ||
Err(e) => { | ||
panic!("Config file could not be read: {}", e); | ||
} | ||
} | ||
}) | ||
.lines() | ||
.filter(|line| !line.starts_with('#')) // Remove comments, parser can't handle them | ||
.map(|line| line.to_string() + "\n") | ||
.collect::<String>(), | ||
) | ||
.expect("Invalid config file"); | ||
|
||
#[allow(clippy::cast_sign_loss)] | ||
#[allow(clippy::cast_possible_truncation)] | ||
let port: u16 = cfg.get("port").unwrap_or(&Toml::Num(8080.0)).num() as u16; | ||
|
||
Config { port } | ||
let tlsenable = cfg.get("tls.enable").unwrap_or(&Toml::Bool(false)); | ||
|
||
let tlscert = cfg | ||
.get("tls.cert") | ||
.unwrap_or(&Toml::Str(String::new())) | ||
.str() | ||
.to_string(); | ||
let tlskey = cfg | ||
.get("tls.key") | ||
.unwrap_or(&Toml::Str(String::new())) | ||
.str() | ||
.to_string(); | ||
|
||
Config { | ||
port, | ||
tls: CfgTLS { | ||
enable: tlsenable == &Toml::Bool(true), | ||
cert: tlscert, | ||
key: tlskey, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters