mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 23:08:40 +08:00
AS
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -70,4 +70,4 @@ crashlytics-build.properties
|
|||||||
#
|
#
|
||||||
# already existing elements were commented out
|
# already existing elements were commented out
|
||||||
|
|
||||||
#/target
|
/z_shpo
|
||||||
|
|||||||
1008
Cargo.lock
generated
Normal file
1008
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,3 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bytes = "1.9.0"
|
||||||
|
tokio = "1.43.0"
|
||||||
|
mini-redis = "0.4"
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
mod web;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
web::run::run();
|
||||||
}
|
}
|
||||||
|
|||||||
1
src/web.rs
Normal file
1
src/web.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod run;
|
||||||
46
src/web/run.rs
Normal file
46
src/web/run.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user