From 13cbfc877d003d7203b09f63cf594e50b1b3ccb7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 19 Dec 2017 09:08:36 -0800 Subject: [PATCH] simplify server start method --- examples/basic.rs | 2 +- examples/diesel/src/main.rs | 2 +- examples/state.rs | 2 +- examples/template_tera/src/main.rs | 2 +- examples/websocket-chat/src/main.rs | 2 +- examples/websocket.rs | 2 +- guide/src/qs_3_5.md | 5 +++-- src/server.rs | 14 ++++++++------ tests/test_server.rs | 6 +++--- 9 files changed, 20 insertions(+), 17 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 4ceef9e13..8e8da3e87 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -93,7 +93,7 @@ fn main() { .body(Body::Empty) }))) .bind("127.0.0.1:8080").unwrap() - .start().unwrap(); + .start(); println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); diff --git a/examples/diesel/src/main.rs b/examples/diesel/src/main.rs index 15f0cc1bf..80eb30d29 100644 --- a/examples/diesel/src/main.rs +++ b/examples/diesel/src/main.rs @@ -105,7 +105,7 @@ fn main() { .middleware(middlewares::Logger::default()) .resource("/{name}", |r| r.method(Method::GET).a(index))}) .bind("127.0.0.1:8080").unwrap() - .start().unwrap(); + .start(); println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); diff --git a/examples/state.rs b/examples/state.rs index c36ac19d2..0a10b77bd 100644 --- a/examples/state.rs +++ b/examples/state.rs @@ -71,7 +71,7 @@ fn main() { // register simple handler, handle all methods .resource("/", |r| r.f(index))) .bind("127.0.0.1:8080").unwrap() - .start().unwrap(); + .start(); println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); diff --git a/examples/template_tera/src/main.rs b/examples/template_tera/src/main.rs index 97b9d4812..23d68cadf 100644 --- a/examples/template_tera/src/main.rs +++ b/examples/template_tera/src/main.rs @@ -37,7 +37,7 @@ fn main() { .middleware(middlewares::Logger::default()) .resource("/", |r| r.method(Method::GET).f(index))}) .bind("127.0.0.1:8080").unwrap() - .start().unwrap(); + .start(); println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index 1f168eb84..8576503a4 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -214,7 +214,7 @@ fn main() { |r| r.h(fs::StaticFiles::new("tail", "static/", true))) }) .bind("127.0.0.1:8080").unwrap() - .start().unwrap(); + .start(); let _ = sys.run(); } diff --git a/examples/websocket.rs b/examples/websocket.rs index 8f62ef296..4b416b541 100644 --- a/examples/websocket.rs +++ b/examples/websocket.rs @@ -71,7 +71,7 @@ fn main() { |r| r.h(fs::StaticFiles::new("tail", "examples/static/", true)))) // start http server on 127.0.0.1:8080 .bind("127.0.0.1:8080").unwrap() - .start().unwrap(); + .start(); println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); diff --git a/guide/src/qs_3_5.md b/guide/src/qs_3_5.md index 70eb48095..01ccffd99 100644 --- a/guide/src/qs_3_5.md +++ b/guide/src/qs_3_5.md @@ -22,7 +22,7 @@ fn main() { || Application::new() .resource("/", |r| r.f(|_| httpcodes::HTTPOk))) .bind("127.0.0.1:59080").unwrap() - .start().unwrap(); + .start(); # actix::Arbiter::system().send(actix::msgs::SystemExit(0)); let _ = sys.run(); @@ -78,7 +78,8 @@ fn main() { HttpServer::new( || Application::new() .resource("/index.html", |r| r.f(index))) - .serve_ssl::<_, ()>("127.0.0.1:8080", pkcs12).unwrap(); + .bind("127.0.0.1:8080").unwrap() + .serve_ssl(pkcs12).unwrap(); } ``` diff --git a/src/server.rs b/src/server.rs index 9bec090a2..08d6645c6 100644 --- a/src/server.rs +++ b/src/server.rs @@ -261,10 +261,12 @@ impl HttpServer /// /// This method starts number of http handler workers in seperate threads. /// For each address this method starts separate thread which does `accept()` in a loop. - pub fn start(mut self) -> io::Result> + /// + /// This methods panics if no socket addresses get bound. + pub fn start(mut self) -> SyncAddress { if self.sockets.is_empty() { - Err(io::Error::new(io::ErrorKind::Other, "No socket addresses are bound")) + panic!("HttpServer::bind() has to be called befor start()"); } else { let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect(); let settings = ServerSettings::new(Some(addrs[0].0), false); @@ -277,7 +279,7 @@ impl HttpServer } // start http server actor - Ok(HttpServer::create(|_| {self})) + HttpServer::create(|_| {self}) } } } @@ -366,7 +368,7 @@ impl HttpServer /// Start listening for incomming connections from a stream. /// /// This method uses only one thread for handling incoming connections. - pub fn start_incoming(mut self, stream: S, secure: bool) -> io::Result> + pub fn start_incoming(mut self, stream: S, secure: bool) -> SyncAddress where S: Stream + 'static { if !self.sockets.is_empty() { @@ -391,11 +393,11 @@ impl HttpServer self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive))); // start server - Ok(HttpServer::create(move |ctx| { + HttpServer::create(move |ctx| { ctx.add_stream(stream.map( move |(t, _)| IoStream{io: t, peer: None, http2: false})); self - })) + }) } } diff --git a/tests/test_server.rs b/tests/test_server.rs index 71f0e0f9e..14369da37 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -18,7 +18,7 @@ fn test_serve() { let srv = HttpServer::new( || vec![Application::new() .resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]); - srv.bind("127.0.0.1:58902").unwrap().start().unwrap(); + srv.bind("127.0.0.1:58902").unwrap().start(); sys.run(); }); assert!(reqwest::get("http://localhost:58902/").unwrap().status().is_success()); @@ -39,7 +39,7 @@ fn test_serve_incoming() { || Application::new() .resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))); let tcp = TcpListener::from_listener(tcp, &addr2, Arbiter::handle()).unwrap(); - srv.start_incoming(tcp.incoming(), false).unwrap(); + srv.start_incoming(tcp.incoming(), false); sys.run(); }); @@ -90,7 +90,7 @@ fn test_middlewares() { finish: Arc::clone(&act_num3)}) .resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]) .bind("127.0.0.1:58904").unwrap() - .start().unwrap(); + .start(); sys.run(); });