Struct ntex::web::HttpServer
source · pub struct HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request, AppConfig>,
S: ServiceFactory<Request, AppConfig>,
S::Error: ResponseError,
S::InitError: Debug,
S::Response: Into<Response<B>>,
B: MessageBody,{ /* 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(
|| App::new()
.service(web::resource("/").to(|| async { HttpResponse::Ok() })))
.bind("127.0.0.1:59090")?
.run()
.await
}
Implementations§
source§impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request, AppConfig>,
S: ServiceFactory<Request, AppConfig> + 'static,
S::Error: ResponseError,
S::InitError: Debug,
S::Response: Into<Response<B>>,
B: MessageBody + 'static,
impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request, AppConfig>,
S: ServiceFactory<Request, AppConfig> + 'static,
S::Error: ResponseError,
S::InitError: Debug,
S::Response: Into<Response<B>>,
B: MessageBody + 'static,
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 2048.
This method should be called before bind()
method call.
sourcepub fn maxconn(self, num: usize) -> Self
pub fn maxconn(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 maxconnrate(self, num: usize) -> Self
pub fn maxconnrate(self, num: usize) -> Self
Sets the maximum per-worker concurrent connection establish process.
All listeners will stop accepting connections when this limit is reached. It can be used to limit the global SSL CPU usage.
By default max connections is set to a 256.
sourcepub fn keep_alive<T: Into<KeepAlive>>(self, val: T) -> Self
pub fn keep_alive<T: Into<KeepAlive>>(self, val: T) -> Self
Set server keep-alive setting.
By default keep alive is set to a 5 seconds.
sourcepub fn client_timeout(self, timeout: Seconds) -> Self
pub fn client_timeout(self, timeout: Seconds) -> Self
Set request read timeout in seconds.
Defines a timeout for reading client request headers. If a client does not transmit the entire set headers within this time, the request is terminated with the 408 (Request Time-out) error.
To disable timeout set value to 0.
By default client timeout is set to 3 seconds.
sourcepub fn disconnect_timeout(self, val: Seconds) -> Self
pub fn disconnect_timeout(self, val: Seconds) -> Self
Set server connection disconnect timeout in seconds.
Defines a timeout for shutdown connection. If a shutdown procedure does not complete within this time, the request is dropped.
To disable timeout set value to 0.
By default client timeout is set to 5 seconds.
sourcepub fn ssl_handshake_timeout(self, val: Seconds) -> Self
pub fn ssl_handshake_timeout(self, val: Seconds) -> Self
Set server ssl handshake timeout in seconds.
Defines a timeout for connection ssl handshake negotiation. To disable timeout set value to 0.
By default handshake timeout is set to 5 seconds.
sourcepub fn headers_read_rate(
self,
timeout: Seconds,
max_timeout: Seconds,
rate: u16
) -> Self
pub fn headers_read_rate( self, timeout: Seconds, max_timeout: Seconds, rate: u16 ) -> Self
Set read rate parameters for request headers.
Set max timeout for reading request headers. If the client
sends rate
amount of data, increase the timeout by 1 second for every.
But no more than max_timeout
timeout.
By default headers read rate is set to 1sec with max timeout 5sec.
sourcepub fn payload_read_rate(
self,
timeout: Seconds,
max_timeout: Seconds,
rate: u16
) -> Self
pub fn payload_read_rate( self, timeout: Seconds, max_timeout: Seconds, rate: u16 ) -> Self
Set read rate parameters for request’s payload.
Set time pariod for reading payload. Client must
sends rate
amount of data per one time period.
But no more than max_timeout
timeout.
By default payload read rate is disabled.
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 documentation for more information.
By default host name is set to a “localhost” 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 disable_signals(self) -> Self
pub fn disable_signals(self) -> Self
Disable signal handling.
By default signal handling is enabled.
sourcepub fn shutdown_timeout(self, sec: Seconds) -> Self
pub fn shutdown_timeout(self, sec: Seconds) -> Self
Timeout for graceful workers shutdown.
After receiving a stop signal, workers have this much time to finish serving requests. Workers still alive after the timeout are force dropped.
By default shutdown timeout sets to 30 seconds.
sourcepub fn memory_pool(self, id: PoolId) -> Self
pub fn memory_pool(self, id: PoolId) -> Self
Set memory pool.
Use specified memory pool for memory allocations.
sourcepub fn listen(self, lst: TcpListener) -> Result<Self>
pub fn listen(self, lst: TcpListener) -> 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,
builder: SslAcceptorBuilder
) -> Result<Self>
pub fn listen_openssl( self, lst: TcpListener, 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,
config: RustlsServerConfig
) -> Result<Self>
pub fn listen_rustls( self, lst: TcpListener, 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) -> Result<Self>
pub fn bind<A: ToSocketAddrs>(self, addr: A) -> 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
) -> Result<Self>where
A: ToSocketAddrs,
pub fn bind_openssl<A>(
self,
addr: A,
builder: SslAcceptorBuilder
) -> 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
) -> Result<Self>
pub fn bind_rustls<A: ToSocketAddrs>( self, addr: A, config: RustlsServerConfig ) -> 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) -> Result<Self>
pub fn listen_uds(self, lst: UnixListener) -> Result<Self>
Start listening for unix domain connections on existing listener.
This method is available with uds
feature.
source§impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request, AppConfig>,
S: ServiceFactory<Request, AppConfig>,
S::Error: ResponseError,
S::InitError: Debug,
S::Response: Into<Response<B>>,
S::Service: 'static,
B: MessageBody,
impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request, AppConfig>,
S: ServiceFactory<Request, AppConfig>,
S::Error: ResponseError,
S::InitError: Debug,
S::Response: Into<Response<B>>,
S::Service: 'static,
B: MessageBody,
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.
This methods panics if no socket address can be bound or an ntex system is not yet configured.
use ntex::web::{self, App, HttpResponse, HttpServer};
#[ntex::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(
|| App::new().service(web::resource("/").to(|| async { HttpResponse::Ok() }))
)
.bind("127.0.0.1:0")?
.run()
.await
}