Skip to content

Commit

Permalink
Setup the config parser + add configs for GateServer
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Oct 8, 2022
1 parent 1697d65 commit 2609b95
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk


# Added by cargo

/target
8 changes: 8 additions & 0 deletions Cargo.toml
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"
]
11 changes: 11 additions & 0 deletions common_utils/Cargo.toml
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" ] }
1 change: 1 addition & 0 deletions common_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod parser;
72 changes: 72 additions & 0 deletions common_utils/src/parser/config_parser.rs
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");
}
}
1 change: 1 addition & 0 deletions common_utils/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod config_parser;
2 changes: 2 additions & 0 deletions common_utils/tests/invalid_yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test23123
tt43441
2 changes: 2 additions & 0 deletions common_utils/tests/valid_yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id: 5
name: "This is the name"
10 changes: 10 additions & 0 deletions gate_server/Cargo.toml
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" ] }
79 changes: 79 additions & 0 deletions gate_server/src/config.rs
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
});
}
}
10 changes: 10 additions & 0 deletions gate_server/src/main.rs
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"));

}
19 changes: 19 additions & 0 deletions gate_server/tests/test_gate_config.yaml
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 added src/main.rs
Empty file.

0 comments on commit 2609b95

Please sign in to comment.