1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

impl HttpHandler for Box<HttpHandler> and add helper method Application::boxed() #49

This commit is contained in:
Nikolay Kim 2018-01-13 18:58:17 -08:00
parent 2b0f3d2a9a
commit 927a92fcac
5 changed files with 52 additions and 5 deletions

View File

@ -1,5 +1,10 @@
# Changes # Changes
## 0.3.2 (2018-01-xx)
* Can't have multiple Applications on a single server with different state #49
## 0.3.1 (2018-01-13) ## 0.3.1 (2018-01-13)
* Fix directory entry path #47 * Fix directory entry path #47

View File

@ -8,4 +8,4 @@ workspace = "../.."
futures = "*" futures = "*"
env_logger = "0.4" env_logger = "0.4"
actix = "0.4" actix = "0.4"
actix-web = { git = "https://github.com/actix/actix-web" } actix-web = { path = "../../" }

View File

@ -33,7 +33,7 @@ struct MyWebSocket {
} }
impl Actor for MyWebSocket { impl Actor for MyWebSocket {
type Context = HttpContext<Self, AppState>; type Context = ws::WebsocketContext<Self, AppState>;
} }
impl Handler<ws::Message> for MyWebSocket { impl Handler<ws::Message> for MyWebSocket {
@ -43,9 +43,9 @@ impl Handler<ws::Message> for MyWebSocket {
self.counter += 1; self.counter += 1;
println!("WS({}): {:?}", self.counter, msg); println!("WS({}): {:?}", self.counter, msg);
match msg { match msg {
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg), ws::Message::Ping(msg) => ctx.pong(&msg),
ws::Message::Text(text) => ws::WsWriter::text(ctx, &text), ws::Message::Text(text) => ctx.text(&text),
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), ws::Message::Binary(bin) => ctx.binary(bin),
ws::Message::Closed | ws::Message::Error => { ws::Message::Closed | ws::Message::Error => {
ctx.stop(); ctx.stop();
} }

View File

@ -59,9 +59,11 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
#[cfg(test)] #[cfg(test)]
impl<S: 'static> HttpApplication<S> { impl<S: 'static> HttpApplication<S> {
#[cfg(test)]
pub(crate) fn run(&mut self, req: HttpRequest<S>) -> Reply { pub(crate) fn run(&mut self, req: HttpRequest<S>) -> Reply {
self.inner.borrow_mut().handle(req) self.inner.borrow_mut().handle(req)
} }
#[cfg(test)]
pub(crate) fn prepare_request(&self, req: HttpRequest) -> HttpRequest<S> { pub(crate) fn prepare_request(&self, req: HttpRequest) -> HttpRequest<S> {
req.with_state(Rc::clone(&self.state), self.router.clone()) req.with_state(Rc::clone(&self.state), self.router.clone())
} }
@ -356,6 +358,40 @@ impl<S> Application<S> where S: 'static {
middlewares: Rc::new(parts.middlewares), middlewares: Rc::new(parts.middlewares),
} }
} }
/// Convenience method for creating `Box<HttpHandler>` instance.
///
/// This method is useful if you need to register several application instances
/// with different state.
///
/// ```rust
/// # use std::thread;
/// # extern crate actix_web;
/// use actix_web::*;
///
/// struct State1;
///
/// struct State2;
///
/// fn main() {
/// # thread::spawn(|| {
/// HttpServer::new(|| { vec![
/// Application::with_state(State1)
/// .prefix("/app1")
/// .resource("/", |r| r.h(httpcodes::HTTPOk))
/// .boxed(),
/// Application::with_state(State2)
/// .prefix("/app2")
/// .resource("/", |r| r.h(httpcodes::HTTPOk))
/// .boxed() ]})
/// .bind("127.0.0.1:8080").unwrap()
/// .run()
/// # });
/// }
/// ```
pub fn boxed(mut self) -> Box<HttpHandler> {
Box::new(self.finish())
}
} }
impl<S: 'static> IntoHttpHandler for Application<S> { impl<S: 'static> IntoHttpHandler for Application<S> {

View File

@ -54,6 +54,12 @@ pub trait HttpHandler: 'static {
fn handle(&mut self, req: HttpRequest) -> Result<Box<HttpHandlerTask>, HttpRequest>; fn handle(&mut self, req: HttpRequest) -> Result<Box<HttpHandlerTask>, HttpRequest>;
} }
impl HttpHandler for Box<HttpHandler> {
fn handle(&mut self, req: HttpRequest) -> Result<Box<HttpHandlerTask>, HttpRequest> {
self.as_mut().handle(req)
}
}
pub trait HttpHandlerTask { pub trait HttpHandlerTask {
fn poll(&mut self) -> Poll<(), Error>; fn poll(&mut self) -> Poll<(), Error>;