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

82 lines
2.4 KiB
Rust
Raw Normal View History

2022-10-16 19:14:40 +01:00
use std::io;
use actix_web::{error, middleware, web, App, HttpResponse, HttpServer, Responder};
2020-09-12 16:49:45 +01:00
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(
2022-10-16 19:14:40 +01:00
web::Json(info): web::Json<CacheInfo>,
redis: web::Data<redis::Client>,
) -> actix_web::Result<impl Responder> {
let mut conn = redis
.get_tokio_connection_manager()
.await
2022-10-16 19:14:40 +01:00
.map_err(error::ErrorInternalServerError)?;
2023-07-09 00:56:56 +01:00
let res = redis::Cmd::mset(&[
2022-10-16 19:14:40 +01:00
("my_domain:one", info.one),
("my_domain:two", info.two),
("my_domain:three", info.three),
])
.query_async::<_, String>(&mut conn)
.await
.map_err(error::ErrorInternalServerError)?;
2018-08-11 06:58:29 -04:00
2022-10-16 19:14:40 +01:00
// not strictly necessary, but successful SET operations return "OK"
if res == "OK" {
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-10-16 19:14:40 +01:00
async fn del_stuff(redis: web::Data<redis::Client>) -> actix_web::Result<impl Responder> {
let mut conn = redis
.get_tokio_connection_manager()
.await
.map_err(error::ErrorInternalServerError)?;
2019-12-16 11:23:36 +06:00
2022-10-16 19:14:40 +01:00
let res = redis::Cmd::del(&["my_domain:one", "my_domain:two", "my_domain:three"])
.query_async::<_, usize>(&mut conn)
.await
.map_err(error::ErrorInternalServerError)?;
2022-10-16 19:14:40 +01:00
// not strictly necessary, but successful DEL operations return the number of keys deleted
if res == 3 {
Ok(HttpResponse::Ok().body("successfully deleted values"))
} else {
log::error!("deleted {res} keys");
Ok(HttpResponse::InternalServerError().finish())
2019-12-16 11:23:36 +06:00
}
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]
2022-10-16 19:14:40 +01:00
async fn main() -> 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
2022-10-16 19:14:40 +01:00
let redis = redis::Client::open("redis://127.0.0.1:6379").unwrap();
2018-08-11 06:58:29 -04:00
2022-10-16 19:14:40 +01:00
HttpServer::new(move || {
2019-03-29 13:43:03 -07:00
App::new()
2022-10-16 19:14:40 +01:00
.app_data(web::Data::new(redis.clone()))
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-10-16 19:14:40 +01:00
.workers(2)
.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
}