1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-23 16:40:37 +00: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| {
println!("{:?}", req);
httpcodes::HTTPFound
.build()
HttpResponse::Found()
.header("LOCATION", "/index.html")
.finish()
})))

View file

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

View file

@ -53,11 +53,13 @@ impl Default for ServerSettings {
impl ServerSettings {
/// Crate server settings instance
fn new(addr: Option<net::SocketAddr>, secure: bool) -> Self {
let host = if let Some(ref addr) = addr {
fn new(addr: Option<net::SocketAddr>, host: &Option<String>, secure: bool) -> Self {
let host = if let Some(ref host) = *host {
host.clone()
} else if let Some(ref addr) = addr {
format!("{}", addr)
} else {
"unknown".to_owned()
"localhost".to_owned()
};
ServerSettings {
addr: addr,
@ -97,6 +99,7 @@ pub struct HttpServer<T, A, H, U>
addr: PhantomData<A>,
threads: usize,
backlog: i32,
host: Option<String>,
keep_alive: Option<u64>,
factory: Arc<Fn() -> U + Send + Sync>,
workers: Vec<SyncAddress<Worker<H>>>,
@ -134,6 +137,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
addr: PhantomData,
threads: num_cpus::get(),
backlog: 2048,
host: None,
keep_alive: None,
factory: Arc::new(factory),
workers: Vec::new(),
@ -175,6 +179,16 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
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
///
/// 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()");
} else {
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);
// start acceptors threads
@ -395,7 +409,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
{
if !self.sockets.is_empty() {
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);
// start acceptors threads
@ -407,7 +421,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
// set server settings
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();
for app in &mut apps {
app.server_settings(settings.clone());