mirror of
https://github.com/actix/examples
synced 2025-06-28 09:50:36 +02:00
update deps
This commit is contained in:
@ -2,13 +2,15 @@
|
||||
name = "actix_redis"
|
||||
version = "0.1.0"
|
||||
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||
edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix = "0.7.3"
|
||||
actix-web = "0.7.3"
|
||||
actix-redis = "0.5.1"
|
||||
actix = "0.8.0-alpha.2"
|
||||
actix-web = "1.0.0-alpha.1"
|
||||
actix-redis = { git="https://github.com/actix/actix-redis.git" }
|
||||
futures = "0.1.23"
|
||||
redis-async = "0.4.0"
|
||||
serde = "1.0.71"
|
||||
serde_derive = "1.0.71"
|
||||
env_logger = "0.5.12"
|
||||
env_logger = "0.6"
|
||||
|
@ -1,23 +1,13 @@
|
||||
extern crate actix;
|
||||
extern crate actix_redis;
|
||||
extern crate actix_web;
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
#[macro_use]
|
||||
extern crate redis_async;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
use actix::prelude::*;
|
||||
use actix_redis::{Command, Error as ARError, RedisActor};
|
||||
use actix_web::{
|
||||
http::Method, middleware, server, App, AsyncResponder, Error as AWError,
|
||||
HttpRequest, HttpResponse, Json,
|
||||
};
|
||||
use actix_web::{middleware, web, App, Error as AWError, HttpResponse, HttpServer};
|
||||
use futures::future::{join_all, Future};
|
||||
use redis_async::resp::RespValue;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CacheInfo {
|
||||
@ -27,10 +17,10 @@ pub struct CacheInfo {
|
||||
}
|
||||
|
||||
fn cache_stuff(
|
||||
(info, req): (Json<CacheInfo>, HttpRequest<AppState>),
|
||||
info: web::Json<CacheInfo>,
|
||||
redis: web::Data<Addr<RedisActor>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = AWError> {
|
||||
let info = info.into_inner();
|
||||
let redis = req.state().redis_addr.clone();
|
||||
|
||||
let one = redis.send(Command(resp_array!["SET", "mydomain:one", info.one]));
|
||||
let two = redis.send(Command(resp_array!["SET", "mydomain:two", info.two]));
|
||||
@ -46,26 +36,23 @@ fn cache_stuff(
|
||||
let info_set = join_all(vec![one, two, three].into_iter());
|
||||
|
||||
info_set
|
||||
.map_err(AWError::from)
|
||||
.and_then(|res: Vec<Result<RespValue, ARError>>|
|
||||
// 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"))
|
||||
}
|
||||
)
|
||||
.responder()
|
||||
.map_err(AWError::from)
|
||||
.and_then(|res: Vec<Result<RespValue, ARError>>|
|
||||
// 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"))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn del_stuff(
|
||||
req: HttpRequest<AppState>,
|
||||
redis: web::Data<Addr<RedisActor>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = AWError> {
|
||||
let redis = req.state().redis_addr.clone();
|
||||
|
||||
redis
|
||||
.send(Command(resp_array![
|
||||
"DEL",
|
||||
@ -83,33 +70,24 @@ fn del_stuff(
|
||||
Ok(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
})
|
||||
.responder()
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub redis_addr: Arc<Addr<RedisActor>>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
::std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info");
|
||||
fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info");
|
||||
env_logger::init();
|
||||
let sys = actix::System::new("actix_redis_ex");
|
||||
|
||||
server::new(|| {
|
||||
let redis_addr = Arc::new(RedisActor::start("127.0.0.1:6379"));
|
||||
let app_state = AppState { redis_addr };
|
||||
HttpServer::new(|| {
|
||||
let redis_addr = RedisActor::start("127.0.0.1:6379");
|
||||
|
||||
App::with_state(app_state)
|
||||
.middleware(middleware::Logger::default())
|
||||
.resource("/stuff", |r| {
|
||||
r.method(Method::POST).with_async(cache_stuff);
|
||||
r.method(Method::DELETE).with_async(del_stuff)
|
||||
})
|
||||
App::new()
|
||||
.data(redis_addr)
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(
|
||||
web::resource("/stuff")
|
||||
.route(web::post().to_async(cache_stuff))
|
||||
.route(web::delete().to_async(del_stuff)),
|
||||
)
|
||||
})
|
||||
.bind("0.0.0.0:8080")
|
||||
.unwrap()
|
||||
.workers(1)
|
||||
.start();
|
||||
|
||||
let _ = sys.run();
|
||||
.bind("0.0.0.0:8080")?
|
||||
.run()
|
||||
}
|
||||
|
Reference in New Issue
Block a user