pub struct HttpServer<Cfg, F, I, Sf>where
Cfg: ServerAppConfig,
Cfg::State: Clone,
F: AsyncFn(&Cfg::State) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, Cfg::State, Request>,
Sf: ServiceFactory<Cfg::State, Request>,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,{ /* private fields */ }Expand description
An HTTP Server.
Create new http server with application factory.
use ntex::web::{self, App, HttpResponse, HttpServer};
#[ntex::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(
async |_| App::new()
.service(web::resource("/").to(async || { HttpResponse::Ok() })))
.bind("127.0.0.1:59090", ntex::SharedCfg::default())?
.run()
.await
}Implementations§
Source§impl<F, I, Sf> HttpServer<NoConfig, F, I, Sf>where
F: AsyncFn(&()) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, (), Request>,
Sf: ServiceFactory<(), Request> + 'static,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,
impl<F, I, Sf> HttpServer<NoConfig, F, I, Sf>where
F: AsyncFn(&()) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, (), Request>,
Sf: ServiceFactory<(), Request> + 'static,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,
Source§impl<Cfg, F, I, Sf> HttpServer<Cfg, F, I, Sf>where
Cfg: ServerAppConfig,
Cfg::State: Clone,
F: AsyncFn(&Cfg::State) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, Cfg::State, Request>,
Sf: ServiceFactory<Cfg::State, Request> + 'static,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,
impl<Cfg, F, I, Sf> HttpServer<Cfg, F, I, Sf>where
Cfg: ServerAppConfig,
Cfg::State: Clone,
F: AsyncFn(&Cfg::State) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, Cfg::State, Request>,
Sf: ServiceFactory<Cfg::State, Request> + 'static,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,
Sourcepub fn with_config(cfg: Cfg, factory: F) -> Selfwhere
Cfg: ServerAppConfig,
pub fn with_config(cfg: Cfg, factory: F) -> Selfwhere
Cfg: ServerAppConfig,
Create new http server with application factory and state mapping
Sourcepub fn workers(self, num: usize) -> Self
pub fn workers(self, num: usize) -> Self
Set number of workers to start.
By default http server uses number of available logical cpu as threads count.
Sourcepub fn backlog(self, backlog: i32) -> Self
pub fn backlog(self, backlog: i32) -> Self
Set the maximum number of pending connections.
This refers to the number of clients that can be waiting to be served. Exceeding this number results in the client getting an error when attempting to connect. It should only affect servers under significant load.
Generally set in the 64-2048 range. Default value is 1024.
This method should be called before bind() method call.
Sourcepub fn max_connections(self, num: usize) -> Self
pub fn max_connections(self, num: usize) -> Self
Sets the maximum per-worker number of concurrent connections.
All socket listeners will stop accepting connections when this limit is reached for each worker.
By default max connections is set to a 25k.
Sourcepub fn max_tls_handshakes(self, num: usize) -> Self
pub fn max_tls_handshakes(self, num: usize) -> Self
Sets the maximum per-worker number of concurrent TLS handshakes.
This limit applies only to TLS acceptors (OpenSSL and rustls). When it is reached, TLS acceptors stop accepting new handshakes until one completes. Plain TCP listeners are not affected. It can be used to limit the CPU usage of TLS handshakes.
By default the limit is 256.
Sourcepub fn server_hostname<T: AsRef<str>>(self, val: T) -> Self
pub fn server_hostname<T: AsRef<str>>(self, val: T) -> Self
Set server host name.
Host name is used by application router as a hostname for url generation.
Check ConnectionInfo::host()
documentation for more information.
By default host name is set to a “localhost:8080” value.
Sourcepub fn stop_runtime(self) -> Self
pub fn stop_runtime(self) -> Self
Stop ntex runtime when server get dropped.
By default “stop runtime” is disabled.
Sourcepub fn stop_on_panic(self) -> Self
pub fn stop_on_panic(self) -> Self
Stops the server when one of the workers panics.
By default, “stop on panic” is disabled.
Sourcepub fn disable_signals(self) -> Self
pub fn disable_signals(self) -> Self
Disable signal handling.
By default, signal handling is enabled.
Sourcepub fn graceful_shutdown_timeout(self, sec: Seconds) -> Self
pub fn graceful_shutdown_timeout(self, sec: Seconds) -> Self
Timeout for graceful worker shutdown.
After receiving a stop signal, workers have this much time to finish serving requests. Workers that are still alive after the timeout are forcefully dropped.
This bounds the worker as a whole, not an individual connection. Each
connection is bound separately by IoConfig::set_shutdown_timeout, so
this value should leave room for the connections a worker is still
draining to shut down themselves.
By default, the timeout is set to 30 seconds.
Sourcepub fn enable_affinity(self) -> Self
pub fn enable_affinity(self) -> Self
Enable cpu affinity.
By default, affinity is disabled.
Sourcepub fn graceful_shutdown(self) -> Self
pub fn graceful_shutdown(self) -> Self
Graceful shutdown.
When enabled, SIGQUIT, SIGSEGV, SIGABRT, and application panics stop the server gracefully. SIGTERM always stops the server gracefully, and SIGINT always stops it immediately.
By default, these events stop the server immediately.
Sourcepub fn listen(self, lst: TcpListener, cfg: impl Into<SharedCfg>) -> Result<Self>
pub fn listen(self, lst: TcpListener, cfg: impl Into<SharedCfg>) -> Result<Self>
Use listener for accepting incoming connection requests
HttpServer does not change any configuration for TcpListener,
it needs to be configured before passing it to listen() method.
Sourcepub fn listen_openssl(
self,
lst: TcpListener,
cfg: impl Into<SharedCfg>,
builder: SslAcceptorBuilder,
) -> Result<Self>
pub fn listen_openssl( self, lst: TcpListener, cfg: impl Into<SharedCfg>, builder: SslAcceptorBuilder, ) -> Result<Self>
Use listener for accepting incoming tls connection requests.
This method sets alpn protocols to “h2” and “http/1.1”
Sourcepub fn listen_rustls(
self,
lst: TcpListener,
cfg: impl Into<SharedCfg>,
config: RustlsServerConfig,
) -> Result<Self>
pub fn listen_rustls( self, lst: TcpListener, cfg: impl Into<SharedCfg>, config: RustlsServerConfig, ) -> Result<Self>
Use listener for accepting incoming tls connection requests.
This method sets alpn protocols to “h2” and “http/1.1”
Sourcepub fn bind<A: ToSocketAddrs>(
self,
addr: A,
cfg: impl Into<SharedCfg>,
) -> Result<Self>
pub fn bind<A: ToSocketAddrs>( self, addr: A, cfg: impl Into<SharedCfg>, ) -> Result<Self>
The socket address to bind.
To bind multiple addresses this method can be called multiple times.
Sourcepub fn bind_openssl<A>(
self,
addr: A,
builder: SslAcceptorBuilder,
cfg: impl Into<SharedCfg>,
) -> Result<Self>where
A: ToSocketAddrs,
pub fn bind_openssl<A>(
self,
addr: A,
builder: SslAcceptorBuilder,
cfg: impl Into<SharedCfg>,
) -> Result<Self>where
A: ToSocketAddrs,
Start listening for incoming tls connections.
This method sets alpn protocols to “h2” and “http/1.1”
Sourcepub fn bind_rustls<A: ToSocketAddrs>(
self,
addr: A,
config: &RustlsServerConfig,
cfg: impl Into<SharedCfg>,
) -> Result<Self>
pub fn bind_rustls<A: ToSocketAddrs>( self, addr: A, config: &RustlsServerConfig, cfg: impl Into<SharedCfg>, ) -> Result<Self>
Start listening for incoming tls connections.
This method sets alpn protocols to “h2” and “http/1.1”
Sourcepub fn listen_uds(
self,
lst: UnixListener,
cfg: impl Into<SharedCfg>,
) -> Result<Self>
pub fn listen_uds( self, lst: UnixListener, cfg: impl Into<SharedCfg>, ) -> Result<Self>
Start listening for unix domain connections on existing listener.
This method is available only on Unix.
Source§impl<Cfg, F, I, Sf> HttpServer<Cfg, F, I, Sf>where
Cfg: ServerAppConfig,
Cfg::State: Clone,
F: AsyncFn(&Cfg::State) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, Cfg::State, Request>,
Sf: ServiceFactory<Cfg::State, Request> + 'static,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,
impl<Cfg, F, I, Sf> HttpServer<Cfg, F, I, Sf>where
Cfg: ServerAppConfig,
Cfg::State: Clone,
F: AsyncFn(&Cfg::State) -> I + Send + Clone + 'static,
I: IntoServiceFactory<Sf, Cfg::State, Request>,
Sf: ServiceFactory<Cfg::State, Request> + 'static,
Sf::Res: Into<Response>,
Sf::Error: ResponseError,
Sf::InitError: IntoFailure,
Sourcepub fn run(self) -> Server
pub fn run(self) -> Server
Start listening for incoming connections.
This method starts number of http workers in separate threads.
For each address this method starts separate thread which does
accept() in a loop.
§Panics
Panics if no listener was registered before calling this method.
use ntex::web::{self, App, HttpResponse, HttpServer};
#[ntex::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(
async |_| App::new().service(web::resource("/").to(async || { HttpResponse::Ok() }))
)
.bind("127.0.0.1:0", ntex::SharedCfg::default())?
.run()
.await
}