mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
upgrade to actix/actix-web 0.7
This commit is contained in:
parent
93707884d2
commit
62c382d280
13
Cargo.toml
13
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-redis"
|
||||
version = "0.4.0"
|
||||
version = "0.5.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Redis integration for actix framework"
|
||||
license = "MIT/Apache-2.0"
|
||||
@ -27,21 +27,22 @@ default = ["web"]
|
||||
web = ["actix-web", "cookie", "http", "rand", "serde", "serde_json"]
|
||||
|
||||
[dependencies]
|
||||
actix = "0.5"
|
||||
actix = "0.7"
|
||||
|
||||
log = "0.4"
|
||||
backoff = "0.1"
|
||||
failure = "^0.1.1"
|
||||
futures = "0.1"
|
||||
tokio-io = "0.1"
|
||||
tokio-core = "0.1"
|
||||
redis-async = "0.0"
|
||||
tokio-codec = "0.1"
|
||||
tokio-tcp = "0.1"
|
||||
redis-async = "0.3.2"
|
||||
|
||||
# actix web session
|
||||
actix-web = { version="0.6", optional=true }
|
||||
actix-web = { git = "https://github.com/actix/actix-web.git", optional=true }
|
||||
cookie = { version="0.10", features=["percent-encode", "secure"], optional=true }
|
||||
http = { version="0.1", optional=true }
|
||||
rand = { version="0.3", optional=true }
|
||||
rand = { version="0.5", optional=true }
|
||||
serde = { version="1.0", optional=true }
|
||||
serde_json = { version="1.0", optional=true }
|
||||
|
||||
|
@ -12,7 +12,7 @@ use actix_web::middleware::session::{self, RequestSession};
|
||||
use actix_web::{middleware, server, App, HttpRequest, HttpResponse, Result};
|
||||
|
||||
/// simple handler
|
||||
fn index(req: HttpRequest) -> Result<HttpResponse> {
|
||||
fn index(req: &HttpRequest) -> Result<HttpResponse> {
|
||||
println!("{:?}", req);
|
||||
|
||||
// session
|
||||
|
@ -5,13 +5,14 @@
|
||||
//! * [API Documentation (Releases)](https://docs.rs/actix-redis/)
|
||||
//! * [Chat on gitter](https://gitter.im/actix/actix)
|
||||
//! * Cargo package: [actix-redis](https://crates.io/crates/actix-redis)
|
||||
//! * Minimum supported Rust version: 1.21 or later
|
||||
//! * Minimum supported Rust version: 1.26 or later
|
||||
//!
|
||||
extern crate actix;
|
||||
extern crate backoff;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_codec;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_tcp;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
#[macro_use]
|
||||
@ -53,9 +54,6 @@ pub enum Error {
|
||||
Disconnected,
|
||||
}
|
||||
|
||||
unsafe impl Send for Error {}
|
||||
unsafe impl Sync for Error {}
|
||||
|
||||
impl From<redis_async::error::Error> for Error {
|
||||
fn from(err: redis_async::error::Error) -> Error {
|
||||
Error::Redis(err)
|
||||
|
22
src/redis.rs
22
src/redis.rs
@ -1,7 +1,7 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::io;
|
||||
|
||||
use actix::actors::{Connect, Connector};
|
||||
use actix::actors::resolver::{Connect, Resolver};
|
||||
use actix::prelude::*;
|
||||
use backoff::backoff::Backoff;
|
||||
use backoff::ExponentialBackoff;
|
||||
@ -9,10 +9,10 @@ use futures::unsync::oneshot;
|
||||
use futures::Future;
|
||||
use redis_async::error::Error as RespError;
|
||||
use redis_async::resp::{RespCodec, RespValue};
|
||||
use tokio_core::net::TcpStream;
|
||||
use tokio_io::codec::FramedRead;
|
||||
use tokio_codec::FramedRead;
|
||||
use tokio_io::io::WriteHalf;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_tcp::TcpStream;
|
||||
|
||||
use Error;
|
||||
|
||||
@ -34,11 +34,11 @@ pub struct RedisActor {
|
||||
|
||||
impl RedisActor {
|
||||
/// Start new `Supervisor` with `RedisActor`.
|
||||
pub fn start<S: Into<String>>(addr: S) -> Addr<Unsync, RedisActor> {
|
||||
pub fn start<S: Into<String>>(addr: S) -> Addr<RedisActor> {
|
||||
let addr = addr.into();
|
||||
|
||||
Supervisor::start(|_| RedisActor {
|
||||
addr: addr,
|
||||
addr,
|
||||
cell: None,
|
||||
backoff: ExponentialBackoff::default(),
|
||||
queue: VecDeque::new(),
|
||||
@ -50,7 +50,7 @@ impl Actor for RedisActor {
|
||||
type Context = Context<Self>;
|
||||
|
||||
fn started(&mut self, ctx: &mut Context<Self>) {
|
||||
Connector::from_registry()
|
||||
Resolver::from_registry()
|
||||
.send(Connect::host(self.addr.as_str()))
|
||||
.into_actor(self)
|
||||
.map(|res, act, ctx| match res {
|
||||
@ -104,10 +104,7 @@ impl Supervised for RedisActor {
|
||||
|
||||
impl actix::io::WriteHandler<io::Error> for RedisActor {
|
||||
fn error(&mut self, err: io::Error, _: &mut Self::Context) -> Running {
|
||||
warn!(
|
||||
"Redis connection dropped: {} error: {}",
|
||||
self.addr, err
|
||||
);
|
||||
warn!("Redis connection dropped: {} error: {}", self.addr, err);
|
||||
Running::Stop
|
||||
}
|
||||
}
|
||||
@ -139,9 +136,6 @@ impl Handler<Command> for RedisActor {
|
||||
let _ = tx.send(Err(Error::NotConnected));
|
||||
}
|
||||
|
||||
Box::new(
|
||||
rx.map_err(|_| Error::Disconnected)
|
||||
.and_then(|res| res),
|
||||
)
|
||||
Box::new(rx.map_err(|_| Error::Disconnected).and_then(|res| res))
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::iter::FromIterator;
|
||||
use std::iter;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix::prelude::*;
|
||||
@ -10,6 +10,7 @@ use cookie::{Cookie, CookieJar, Key};
|
||||
use futures::future::{err as FutErr, ok as FutOk, Either};
|
||||
use futures::Future;
|
||||
use http::header::{self, HeaderValue};
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::{self, Rng};
|
||||
use redis_async::resp::RespValue;
|
||||
use serde_json;
|
||||
@ -105,15 +106,15 @@ impl<S> SessionBackend<S> for RedisSessionBackend {
|
||||
Box::new(self.0.load(req).map(move |state| {
|
||||
if let Some((state, value)) = state {
|
||||
RedisSession {
|
||||
inner,
|
||||
state,
|
||||
changed: false,
|
||||
inner: inner,
|
||||
state: state,
|
||||
value: Some(value),
|
||||
}
|
||||
} else {
|
||||
RedisSession {
|
||||
inner,
|
||||
changed: false,
|
||||
inner: inner,
|
||||
state: HashMap::new(),
|
||||
value: None,
|
||||
}
|
||||
@ -126,7 +127,7 @@ struct Inner {
|
||||
key: Key,
|
||||
ttl: String,
|
||||
name: String,
|
||||
addr: Addr<Unsync, RedisActor>,
|
||||
addr: Addr<RedisActor>,
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
@ -136,7 +137,7 @@ impl Inner {
|
||||
) -> Box<Future<Item = Option<(HashMap<String, String>, String)>, Error = Error>>
|
||||
{
|
||||
if let Ok(cookies) = req.cookies() {
|
||||
for cookie in cookies {
|
||||
for cookie in cookies.iter() {
|
||||
if cookie.name() == self.name {
|
||||
let mut jar = CookieJar::new();
|
||||
jar.add_original(cookie.clone());
|
||||
@ -151,8 +152,7 @@ impl Inner {
|
||||
match val {
|
||||
RespValue::Error(err) => {
|
||||
return Err(
|
||||
error::ErrorInternalServerError(err)
|
||||
.into(),
|
||||
error::ErrorInternalServerError(err),
|
||||
)
|
||||
}
|
||||
RespValue::SimpleString(s) => {
|
||||
@ -173,7 +173,7 @@ impl Inner {
|
||||
Ok(None)
|
||||
}
|
||||
Err(err) => {
|
||||
Err(error::ErrorInternalServerError(err).into())
|
||||
Err(error::ErrorInternalServerError(err))
|
||||
}
|
||||
}),
|
||||
);
|
||||
@ -194,7 +194,10 @@ impl Inner {
|
||||
(value.clone(), None)
|
||||
} else {
|
||||
let mut rng = rand::OsRng::new().unwrap();
|
||||
let value = String::from_iter(rng.gen_ascii_chars().take(32));
|
||||
let value: String = iter::repeat(())
|
||||
.map(|()| rng.sample(Alphanumeric))
|
||||
.take(32)
|
||||
.collect();
|
||||
|
||||
let mut cookie = Cookie::new(self.name.clone(), value.clone());
|
||||
cookie.set_path("/");
|
||||
@ -211,9 +214,7 @@ impl Inner {
|
||||
Err(e) => Either::A(FutErr(e.into())),
|
||||
Ok(body) => Either::B(
|
||||
self.addr
|
||||
.send(Command(resp_array![
|
||||
"SET", value, body, "EX", &self.ttl
|
||||
]))
|
||||
.send(Command(resp_array!["SET", value, body, "EX", &self.ttl]))
|
||||
.map_err(Error::from)
|
||||
.and_then(move |res| match res {
|
||||
Ok(_) => {
|
||||
@ -226,7 +227,7 @@ impl Inner {
|
||||
}
|
||||
Ok(resp)
|
||||
}
|
||||
Err(err) => Err(error::ErrorInternalServerError(err).into()),
|
||||
Err(err) => Err(error::ErrorInternalServerError(err)),
|
||||
}),
|
||||
),
|
||||
})
|
||||
|
@ -16,16 +16,15 @@ fn test_error_connect() {
|
||||
let addr = RedisActor::start("localhost:54000");
|
||||
let _addr2 = addr.clone();
|
||||
|
||||
Arbiter::handle().spawn_fn(move || {
|
||||
addr.send(Command(resp_array!["GET", "test"]))
|
||||
.then(|res| {
|
||||
match res {
|
||||
Ok(Err(Error::NotConnected)) => (),
|
||||
_ => panic!("Should not happen {:?}", res),
|
||||
}
|
||||
Arbiter::system().do_send(actix::msgs::SystemExit(0));
|
||||
Ok(())
|
||||
})
|
||||
Arbiter::spawn_fn(move || {
|
||||
addr.send(Command(resp_array!["GET", "test"])).then(|res| {
|
||||
match res {
|
||||
Ok(Err(Error::NotConnected)) => (),
|
||||
_ => panic!("Should not happen {:?}", res),
|
||||
}
|
||||
System::current().stop();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
sys.run();
|
||||
@ -39,28 +38,26 @@ fn test_redis() {
|
||||
let addr = RedisActor::start("127.0.0.1:6379");
|
||||
let _addr2 = addr.clone();
|
||||
|
||||
Arbiter::handle().spawn_fn(move || {
|
||||
Arbiter::spawn_fn(move || {
|
||||
let addr2 = addr.clone();
|
||||
addr.send(Command(resp_array!["SET", "test", "value"]))
|
||||
.then(move |res| match res {
|
||||
Ok(Ok(resp)) => {
|
||||
assert_eq!(resp, RespValue::SimpleString("OK".to_owned()));
|
||||
addr2
|
||||
.send(Command(resp_array!["GET", "test"]))
|
||||
.then(|res| {
|
||||
match res {
|
||||
Ok(Ok(resp)) => {
|
||||
println!("RESP: {:?}", resp);
|
||||
assert_eq!(
|
||||
resp,
|
||||
RespValue::BulkString((&b"value"[..]).into())
|
||||
);
|
||||
}
|
||||
_ => panic!("Should not happen {:?}", res),
|
||||
addr2.send(Command(resp_array!["GET", "test"])).then(|res| {
|
||||
match res {
|
||||
Ok(Ok(resp)) => {
|
||||
println!("RESP: {:?}", resp);
|
||||
assert_eq!(
|
||||
resp,
|
||||
RespValue::BulkString((&b"value"[..]).into())
|
||||
);
|
||||
}
|
||||
Arbiter::system().do_send(actix::msgs::SystemExit(0));
|
||||
Ok(())
|
||||
})
|
||||
_ => panic!("Should not happen {:?}", res),
|
||||
}
|
||||
System::current().stop();
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
_ => panic!("Should not happen {:?}", res),
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user