1
0
mirror of https://github.com/actix/examples synced 2025-02-02 17:39:05 +01:00

95 lines
3.1 KiB
Rust
Raw Normal View History

2018-08-11 06:58:29 -04:00
use actix::prelude::*;
2019-12-16 11:23:36 +06:00
use actix_redis::{Command, RedisActor};
use actix_web::{error, middleware, web, App, HttpResponse, HttpServer};
use futures_util::future::try_join_all;
2020-09-12 16:49:45 +01:00
use redis_async::{resp::RespValue, resp_array};
use serde::Deserialize;
2018-08-11 06:58:29 -04:00
#[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>>,
) -> actix_web::Result<HttpResponse> {
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]));
// Asynchronously collects 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.
let res = try_join_all([one, two, three])
.await
.map_err(error::ErrorInternalServerError)?
.into_iter()
.map(|item| item.map_err(error::ErrorInternalServerError))
.collect::<Result<Vec<_>, _>>()?;
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
2021-10-07 03:04:59 +01:00
.iter()
.all(|res| matches!(res, RespValue::SimpleString(x) if x == "OK"))
2021-10-07 03:04:59 +01:00
{
2019-12-16 11:23:36 +06:00
Ok(HttpResponse::Ok().body("successfully cached values"))
} else {
Ok(HttpResponse::InternalServerError().finish())
2019-12-16 11:23:36 +06:00
}
2018-08-11 06:58:29 -04:00
}
2022-02-18 02:44:02 +00:00
async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> actix_web::Result<HttpResponse> {
2019-12-16 11:23:36 +06:00
let res = redis
2019-03-09 18:03:09 -08:00
.send(Command(resp_array![
"DEL",
"mydomain:one",
"mydomain:two",
"mydomain:three"
]))
.await
.map_err(error::ErrorInternalServerError)?
.map_err(error::ErrorInternalServerError)?;
2019-12-16 11:23:36 +06:00
match res {
RespValue::Integer(x) if x == 3 => {
2019-12-16 11:23:36 +06:00
Ok(HttpResponse::Ok().body("successfully deleted values"))
}
2019-12-16 11:23:36 +06:00
_ => {
log::error!("{:?}", res);
2019-12-16 11:23:36 +06:00
Ok(HttpResponse::InternalServerError().finish())
}
}
2018-08-11 09:59:07 -04:00
}
2018-08-11 06:58:29 -04:00
2020-09-12 16:49:45 +01:00
#[actix_web::main]
2019-12-16 11:23:36 +06:00
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
2018-08-11 06:58:29 -04:00
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()
.app_data(web::Data::new(redis_addr))
2019-03-29 13:43:03 -07:00
.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
})
.bind(("127.0.0.1", 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
}