Add config to chatserver
Options can be overridden via cli
This commit is contained in:
parent
b478dbe7a6
commit
ec94d0445e
|
@ -114,6 +114,12 @@ version = "0.2.112"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.4.1"
|
||||
|
@ -190,19 +196,52 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "retain_mut"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11000e6ba5020e53e7cc26f73b91ae7d5496b4977851479edb66b694c0675c21"
|
||||
|
||||
[[package]]
|
||||
name = "rustchat"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"retain_mut",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.132"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b9875c23cf305cd1fd7eb77234cbb705f21ea6a72c637a5c6db5fe4b8e7f008"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.132"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ecc0db5cb2556c0e558887d9bbdcf6ac4471e83ff66cf696e5419024d1606276"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"ryu",
|
||||
"serde",
|
||||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -302,3 +341,12 @@ name = "winapi-x86_64-pc-windows-gnu"
|
|||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "yaml-rust"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
|
||||
dependencies = [
|
||||
"linked-hash-map",
|
||||
]
|
||||
|
|
|
@ -9,3 +9,5 @@ edition = "2021"
|
|||
[dependencies]
|
||||
clap = { version = "3.0.0-rc.9", features = ["derive"] }
|
||||
chrono = "0.4"
|
||||
serde = { version = "1.0", features = ["derive"]}
|
||||
serde_yaml = "0.8"
|
||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -5,10 +5,9 @@ use clap::Parser;
|
|||
use std::io::Write;
|
||||
use std::io::{self, BufRead};
|
||||
use chrono::{DateTime, Utc};
|
||||
//use std::borrow::{Borrow, BorrowMut};
|
||||
//use retain_mut::RetainMut;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::fs::File;
|
||||
|
||||
|
||||
/// Represent one single chat user and their network stuff
|
||||
|
@ -30,13 +29,14 @@ impl ChatUser {
|
|||
}
|
||||
|
||||
struct ChatServer {
|
||||
config: ConfigArgs,
|
||||
user_map: BTreeMap<u64, ChatUser>,
|
||||
user_id_counter: u64,
|
||||
}
|
||||
|
||||
impl ChatServer {
|
||||
pub fn new() -> Self {
|
||||
Self{user_map: BTreeMap::new(), user_id_counter: 1}
|
||||
pub fn new(config: ConfigArgs) -> Self {
|
||||
Self{config, user_map: BTreeMap::new(), user_id_counter: 1}
|
||||
}
|
||||
|
||||
pub fn get_user_count(&self) -> usize {
|
||||
|
@ -156,22 +156,46 @@ fn run_thread(stream: TcpStream, server: Arc<Mutex<ChatServer>>) {
|
|||
#[derive(Parser, Debug)]
|
||||
#[clap(about, version, author)]
|
||||
struct Args {
|
||||
/// Path to config file
|
||||
#[clap(short, long, default_value = "config.yaml")]
|
||||
config: String,
|
||||
|
||||
/// IP address to bind to
|
||||
#[clap(long, default_value = "0.0.0.0")]
|
||||
host: String,
|
||||
#[clap(long)]
|
||||
host: Option<String>,
|
||||
|
||||
/// Port to run the server on
|
||||
#[clap(short, long, default_value_t = 1337)]
|
||||
port: i16,
|
||||
#[clap(short, long)]
|
||||
port: Option<u16>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct ConfigArgs {
|
||||
host: String,
|
||||
port: u16,
|
||||
greeting_msg: String,
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
// argument parsing
|
||||
let args = Args::parse();
|
||||
let binding_host = format!("{}:{}", args.host, args.port);
|
||||
|
||||
// read config file
|
||||
let config_reader = File::open(args.config).expect("Could not open config file");
|
||||
let mut config: ConfigArgs = serde_yaml::from_reader(&config_reader).expect("Could not parse config file");
|
||||
|
||||
if let Some(host) = args.host {
|
||||
config.host = host;
|
||||
}
|
||||
if let Some(port) = args.port {
|
||||
config.port = port;
|
||||
}
|
||||
let binding_host = format!("{}:{}", config.host, config.port);
|
||||
println!("Binding to {}", binding_host);
|
||||
|
||||
let listener = TcpListener::bind(binding_host)?;
|
||||
let server = Arc::new(Mutex::new(ChatServer::new()));
|
||||
let server = Arc::new(Mutex::new(ChatServer::new(config)));
|
||||
|
||||
// accept connections and process them serially
|
||||
for stream in listener.incoming() {
|
||||
|
|
Loading…
Reference in New Issue