diff --git a/CHANGES.md b/CHANGES.md index 896d4f4d..695ac150 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # 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) * Fix directory entry path #47 diff --git a/examples/state/Cargo.toml b/examples/state/Cargo.toml index 149e8128..b92b0082 100644 --- a/examples/state/Cargo.toml +++ b/examples/state/Cargo.toml @@ -8,4 +8,4 @@ workspace = "../.." futures = "*" env_logger = "0.4" actix = "0.4" -actix-web = { git = "https://github.com/actix/actix-web" } \ No newline at end of file +actix-web = { path = "../../" } diff --git a/examples/state/src/main.rs b/examples/state/src/main.rs index 395007ae..8216be24 100644 --- a/examples/state/src/main.rs +++ b/examples/state/src/main.rs @@ -33,7 +33,7 @@ struct MyWebSocket { } impl Actor for MyWebSocket { - type Context = HttpContext; + type Context = ws::WebsocketContext; } impl Handler for MyWebSocket { @@ -43,9 +43,9 @@ impl Handler for MyWebSocket { self.counter += 1; println!("WS({}): {:?}", self.counter, msg); match msg { - ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg), - ws::Message::Text(text) => ws::WsWriter::text(ctx, &text), - ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), + ws::Message::Ping(msg) => ctx.pong(&msg), + ws::Message::Text(text) => ctx.text(&text), + ws::Message::Binary(bin) => ctx.binary(bin), ws::Message::Closed | ws::Message::Error => { ctx.stop(); } diff --git a/src/application.rs b/src/application.rs index f7b93e1f..ecd65c6e 100644 --- a/src/application.rs +++ b/src/application.rs @@ -59,9 +59,11 @@ impl PipelineHandler for Inner { #[cfg(test)] impl HttpApplication { + #[cfg(test)] pub(crate) fn run(&mut self, req: HttpRequest) -> Reply { self.inner.borrow_mut().handle(req) } + #[cfg(test)] pub(crate) fn prepare_request(&self, req: HttpRequest) -> HttpRequest { req.with_state(Rc::clone(&self.state), self.router.clone()) } @@ -356,6 +358,40 @@ impl Application where S: 'static { middlewares: Rc::new(parts.middlewares), } } + + /// Convenience method for creating `Box` 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 { + Box::new(self.finish()) + } } impl IntoHttpHandler for Application { diff --git a/src/server/mod.rs b/src/server/mod.rs index c0a04753..56671207 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -54,6 +54,12 @@ pub trait HttpHandler: 'static { fn handle(&mut self, req: HttpRequest) -> Result, HttpRequest>; } +impl HttpHandler for Box { + fn handle(&mut self, req: HttpRequest) -> Result, HttpRequest> { + self.as_mut().handle(req) + } +} + pub trait HttpHandlerTask { fn poll(&mut self) -> Poll<(), Error>;