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

add HttpServer::server_hostname method

This commit is contained in:
Nikolay Kim 2017-12-26 14:36:03 -08:00
parent cce9c68a10
commit dd3a2aa68a
3 changed files with 22 additions and 11 deletions

View File

@ -87,8 +87,7 @@ fn main() {
.resource("/", |r| r.method(Method::GET).f(|req| { .resource("/", |r| r.method(Method::GET).f(|req| {
println!("{:?}", req); println!("{:?}", req);
httpcodes::HTTPFound HttpResponse::Found()
.build()
.header("LOCATION", "/index.html") .header("LOCATION", "/index.html")
.finish() .finish()
}))) })))

View File

@ -8,9 +8,6 @@ const X_FORWARDED_PROTO: &str = "X-FORWARDED-PROTO";
/// `HttpRequest` connection information /// `HttpRequest` connection information
///
/// While it is possible to create `ConnectionInfo` directly,
/// consider using `HttpRequest::load_connection_info()` which cache result.
pub struct ConnectionInfo<'a> { pub struct ConnectionInfo<'a> {
scheme: &'a str, scheme: &'a str,
host: &'a str, host: &'a str,
@ -138,6 +135,7 @@ impl<'a> ConnectionInfo<'a> {
/// - X-Forwarded-Host /// - X-Forwarded-Host
/// - Host /// - Host
/// - Uri /// - Uri
/// - Server hostname
pub fn host(&self) -> &str { pub fn host(&self) -> &str {
self.host self.host
} }

View File

@ -53,11 +53,13 @@ impl Default for ServerSettings {
impl ServerSettings { impl ServerSettings {
/// Crate server settings instance /// Crate server settings instance
fn new(addr: Option<net::SocketAddr>, secure: bool) -> Self { fn new(addr: Option<net::SocketAddr>, host: &Option<String>, secure: bool) -> Self {
let host = if let Some(ref addr) = addr { let host = if let Some(ref host) = *host {
host.clone()
} else if let Some(ref addr) = addr {
format!("{}", addr) format!("{}", addr)
} else { } else {
"unknown".to_owned() "localhost".to_owned()
}; };
ServerSettings { ServerSettings {
addr: addr, addr: addr,
@ -97,6 +99,7 @@ pub struct HttpServer<T, A, H, U>
addr: PhantomData<A>, addr: PhantomData<A>,
threads: usize, threads: usize,
backlog: i32, backlog: i32,
host: Option<String>,
keep_alive: Option<u64>, keep_alive: Option<u64>,
factory: Arc<Fn() -> U + Send + Sync>, factory: Arc<Fn() -> U + Send + Sync>,
workers: Vec<SyncAddress<Worker<H>>>, workers: Vec<SyncAddress<Worker<H>>>,
@ -134,6 +137,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
addr: PhantomData, addr: PhantomData,
threads: num_cpus::get(), threads: num_cpus::get(),
backlog: 2048, backlog: 2048,
host: None,
keep_alive: None, keep_alive: None,
factory: Arc::new(factory), factory: Arc::new(factory),
workers: Vec::new(), workers: Vec::new(),
@ -175,6 +179,16 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
self self
} }
/// Set server host name.
///
/// Host name is used by application router aa a hostname for url generation.
/// Check [ConnectionInfo](./dev/struct.ConnectionInfo.html#method.host) documentation
/// for more information.
pub fn server_hostname(mut self, val: String) -> Self {
self.host = Some(val);
self
}
/// The socket address to bind /// The socket address to bind
/// ///
/// To mind multiple addresses this method can be call multiple times. /// To mind multiple addresses this method can be call multiple times.
@ -291,7 +305,7 @@ impl<H: HttpHandler, U, V> HttpServer<TcpStream, net::SocketAddr, H, U>
panic!("HttpServer::bind() has to be called befor start()"); 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), &self.host, false);
let workers = self.start_workers(&settings, &StreamHandlerType::Normal); let workers = self.start_workers(&settings, &StreamHandlerType::Normal);
// start acceptors threads // start acceptors threads
@ -395,7 +409,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
{ {
if !self.sockets.is_empty() { if !self.sockets.is_empty() {
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), &self.host, false);
let workers = self.start_workers(&settings, &StreamHandlerType::Normal); let workers = self.start_workers(&settings, &StreamHandlerType::Normal);
// start acceptors threads // start acceptors threads
@ -407,7 +421,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
// set server settings // set server settings
let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap(); let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
let settings = ServerSettings::new(Some(addr), secure); let settings = ServerSettings::new(Some(addr), &self.host, secure);
let mut apps: Vec<_> = (*self.factory)().into_iter().map(|h| h.into_handler()).collect(); let mut apps: Vec<_> = (*self.factory)().into_iter().map(|h| h.into_handler()).collect();
for app in &mut apps { for app in &mut apps {
app.server_settings(settings.clone()); app.server_settings(settings.clone());