1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

simplify server start method

This commit is contained in:
Nikolay Kim 2017-12-19 09:08:36 -08:00
parent 4f6145e5c7
commit 13cbfc877d
9 changed files with 20 additions and 17 deletions

View File

@ -93,7 +93,7 @@ fn main() {
.body(Body::Empty) .body(Body::Empty)
}))) })))
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start().unwrap(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();

View File

@ -105,7 +105,7 @@ fn main() {
.middleware(middlewares::Logger::default()) .middleware(middlewares::Logger::default())
.resource("/{name}", |r| r.method(Method::GET).a(index))}) .resource("/{name}", |r| r.method(Method::GET).a(index))})
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start().unwrap(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();

View File

@ -71,7 +71,7 @@ fn main() {
// register simple handler, handle all methods // register simple handler, handle all methods
.resource("/", |r| r.f(index))) .resource("/", |r| r.f(index)))
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start().unwrap(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();

View File

@ -37,7 +37,7 @@ fn main() {
.middleware(middlewares::Logger::default()) .middleware(middlewares::Logger::default())
.resource("/", |r| r.method(Method::GET).f(index))}) .resource("/", |r| r.method(Method::GET).f(index))})
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start().unwrap(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();

View File

@ -214,7 +214,7 @@ fn main() {
|r| r.h(fs::StaticFiles::new("tail", "static/", true))) |r| r.h(fs::StaticFiles::new("tail", "static/", true)))
}) })
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start().unwrap(); .start();
let _ = sys.run(); let _ = sys.run();
} }

View File

@ -71,7 +71,7 @@ fn main() {
|r| r.h(fs::StaticFiles::new("tail", "examples/static/", true)))) |r| r.h(fs::StaticFiles::new("tail", "examples/static/", true))))
// start http server on 127.0.0.1:8080 // start http server on 127.0.0.1:8080
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.start().unwrap(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");
let _ = sys.run(); let _ = sys.run();

View File

@ -22,7 +22,7 @@ fn main() {
|| Application::new() || Application::new()
.resource("/", |r| r.f(|_| httpcodes::HTTPOk))) .resource("/", |r| r.f(|_| httpcodes::HTTPOk)))
.bind("127.0.0.1:59080").unwrap() .bind("127.0.0.1:59080").unwrap()
.start().unwrap(); .start();
# actix::Arbiter::system().send(actix::msgs::SystemExit(0)); # actix::Arbiter::system().send(actix::msgs::SystemExit(0));
let _ = sys.run(); let _ = sys.run();
@ -78,7 +78,8 @@ fn main() {
HttpServer::new( HttpServer::new(
|| Application::new() || Application::new()
.resource("/index.html", |r| r.f(index))) .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();
} }
``` ```

View File

@ -261,10 +261,12 @@ impl<H: HttpHandler, U, V> HttpServer<TcpStream, net::SocketAddr, H, U>
/// ///
/// This method starts number of http handler workers in seperate threads. /// 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. /// For each address this method starts separate thread which does `accept()` in a loop.
pub fn start(mut self) -> io::Result<SyncAddress<Self>> ///
/// This methods panics if no socket addresses get bound.
pub fn start(mut self) -> SyncAddress<Self>
{ {
if self.sockets.is_empty() { 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 { } else {
let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect(); let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect();
let settings = ServerSettings::new(Some(addrs[0].0), false); let settings = ServerSettings::new(Some(addrs[0].0), false);
@ -277,7 +279,7 @@ impl<H: HttpHandler, U, V> HttpServer<TcpStream, net::SocketAddr, H, U>
} }
// start http server actor // start http server actor
Ok(HttpServer::create(|_| {self})) HttpServer::create(|_| {self})
} }
} }
} }
@ -366,7 +368,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
/// Start listening for incomming connections from a stream. /// Start listening for incomming connections from a stream.
/// ///
/// This method uses only one thread for handling incoming connections. /// This method uses only one thread for handling incoming connections.
pub fn start_incoming<S>(mut self, stream: S, secure: bool) -> io::Result<SyncAddress<Self>> pub fn start_incoming<S>(mut self, stream: S, secure: bool) -> SyncAddress<Self>
where S: Stream<Item=(T, A), Error=io::Error> + 'static where S: Stream<Item=(T, A), Error=io::Error> + 'static
{ {
if !self.sockets.is_empty() { if !self.sockets.is_empty() {
@ -391,11 +393,11 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive))); self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
// start server // start server
Ok(HttpServer::create(move |ctx| { HttpServer::create(move |ctx| {
ctx.add_stream(stream.map( ctx.add_stream(stream.map(
move |(t, _)| IoStream{io: t, peer: None, http2: false})); move |(t, _)| IoStream{io: t, peer: None, http2: false}));
self self
})) })
} }
} }

View File

@ -18,7 +18,7 @@ fn test_serve() {
let srv = HttpServer::new( let srv = HttpServer::new(
|| vec![Application::new() || vec![Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]); .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(); sys.run();
}); });
assert!(reqwest::get("http://localhost:58902/").unwrap().status().is_success()); assert!(reqwest::get("http://localhost:58902/").unwrap().status().is_success());
@ -39,7 +39,7 @@ fn test_serve_incoming() {
|| Application::new() || Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))); .resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk)));
let tcp = TcpListener::from_listener(tcp, &addr2, Arbiter::handle()).unwrap(); 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(); sys.run();
}); });
@ -90,7 +90,7 @@ fn test_middlewares() {
finish: Arc::clone(&act_num3)}) finish: Arc::clone(&act_num3)})
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]) .resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))])
.bind("127.0.0.1:58904").unwrap() .bind("127.0.0.1:58904").unwrap()
.start().unwrap(); .start();
sys.run(); sys.run();
}); });