This commit is contained in:
Ara Sadoyan
2025-01-20 14:49:02 +01:00
parent e2c8458e6e
commit 320348c85f
7 changed files with 1067 additions and 2 deletions

2
.gitignore vendored
View File

@@ -70,4 +70,4 @@ crashlytics-build.properties
#
# already existing elements were commented out
#/target
/z_shpo

1008
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -4,3 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
bytes = "1.9.0"
tokio = "1.43.0"
mini-redis = "0.4"

View File

@@ -1,3 +1,5 @@
mod web;
fn main() {
println!("Hello, world!");
web::run::run();
}

1
src/web.rs Normal file
View File

@@ -0,0 +1 @@
pub mod run;

46
src/web/run.rs Normal file
View File

@@ -0,0 +1,46 @@
use bytes::Bytes;
use mini_redis::{Connection, Frame};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::net::{TcpListener, TcpStream};
type Db = Arc<Mutex<HashMap<String, Bytes>>>;
#[tokio::main]
pub async fn run() {
println!("\n= = = = = = = = ASYNC TOKIO = = = = = = = = =\n");
let listener = TcpListener::bind("0.0.0.0:6379").await.unwrap();
println!("Server is running on 0.0.0.0:6379 !\n");
let db = Arc::new(Mutex::new(HashMap::new()));
loop {
let (socket, _) = listener.accept().await.unwrap();
let db = db.clone();
tokio::spawn(async move {
process(socket, db).await;
});
}
}
async fn process(socket: TcpStream, db: Db) {
use mini_redis::Command::{self, Get, Set};
let mut connection = Connection::new(socket);
while let Some(frame) = connection.read_frame().await.unwrap() {
let response = match Command::from_frame(frame).unwrap() {
Set(cmd) => {
// println!("{:?}", Command::from_frame(frame).unwrap());
let mut db = db.lock().unwrap();
db.insert(cmd.key().to_string(), cmd.value().clone());
Frame::Simple("OK".to_string())
}
Get(cmd) => {
let db = db.lock().unwrap();
if let Some(value) = db.get(cmd.key()) {
println!("{:?}", db);
Frame::Bulk(value.clone())
} else {
Frame::Null
}
}
cmd => panic!("unimplemented {:?}", cmd),
};
connection.write_frame(&response).await.unwrap();
}
}

4
start.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
reflex -d none -r 'src/' -s -- sh -c 'reset && cargo run livelong poem.txt'