pub struct ServerConfig {
pub bind_addr: String,
pub request_timeout: Time,
pub max_connections: usize,
pub read_buffer_size: usize,
pub parse_limits: ParseLimits,
pub allowed_hosts: Vec<String>,
pub trust_x_forwarded_host: bool,
pub tcp_nodelay: bool,
pub keep_alive_timeout: Duration,
pub max_requests_per_connection: usize,
pub drain_timeout: Duration,
}Expand description
Server configuration for the HTTP/1.1 server.
Controls bind address, timeouts, connection limits, and HTTP parsing behavior. All timeouts use sensible defaults suitable for production use.
§Defaults
| Setting | Default |
|---|---|
request_timeout | 30s |
max_connections | 0 (unlimited) |
read_buffer_size | 8192 bytes |
tcp_nodelay | true |
keep_alive_timeout | 75s |
max_requests_per_connection | 100 |
drain_timeout | 30s |
§Example
use fastapi_http::{ServerConfig, serve_with_config};
let config = ServerConfig::new("0.0.0.0:8000")
.with_request_timeout_secs(60)
.with_max_connections(1000)
.with_keep_alive_timeout_secs(120);Fields§
§bind_addr: StringAddress to bind to.
request_timeout: TimeDefault request timeout.
max_connections: usizeMaximum connections (0 = unlimited).
read_buffer_size: usizeRead buffer size.
parse_limits: ParseLimitsHTTP parse limits.
allowed_hosts: Vec<String>Allowed hostnames for Host header validation (empty = allow all).
trust_x_forwarded_host: boolWhether to trust X-Forwarded-Host for host validation.
tcp_nodelay: boolEnable TCP_NODELAY.
keep_alive_timeout: DurationKeep-alive timeout (time to wait for next request on a connection). Set to 0 to disable keep-alive timeout.
max_requests_per_connection: usizeMaximum requests per connection (0 = unlimited).
drain_timeout: DurationDrain timeout (time to wait for in-flight requests on shutdown). After this timeout, connections are forcefully closed.
Implementations§
Source§impl ServerConfig
impl ServerConfig
Sourcepub fn new(bind_addr: impl Into<String>) -> Self
pub fn new(bind_addr: impl Into<String>) -> Self
Creates a new server configuration with the given bind address.
Sourcepub fn with_request_timeout(self, timeout: Time) -> Self
pub fn with_request_timeout(self, timeout: Time) -> Self
Sets the request timeout.
Sourcepub fn with_request_timeout_secs(self, secs: u64) -> Self
pub fn with_request_timeout_secs(self, secs: u64) -> Self
Sets the request timeout in seconds.
Sourcepub fn with_max_connections(self, max: usize) -> Self
pub fn with_max_connections(self, max: usize) -> Self
Sets the maximum number of connections.
Sourcepub fn with_read_buffer_size(self, size: usize) -> Self
pub fn with_read_buffer_size(self, size: usize) -> Self
Sets the read buffer size.
Sourcepub fn with_parse_limits(self, limits: ParseLimits) -> Self
pub fn with_parse_limits(self, limits: ParseLimits) -> Self
Sets the HTTP parse limits.
Sourcepub fn with_allowed_hosts<I, S>(self, hosts: I) -> Self
pub fn with_allowed_hosts<I, S>(self, hosts: I) -> Self
Sets allowed hosts for Host header validation.
An empty list means “allow any host”. Patterns are normalized to lowercase for case-insensitive matching.
Sourcepub fn allow_host(self, host: impl Into<String>) -> Self
pub fn allow_host(self, host: impl Into<String>) -> Self
Adds a single allowed host.
The pattern is normalized to lowercase for case-insensitive matching.
Sourcepub fn with_trust_x_forwarded_host(self, trust: bool) -> Self
pub fn with_trust_x_forwarded_host(self, trust: bool) -> Self
Enables or disables trust of X-Forwarded-Host.
Sourcepub fn with_tcp_nodelay(self, enabled: bool) -> Self
pub fn with_tcp_nodelay(self, enabled: bool) -> Self
Enables or disables TCP_NODELAY.
Sourcepub fn with_keep_alive_timeout(self, timeout: Duration) -> Self
pub fn with_keep_alive_timeout(self, timeout: Duration) -> Self
Sets the keep-alive timeout.
This is the time to wait for another request on a keep-alive connection before closing it. Set to Duration::ZERO to disable keep-alive timeout.
Sourcepub fn with_keep_alive_timeout_secs(self, secs: u64) -> Self
pub fn with_keep_alive_timeout_secs(self, secs: u64) -> Self
Sets the keep-alive timeout in seconds.
Sourcepub fn with_max_requests_per_connection(self, max: usize) -> Self
pub fn with_max_requests_per_connection(self, max: usize) -> Self
Sets the maximum requests per connection.
Set to 0 for unlimited requests per connection.
Sourcepub fn with_drain_timeout(self, timeout: Duration) -> Self
pub fn with_drain_timeout(self, timeout: Duration) -> Self
Sets the drain timeout.
This is the time to wait for in-flight requests to complete during shutdown. After this timeout, connections are forcefully closed.
Sourcepub fn with_drain_timeout_secs(self, secs: u64) -> Self
pub fn with_drain_timeout_secs(self, secs: u64) -> Self
Sets the drain timeout in seconds.
Trait Implementations§
Source§impl Clone for ServerConfig
impl Clone for ServerConfig
Source§fn clone(&self) -> ServerConfig
fn clone(&self) -> ServerConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more