diff options
| author | omagdy <omar.professional8777@gmail.com> | 2025-07-20 16:29:34 +0300 |
|---|---|---|
| committer | omagdy <omar.professional8777@gmail.com> | 2025-07-20 16:29:34 +0300 |
| commit | 846479e6bcf8238879546534b09e141b9bb668f8 (patch) | |
| tree | ac2e65b5842bea095107c0a750843d0cf7bc74b4 /src/main.rs | |
| parent | 38b649ea16d8ed053fd9222bfb9867e3432ee2a6 (diff) | |
| download | redis-rust-846479e6bcf8238879546534b09e141b9bb668f8.tar.xz redis-rust-846479e6bcf8238879546534b09e141b9bb668f8.zip | |
feat: Added parsing of simple command line arguments and CONFIG GET command implementation
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index 6fb16b8..b29e2ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![allow(unused_imports)] use std::{ collections::HashMap, + env, io::{Read, Write}, net::{TcpListener, TcpStream}, sync::{Arc, Mutex}, @@ -8,9 +9,12 @@ use std::{ time::{Duration, SystemTime, UNIX_EPOCH}, }; -use codecrafters_redis::resp_commands::RedisCommands; -use codecrafters_redis::resp_parser::{parse, RespType}; use codecrafters_redis::shared_cache::*; +use codecrafters_redis::{resp_commands::RedisCommands, Config}; +use codecrafters_redis::{ + resp_parser::{parse, RespType}, + SharedConfig, +}; fn spawn_cleanup_thread(cache: SharedCache) { let cache_clone = cache.clone(); @@ -30,7 +34,7 @@ fn spawn_cleanup_thread(cache: SharedCache) { }); } -fn handle_client(mut stream: TcpStream, cache: SharedCache) { +fn handle_client(mut stream: TcpStream, cache: SharedCache, config: SharedConfig) { let mut buffer = [0; 512]; loop { @@ -41,7 +45,7 @@ fn handle_client(mut stream: TcpStream, cache: SharedCache) { }; let parsed_resp = parse(&buffer).unwrap(); - let response = RedisCommands::from(parsed_resp.0).execute(cache.clone()); + let response = RedisCommands::from(parsed_resp.0).execute(cache.clone(), config.clone()); // write respose back to the client stream.write(&response).unwrap(); @@ -51,15 +55,28 @@ fn handle_client(mut stream: TcpStream, cache: SharedCache) { fn main() -> std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:6379").unwrap(); let cache: SharedCache = Arc::new(Mutex::new(HashMap::new())); + let mut config: SharedConfig = None.into(); spawn_cleanup_thread(cache.clone()); + match Config::new() { + Ok(conf) => { + config = Arc::new(Some((conf))); + } + Err(e) => { + config = Arc::new(None); + eprintln!("Error: {}", e); + std::process::exit(1); + } + } + for stream in listener.incoming() { match stream { Ok(stream) => { let cache_clone = cache.clone(); + let config_clone = Arc::clone(&config); thread::spawn(|| { - handle_client(stream, cache_clone); + handle_client(stream, cache_clone, config_clone); }); } Err(e) => { |
