Skip to content

Commit 463bc70

Browse files
committed
1/2 way through getting rocket async
1 parent c4e1887 commit 463bc70

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ edition = "2018"
88

99
[dependencies]
1010
rocket = "0.4.4"
11+
reqwest = { version = "0.10", features = ["json"] }
12+
tokio = { version = "0.3", features = ["full"] }
13+
mini-redis = "0.3"

src/main.rs

100644100755
+16-21
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
1+
#!/usr/bin/env run-cargo-script
2+
//! ```cargo
3+
//! [dependencies]
4+
//! rocket = "0.4.4"
5+
//! ```
16
#![feature(proc_macro_hygiene, decl_macro)]
27
#[macro_use] extern crate rocket;
38

49
use std::env;
10+
use mini_redis::{client, Result};
511
use rocket::http::RawStr;
6-
use rocket::State;
7-
use std::collections::HashMap;
8-
use std::sync::RwLock;
9-
10-
struct Store<> {
11-
store: RwLock<HashMap<String, String>>,
12-
}
1312

1413
#[get("/store/<id>")]
15-
fn retrieve(id: &RawStr, store: State<Store>) -> String {
16-
let _astring = "test";
17-
let hm = store.store.read().unwrap();
18-
format!("{}: {:?}", id, hm.get(&id.to_string()))
14+
async fn retrieve(id: &RawStr) -> Result<String> {
15+
let mut client = client::connect("127.0.0.1:6379").await?;
16+
let result = client.get(&id.to_string()).await?;
17+
Ok(format!("{}", id))
1918
}
2019

2120
#[get("/store/<id>/<val>")]
22-
fn set(id: &RawStr, val: &RawStr, store: State<Store>) -> String {
23-
let id_copy = id.to_string();
24-
let value = val.to_string();
21+
fn set(id: &RawStr, val: &RawStr) -> String {
22+
let value= val.to_string();
2523

26-
let mut hm = store.store.write().unwrap();
27-
hm.insert(id_copy, value);
28-
drop(hm);
29-
let hm_read = store.store.read().unwrap();
30-
format!("{}: {:?}", id, hm_read.get(&id.to_string()))
24+
format!("{}: {:?}", id, value)
3125
}
3226

33-
fn main() {
27+
#[tokio::main]
28+
async fn main() -> Result<()> {
3429
match env::var("PORT") {
3530
Ok(val) => env::set_var("ROCKET_PORT", val),
3631
Err(e) => println!("Error: {}", e),
3732
}
3833
rocket::ignite()
39-
.manage(Store { store: RwLock::new(HashMap::new())})
4034
.mount("/", routes![retrieve, set])
4135
.launch();
36+
Ok(())
4237
}

0 commit comments

Comments
 (0)