1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

better ergonomics for Server::service() method

This commit is contained in:
Nikolay Kim 2018-08-09 13:38:10 -07:00
parent 2e8d67e2ae
commit 2ab7dbadce
2 changed files with 16 additions and 14 deletions

View File

@ -427,14 +427,19 @@ where
} }
} }
impl<H: IntoHttpHandler> Into<Box<Service>> for HttpServer<H> { impl<H: IntoHttpHandler> Into<(Box<Service>, Vec<(Token, net::TcpListener)>)> for HttpServer<H> {
fn into(self) -> Box<Service> { fn into(mut self) -> (Box<Service>, Vec<(Token, net::TcpListener)>) {
Box::new(HttpService { let sockets: Vec<_> = mem::replace(&mut self.sockets, Vec::new())
.into_iter()
.map(|item| (item.token, item.lst))
.collect();
(Box::new(HttpService {
factory: self.factory, factory: self.factory,
host: self.host, host: self.host,
keep_alive: self.keep_alive, keep_alive: self.keep_alive,
handlers: self.handlers, handlers: self.handlers,
}) }), sockets)
} }
} }
@ -500,7 +505,7 @@ impl<H: IntoHttpHandler> HttpServer<H> {
/// sys.run(); // <- Run actix system, this method starts all async processes /// sys.run(); // <- Run actix system, this method starts all async processes
/// } /// }
/// ``` /// ```
pub fn start(mut self) -> Addr<Server> { pub fn start(self) -> Addr<Server> {
let mut srv = Server::new() let mut srv = Server::new()
.workers(self.threads) .workers(self.threads)
.maxconn(self.maxconn) .maxconn(self.maxconn)
@ -514,11 +519,7 @@ impl<H: IntoHttpHandler> HttpServer<H> {
srv srv
}; };
let sockets: Vec<_> = mem::replace(&mut self.sockets, Vec::new()) srv.service(self).start()
.into_iter()
.map(|item| (item.token, item.lst))
.collect();
srv.service(self, sockets).start()
} }
/// Spawn new thread and start listening for incoming connections. /// Spawn new thread and start listening for incoming connections.

View File

@ -145,11 +145,12 @@ impl Server {
} }
/// Add new service to server /// Add new service to server
pub fn service<T>(mut self, srv: T, sockets: Vec<(Token, net::TcpListener)>) -> Self pub fn service<T>(mut self, srv: T) -> Self
where where
T: Into<Box<Service>> T: Into<(Box<Service>, Vec<(Token, net::TcpListener)>)>
{ {
self.services.push(srv.into()); let (srv, sockets) = srv.into();
self.services.push(srv);
self.sockets.push(sockets); self.sockets.push(sockets);
self self
} }