mirror of
https://github.com/actix/actix-web.git
synced 2024-11-29 04:51:13 +00:00
simplify server method
This commit is contained in:
parent
663492407d
commit
955e50313d
3 changed files with 36 additions and 25 deletions
|
@ -33,8 +33,6 @@ actix-web = { git = "https://github.com/fafhrd91/actix-web.git" }
|
||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
use std::net;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
@ -53,8 +51,7 @@ fn main() {
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.finish())
|
.finish())
|
||||||
.serve::<()>(
|
.serve::<_, ()>("127.0.0.1:8080").unwrap();
|
||||||
&net::SocketAddr::from_str("127.0.0.1:8880").unwrap()).unwrap();
|
|
||||||
|
|
||||||
// stop system
|
// stop system
|
||||||
Arbiter::handle().spawn_fn(|| {
|
Arbiter::handle().spawn_fn(|| {
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -5,9 +5,6 @@ extern crate actix_web;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
use std::net;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
|
||||||
|
@ -107,13 +104,12 @@ fn main() {
|
||||||
|
|
||||||
HttpServer::new(
|
HttpServer::new(
|
||||||
RoutingMap::default()
|
RoutingMap::default()
|
||||||
.app(
|
.app("/blah", Application::default()
|
||||||
"/blah", Application::default()
|
.resource("/test", |r| {
|
||||||
.resource("/test", |r| {
|
r.get::<MyRoute>();
|
||||||
r.get::<MyRoute>();
|
r.post::<MyRoute>();
|
||||||
r.post::<MyRoute>();
|
})
|
||||||
})
|
.finish())
|
||||||
.finish())
|
|
||||||
.resource("/test", |r| r.post::<MyRoute>())
|
.resource("/test", |r| r.post::<MyRoute>())
|
||||||
.resource("/ws/", |r| r.get::<MyWS>())
|
.resource("/ws/", |r| r.get::<MyWS>())
|
||||||
.resource("/simple/", |r|
|
.resource("/simple/", |r|
|
||||||
|
@ -121,8 +117,7 @@ fn main() {
|
||||||
httpcodes::HTTPOk
|
httpcodes::HTTPOk
|
||||||
}))
|
}))
|
||||||
.finish())
|
.finish())
|
||||||
.serve::<()>(
|
.serve::<_, ()>("127.0.0.1:9080").unwrap();
|
||||||
&net::SocketAddr::from_str("127.0.0.1:9080").unwrap()).unwrap();
|
|
||||||
|
|
||||||
println!("starting");
|
println!("starting");
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{io, net, mem};
|
use std::{io, mem, net};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
@ -28,15 +28,34 @@ impl HttpServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start listening for incomming connections.
|
/// Start listening for incomming connections.
|
||||||
pub fn serve<Addr>(self, addr: &net::SocketAddr) -> io::Result<Addr>
|
pub fn serve<S, Addr>(self, addr: S) -> io::Result<Addr>
|
||||||
where Self: ActorAddress<Self, Addr>
|
where Self: ActorAddress<Self, Addr>,
|
||||||
|
S: net::ToSocketAddrs,
|
||||||
{
|
{
|
||||||
let tcp = TcpListener::bind(addr, Arbiter::handle())?;
|
let mut err = None;
|
||||||
|
let mut addrs = Vec::new();
|
||||||
Ok(HttpServer::create(move |ctx| {
|
for iter in addr.to_socket_addrs() {
|
||||||
ctx.add_stream(tcp.incoming());
|
for addr in iter {
|
||||||
self
|
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
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue