diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a699de2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "MiniRedis" +version = "0.0.1" +description = "Mini Redis" +repository = "https://github.com/GuilhermeWerner/MiniRedis" +authors = ["GuilhermeWerner "] +license = "MIT" +edition = "2018" +publish = false + +[[bin]] +name="Client" +path = "Source/Client.rs" + +[[bin]] +name="Server" +path = "Source/Server.rs" + +[dependencies] +tokio = { version = "1.0.2", features = ["full"] } +mini-redis = "0.4" + +[[example]] +name="Hello" +path = "Examples/Hello.rs" diff --git a/Examples/Hello.rs b/Examples/Hello.rs new file mode 100644 index 0000000..3fa16ce --- /dev/null +++ b/Examples/Hello.rs @@ -0,0 +1,17 @@ +use mini_redis::{client, Result}; + +#[tokio::main] +pub async fn main() -> Result<()> { + // Open a connection to the mini-redis address. + let mut client = client::connect("127.0.0.1:6379").await?; + + // Set the key "hello" with value "world" + client.set("hello", "world".into()).await?; + + // Get key "hello" + let result = client.get("hello").await?; + + println!("{:?}", result); + + Ok(()) +} diff --git a/Source/Client.rs b/Source/Client.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/Source/Client.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/Source/Server.rs b/Source/Server.rs new file mode 100644 index 0000000..6594520 --- /dev/null +++ b/Source/Server.rs @@ -0,0 +1,28 @@ +use mini_redis::{Connection, Frame}; +use tokio::net::{TcpListener, TcpStream}; + +#[tokio::main] +async fn main() { + // Bind the listener to the address + let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap(); + + loop { + // The second item contains the IP and port of the new connection. + let (socket, _) = listener.accept().await.unwrap(); + process(socket).await; + } +} + +async fn process(socket: TcpStream) { + // The `Connection` lets us read/write redis **frames** instead of + // byte streams. The `Connection` type is defined by mini-redis. + let mut connection = Connection::new(socket); + + if let Some(frame) = connection.read_frame().await.unwrap() { + println!("GOT: {:?}", frame); + + // Respond with an error + let response = Frame::Error("unimplemented".to_string()); + connection.write_frame(&response).await.unwrap(); + } +}