-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup the config parser + add configs for GateServer
- Loading branch information
root
authored and
root
committed
Oct 8, 2022
1 parent
1697d65
commit 2609b95
Showing
13 changed files
with
220 additions
and
0 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 |
---|---|---|
|
@@ -8,3 +8,8 @@ Cargo.lock | |
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
|
||
# Added by cargo | ||
|
||
/target |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[workspace] | ||
|
||
members = [ | ||
"gate_server", | ||
"common_utils" | ||
] |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "common_utils" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.65" | ||
serde_yaml = "0.9.13" | ||
serde = { version = "1.0", features = [ "derive" ] } |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod parser; |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use serde::{Serialize, Deserialize}; | ||
use serde::de::DeserializeOwned; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ConfigParser<T> { | ||
data: T | ||
} | ||
|
||
impl<T> ConfigParser<T> { | ||
pub fn parse_file(file_path: &Path) -> Self | ||
where | ||
T: DeserializeOwned { | ||
let contents = match fs::read_to_string (file_path) { | ||
Ok(contents) => contents, | ||
Err(err) => panic!("Could not find config file at {:?}, error - {}", file_path, err) | ||
}; | ||
|
||
let data: T = match serde_yaml::from_str(&contents) { | ||
Ok(yaml) => yaml, | ||
Err(err) => panic!("Unable to parse the YAML config, err - {}", err) | ||
}; | ||
|
||
let config_parser: ConfigParser<T> = ConfigParser { | ||
data | ||
}; | ||
|
||
config_parser | ||
} | ||
|
||
pub fn get_data(self) -> T { | ||
self.data | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use super::ConfigParser; | ||
|
||
use serde::{Serialize, Deserialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct ConfigStructure { | ||
name: String, | ||
id: u64 | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn it_throws_an_error_if_file_doesnt_exist() { | ||
ConfigParser::<ConfigStructure>::parse_file(Path::new("./tests/yada.yml")); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn it_throws_an_error_if_the_file_is_not_valid_yaml() { | ||
ConfigParser::<ConfigStructure>::parse_file(Path::new("./tests/invalid_yaml.yml")); | ||
} | ||
|
||
#[test] | ||
fn it_parses_a_valid_yml_file_correctly() { | ||
let contents = ConfigParser::<ConfigStructure>::parse_file(Path::new("./tests/valid_yaml.yml")); | ||
let data = contents.get_data(); | ||
|
||
assert_eq!(data.id, 5); | ||
assert_eq!(data.name, "This is the name"); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod config_parser; |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test23123 | ||
tt43441 |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
id: 5 | ||
name: "This is the name" |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "gate_server" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
common_utils = { path = "../common_utils" } | ||
serde = { version = "1.0", features = [ "derive" ] } |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::{net::{Ipv4Addr}, path::Path}; | ||
use common_utils::parser::config_parser::ConfigParser; | ||
use serde::Deserialize; | ||
|
||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
struct GroupConfig { | ||
ip: Ipv4Addr, | ||
port: u16, | ||
ping_duration: u16 | ||
} | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
struct ClientConfig { | ||
ip: Ipv4Addr, | ||
port: u16, | ||
ping_duration: u16, | ||
max_connections: u32, | ||
wpe_protection: bool, | ||
wpe_version: u16, | ||
max_login_per_ip: u16, | ||
ddos_protection: bool, | ||
} | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
struct GameServerConfig { | ||
ip: Ipv4Addr, | ||
port: u16, | ||
ping_duration: u16, | ||
} | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
pub struct GateServerConfig { | ||
group_config: GroupConfig, | ||
client_config: ClientConfig, | ||
game_server_config: GameServerConfig, | ||
} | ||
|
||
pub fn parse_config(path: &Path) -> GateServerConfig { | ||
let parser = ConfigParser::<GateServerConfig>::parse_file(path); | ||
|
||
parser.get_data() | ||
} | ||
|
||
mod tests { | ||
use std::{net::{SocketAddrV4, Ipv4Addr}, path::Path}; | ||
|
||
use crate::config::{GroupConfig, GameServerConfig}; | ||
|
||
use super::{parse_config, ClientConfig}; | ||
|
||
#[test] | ||
fn it_should_parse_the_gateserver_config_correctly() { | ||
let data = parse_config(Path::new("./tests/test_gate_config.yaml")); | ||
|
||
assert_eq!(data.client_config, ClientConfig { | ||
ip: Ipv4Addr::new(127, 0, 0, 1), | ||
ddos_protection: true, | ||
max_connections: 500, | ||
max_login_per_ip: 50, | ||
ping_duration: 180, | ||
port: 3000, | ||
wpe_protection: true, | ||
wpe_version: 30 | ||
}); | ||
|
||
assert_eq!(data.group_config, GroupConfig { | ||
ip: Ipv4Addr::new(127, 0, 0, 1), | ||
ping_duration: 180, | ||
port: 3001 | ||
}); | ||
|
||
assert_eq!(data.game_server_config, GameServerConfig { | ||
ip: Ipv4Addr::new(127, 0, 0, 1), | ||
ping_duration: 180, | ||
port: 3002 | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::path::Path; | ||
|
||
use config::parse_config; | ||
|
||
mod config; | ||
|
||
fn main() { | ||
let config = parse_config(Path::new("./config.yaml")); | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
client_config: | ||
ip: "127.0.0.1" | ||
ddos_protection: true | ||
max_connections: 500 | ||
max_login_per_ip: 50 | ||
ping_duration: 180 | ||
port: 3000 | ||
wpe_protection: true | ||
wpe_version: 30 | ||
|
||
group_config: | ||
ip: "127.0.0.1" | ||
port: 3001 | ||
ping_duration: 180 | ||
|
||
game_server_config: | ||
ip: "127.0.0.1" | ||
port: 3002 | ||
ping_duration: 180 |
Empty file.