1
0
mirror of https://github.com/actix/examples synced 2025-01-23 14:24:35 +01:00
examples/redis/src/main.rs

98 lines
3.1 KiB
Rust
Raw Normal View History

2019-03-09 18:03:09 -08:00
#[macro_use]
extern crate redis_async;
use serde::Deserialize;
2018-08-11 06:58:29 -04:00
use actix::prelude::*;
2019-12-16 11:23:36 +06:00
use actix_redis::{Command, RedisActor};
2019-03-29 13:43:03 -07:00
use actix_web::{middleware, web, App, Error as AWError, HttpResponse, HttpServer};
2019-12-16 11:23:36 +06:00
use futures::future::join_all;
2018-08-11 06:58:29 -04:00
use redis_async::resp::RespValue;
#[derive(Deserialize)]
pub struct CacheInfo {
one: String,
two: String,
2019-03-09 18:03:09 -08:00
three: String,
2018-08-11 06:58:29 -04:00
}
2019-12-16 11:23:36 +06:00
async fn cache_stuff(
2019-03-29 13:43:03 -07:00
info: web::Json<CacheInfo>,
redis: web::Data<Addr<RedisActor>>,
2019-12-16 11:23:36 +06:00
) -> Result<HttpResponse, AWError> {
2018-08-11 06:58:29 -04:00
let info = info.into_inner();
let one = redis.send(Command(resp_array!["SET", "mydomain:one", info.one]));
let two = redis.send(Command(resp_array!["SET", "mydomain:two", info.two]));
let three = redis.send(Command(resp_array!["SET", "mydomain:three", info.three]));
// Creates a future which represents a collection of the results of the futures
// given. The returned future will drive execution for all of its underlying futures,
// collecting the results into a destination `Vec<RespValue>` in the same order as they
// were provided. If any future returns an error then all other futures will be
// canceled and an error will be returned immediately. If all futures complete
// successfully, however, then the returned future will succeed with a `Vec` of
// all the successful results.
2019-12-16 11:23:36 +06:00
let res: Vec<Result<RespValue, AWError>> =
join_all(vec![one, two, three].into_iter())
.await
.into_iter()
.map(|item| {
item.map_err(AWError::from)
.and_then(|res| res.map_err(AWError::from))
})
.collect();
2018-08-11 06:58:29 -04:00
2019-12-16 11:23:36 +06:00
// successful operations return "OK", so confirm that all returned as so
if !res.iter().all(|res| match res {
Ok(RespValue::SimpleString(x)) if x == "OK" => true,
_ => false,
}) {
Ok(HttpResponse::InternalServerError().finish())
} else {
Ok(HttpResponse::Ok().body("successfully cached values"))
}
2018-08-11 06:58:29 -04:00
}
2019-12-16 11:23:36 +06:00
async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> Result<HttpResponse, AWError> {
let res = redis
2019-03-09 18:03:09 -08:00
.send(Command(resp_array![
"DEL",
"mydomain:one",
"mydomain:two",
"mydomain:three"
]))
2019-12-16 11:23:36 +06:00
.await?;
match res {
Ok(RespValue::Integer(x)) if x == 3 => {
Ok(HttpResponse::Ok().body("successfully deleted values"))
}
_ => {
println!("---->{:?}", res);
Ok(HttpResponse::InternalServerError().finish())
}
}
2018-08-11 09:59:07 -04:00
}
2018-08-11 06:58:29 -04:00
2019-12-16 11:23:36 +06:00
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=trace,actix_redis=trace");
2018-08-11 06:58:29 -04:00
env_logger::init();
2019-03-29 13:43:03 -07:00
HttpServer::new(|| {
let redis_addr = RedisActor::start("127.0.0.1:6379");
2018-08-11 06:58:29 -04:00
2019-03-29 13:43:03 -07:00
App::new()
.data(redis_addr)
.wrap(middleware::Logger::default())
.service(
web::resource("/stuff")
2019-12-16 11:23:36 +06:00
.route(web::post().to(cache_stuff))
.route(web::delete().to(del_stuff)),
2019-03-29 13:43:03 -07:00
)
2019-03-09 18:03:09 -08:00
})
2019-03-29 13:43:03 -07:00
.bind("0.0.0.0:8080")?
2019-12-25 20:48:33 +04:00
.run()
2019-12-16 11:23:36 +06:00
.await
2018-08-11 06:58:29 -04:00
}