1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 18:09:22 +02:00

simplify middleware api; fix examples

This commit is contained in:
Nikolay Kim
2017-11-26 21:47:33 -08:00
parent 5a3b6638a7
commit fdafb0c848
11 changed files with 74 additions and 64 deletions

View File

@ -101,9 +101,9 @@ impl Logger {
impl Middleware for Logger {
fn start(&self, mut req: HttpRequest) -> Started {
fn start(&self, req: &mut HttpRequest) -> Started {
req.extensions().insert(StartTime(time::now()));
Started::Done(req)
Started::Done
}
fn finish(&self, req: &mut HttpRequest, resp: &HttpResponse) -> Finished {
@ -299,14 +299,14 @@ mod tests {
let mut headers = HeaderMap::new();
headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let req = HttpRequest::new(
let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty());
let resp = HttpResponse::builder(StatusCode::OK)
.header("X-Test", "ttt")
.force_close().body(Body::Empty).unwrap();
let mut req = match logger.start(req) {
Started::Done(req) => req,
match logger.start(&mut req) {
Started::Done => (),
_ => panic!(),
};
match logger.finish(&mut req, &resp) {

View File

@ -16,12 +16,12 @@ pub enum Started {
/// Moddleware error
Err(Error),
/// Execution completed
Done(HttpRequest),
Done,
/// New http response got generated. If middleware generates response
/// handler execution halts.
Response(HttpRequest, HttpResponse),
Response(HttpResponse),
/// Execution completed, runs future to completion.
Future(Box<Future<Item=(HttpRequest, Option<HttpResponse>), Error=Error>>),
Future(Box<Future<Item=Option<HttpResponse>, Error=Error>>),
}
/// Middleware execution result
@ -48,8 +48,8 @@ pub trait Middleware {
/// Method is called when request is ready. It may return
/// future, which should resolve before next middleware get called.
fn start(&self, req: HttpRequest) -> Started {
Started::Done(req)
fn start(&self, req: &mut HttpRequest) -> Started {
Started::Done
}
/// Method is called when handler returns response,

View File

@ -90,14 +90,15 @@ impl<T: SessionBackend> SessionStorage<T> {
impl<T: SessionBackend> Middleware for SessionStorage<T> {
fn start(&self, mut req: HttpRequest) -> Started {
fn start(&self, req: &mut HttpRequest) -> Started {
let mut req = req.clone();
let fut = self.0.from_request(&mut req)
.then(|res| {
.then(move |res| {
match res {
Ok(sess) => {
req.extensions().insert(Arc::new(SessionImplBox(Box::new(sess))));
let resp: Option<HttpResponse> = None;
FutOk((req, resp))
FutOk(None)
},
Err(err) => FutErr(err)
}