1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00

simplify server method

This commit is contained in:
Nikolay Kim 2017-10-15 14:53:03 -07:00
parent 663492407d
commit 955e50313d
3 changed files with 36 additions and 25 deletions

View file

@ -33,8 +33,6 @@ actix-web = { git = "https://github.com/fafhrd91/actix-web.git" }
extern crate actix;
extern crate actix_web;
extern crate futures;
use std::net;
use std::str::FromStr;
use actix::prelude::*;
use actix_web::*;
@ -53,8 +51,7 @@ fn main() {
})
)
.finish())
.serve::<()>(
&net::SocketAddr::from_str("127.0.0.1:8880").unwrap()).unwrap();
.serve::<_, ()>("127.0.0.1:8080").unwrap();
// stop system
Arbiter::handle().spawn_fn(|| {

View file

@ -5,9 +5,6 @@ extern crate actix_web;
extern crate tokio_core;
extern crate env_logger;
use std::net;
use std::str::FromStr;
use actix::prelude::*;
use actix_web::*;
@ -107,13 +104,12 @@ fn main() {
HttpServer::new(
RoutingMap::default()
.app(
"/blah", Application::default()
.resource("/test", |r| {
r.get::<MyRoute>();
r.post::<MyRoute>();
})
.finish())
.app("/blah", Application::default()
.resource("/test", |r| {
r.get::<MyRoute>();
r.post::<MyRoute>();
})
.finish())
.resource("/test", |r| r.post::<MyRoute>())
.resource("/ws/", |r| r.get::<MyWS>())
.resource("/simple/", |r|
@ -121,8 +117,7 @@ fn main() {
httpcodes::HTTPOk
}))
.finish())
.serve::<()>(
&net::SocketAddr::from_str("127.0.0.1:9080").unwrap()).unwrap();
.serve::<_, ()>("127.0.0.1:9080").unwrap();
println!("starting");
let _ = sys.run();

View file

@ -1,4 +1,4 @@
use std::{io, net, mem};
use std::{io, mem, net};
use std::rc::Rc;
use std::time::Duration;
use std::collections::VecDeque;
@ -28,15 +28,34 @@ impl HttpServer {
}
/// Start listening for incomming connections.
pub fn serve<Addr>(self, addr: &net::SocketAddr) -> io::Result<Addr>
where Self: ActorAddress<Self, Addr>
pub fn serve<S, Addr>(self, addr: S) -> io::Result<Addr>
where Self: ActorAddress<Self, Addr>,
S: net::ToSocketAddrs,
{
let tcp = TcpListener::bind(addr, Arbiter::handle())?;
Ok(HttpServer::create(move |ctx| {
ctx.add_stream(tcp.incoming());
self
}))
let mut err = None;
let mut addrs = Vec::new();
for iter in addr.to_socket_addrs() {
for addr in iter {
match TcpListener::bind(&addr, Arbiter::handle()) {
Ok(tcp) => addrs.push(tcp),
Err(e) => err = Some(e),
}
}
}
if addrs.is_empty() {
if let Some(e) = err.take() {
Err(e)
} else {
Err(io::Error::new(io::ErrorKind::Other, "Can not bind to address."))
}
} else {
Ok(HttpServer::create(move |ctx| {
for tcp in addrs {
ctx.add_stream(tcp.incoming());
}
self
}))
}
}
}