aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs27
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) => {