2018-08-11 06:58:29 -04:00
|
|
|
use actix::prelude::*;
|
2019-12-16 11:23:36 +06:00
|
|
|
use actix_redis::{Command, RedisActor};
|
2022-02-15 00:35:00 +00:00
|
|
|
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>>,
|
2022-02-15 00:35:00 +00:00
|
|
|
) -> 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]));
|
|
|
|
|
2022-02-15 00:35:00 +00:00
|
|
|
// 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
|
2022-02-15 00:35:00 +00:00
|
|
|
if res
|
2021-10-07 03:04:59 +01:00
|
|
|
.iter()
|
2022-02-15 00:35:00 +00:00
|
|
|
.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"))
|
2022-02-15 00:35:00 +00:00
|
|
|
} 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"
|
|
|
|
]))
|
2022-02-15 00:35:00 +00:00
|
|
|
.await
|
|
|
|
.map_err(error::ErrorInternalServerError)?
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
2019-12-16 11:23:36 +06:00
|
|
|
|
|
|
|
match res {
|
2022-02-15 00:35:00 +00:00
|
|
|
RespValue::Integer(x) if x == 3 => {
|
2019-12-16 11:23:36 +06:00
|
|
|
Ok(HttpResponse::Ok().body("successfully deleted values"))
|
|
|
|
}
|
2022-02-15 00:35:00 +00:00
|
|
|
|
2019-12-16 11:23:36 +06:00
|
|
|
_ => {
|
2022-06-07 22:53:38 -04: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<()> {
|
2022-02-15 00:35:00 +00:00
|
|
|
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()
|
2022-02-15 00:35:00 +00:00
|
|
|
.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
|
|
|
})
|
2022-02-15 00:35:00 +00: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
|
|
|
}
|