1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +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> {
fn into(self) -> Box<Service> {
Box::new(HttpService {
impl<H: IntoHttpHandler> Into<(Box<Service>, Vec<(Token, net::TcpListener)>)> for HttpServer<H> {
fn into(mut self) -> (Box<Service>, Vec<(Token, net::TcpListener)>) {
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,
host: self.host,
keep_alive: self.keep_alive,
handlers: self.handlers,
})
}), sockets)
}
}
@ -500,7 +505,7 @@ impl<H: IntoHttpHandler> HttpServer<H> {
/// 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()
.workers(self.threads)
.maxconn(self.maxconn)
@ -514,11 +519,7 @@ impl<H: IntoHttpHandler> HttpServer<H> {
srv
};
let sockets: Vec<_> = mem::replace(&mut self.sockets, Vec::new())
.into_iter()
.map(|item| (item.token, item.lst))
.collect();
srv.service(self, sockets).start()
srv.service(self).start()
}
/// Spawn new thread and start listening for incoming connections.

View File

@ -145,11 +145,12 @@ impl 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
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
}