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.
§Panics
Panics if num is zero.
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 acceptors(self, num: usize) -> Self
pub fn acceptors(self, num: usize) -> Self
Sets the number of accept threads.
Each accept thread has its own SO_REUSEPORT copy of every TCP
listener, and the kernel spreads incoming connections between them.
This raises the connection rate the server can accept. Unix domain
listeners are served by a single accept thread.
Multiple accept threads are supported on Linux and Android. On other platforms, a single thread is used.
By default, the server uses a single accept thread. One thread is usually enough, add more only if the accept thread is saturated.
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 of this server stop accepting connections when every worker reaches this limit. The limit applies only to this server.
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 per-worker soft limit on concurrent TLS handshakes.
This limit applies only to TLS acceptors (OpenSSL and rustls), which apply readiness backpressure when it is reached. Independent listener or service pipelines with cached readiness can still start handshakes and exceed the limit; it is not a strict admission limit. Plain TCP listeners are not affected.
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
👎Deprecated since 4.4.0: affinity is enabled by default, use affinity() to change it
pub fn enable_affinity(self) -> Self
affinity is enabled by default, use affinity() to change it
Does nothing, affinity is enabled by default.
Sourcepub fn affinity(self, affinity: Affinity) -> Self
pub fn affinity(self, affinity: Affinity) -> Self
Sets the CPU affinity of worker threads.
Workers are spread over physical cores first, see Affinity.
By default, Affinity::Soft is used on Windows and
Affinity::Core on other platforms.
Sourcepub fn load_balancing(self, balancing: LoadBalancing) -> Self
pub fn load_balancing(self, balancing: LoadBalancing) -> Self
Sets how new connections are distributed between workers.
By default, LoadBalancing::IncomingCpu sends new connections to idle
workers, which helps when connections are long-lived or their load is
uneven, and prefers the worker on the CPU that receives a connection’s
network packets. LoadBalancing::RoundRobin sends them to the
workers in turn. LoadBalancing::Reuseport lets each worker accept
TCP connections on its own SO_REUSEPORT socket on Linux.
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. SIGSEGV and SIGABRT require runtime fatal signal handling, application panics require runtime panic handling.
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
}