use futures_util::future;
use log::info;
use std::net::ToSocketAddrs;
use super::handler::NewHandler;
use super::{bind_server, new_runtime, tcp_listener, StartError};
#[cfg(feature = "testing")]
pub mod test;
pub fn start<NH, A>(addr: A, new_handler: NH) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
start_with_num_threads(addr, new_handler, num_cpus::get())
}
pub fn start_with_num_threads<NH, A>(
addr: A,
new_handler: NH,
threads: usize,
) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
let runtime = new_runtime(threads);
runtime.block_on(init_server(addr, new_handler))
}
pub async fn init_server<NH, A>(addr: A, new_handler: NH) -> Result<(), StartError>
where
NH: NewHandler + 'static,
A: ToSocketAddrs + 'static + Send,
{
let listener = tcp_listener(addr).await?;
let addr = listener.local_addr().unwrap();
info! {
target: "gotham::start",
" Gotham listening on http://{}", addr
}
bind_server(listener, new_handler, future::ok).await
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::State;
use hyper::{Body, Response};
fn handler(_: State) -> (State, Response<Body>) {
unimplemented!()
}
#[test]
fn test_error_on_invalid_port() {
let res = start("0.0.0.0:99999", || Ok(handler));
assert!(res.is_err());
}
}