Skip to main content

ServerConfig

Struct ServerConfig 

Source
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

SettingDefault
request_timeout30s
max_connections0 (unlimited)
read_buffer_size8192 bytes
tcp_nodelaytrue
keep_alive_timeout75s
max_requests_per_connection100
drain_timeout30s

§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: String

Address to bind to.

§request_timeout: Time

Default request timeout.

§max_connections: usize

Maximum connections (0 = unlimited).

§read_buffer_size: usize

Read buffer size.

§parse_limits: ParseLimits

HTTP parse limits.

§allowed_hosts: Vec<String>

Allowed hostnames for Host header validation (empty = allow all).

§trust_x_forwarded_host: bool

Whether to trust X-Forwarded-Host for host validation.

§tcp_nodelay: bool

Enable TCP_NODELAY.

§keep_alive_timeout: Duration

Keep-alive timeout (time to wait for next request on a connection). Set to 0 to disable keep-alive timeout.

§max_requests_per_connection: usize

Maximum requests per connection (0 = unlimited).

§drain_timeout: Duration

Drain timeout (time to wait for in-flight requests on shutdown). After this timeout, connections are forcefully closed.

Implementations§

Source§

impl ServerConfig

Source

pub fn new(bind_addr: impl Into<String>) -> Self

Creates a new server configuration with the given bind address.

Source

pub fn with_request_timeout(self, timeout: Time) -> Self

Sets the request timeout.

Source

pub fn with_request_timeout_secs(self, secs: u64) -> Self

Sets the request timeout in seconds.

Source

pub fn with_max_connections(self, max: usize) -> Self

Sets the maximum number of connections.

Source

pub fn with_read_buffer_size(self, size: usize) -> Self

Sets the read buffer size.

Source

pub fn with_parse_limits(self, limits: ParseLimits) -> Self

Sets the HTTP parse limits.

Source

pub fn with_allowed_hosts<I, S>(self, hosts: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Sets allowed hosts for Host header validation.

An empty list means “allow any host”. Patterns are normalized to lowercase for case-insensitive matching.

Source

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.

Source

pub fn with_trust_x_forwarded_host(self, trust: bool) -> Self

Enables or disables trust of X-Forwarded-Host.

Source

pub fn with_tcp_nodelay(self, enabled: bool) -> Self

Enables or disables TCP_NODELAY.

Source

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.

Source

pub fn with_keep_alive_timeout_secs(self, secs: u64) -> Self

Sets the keep-alive timeout in seconds.

Source

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.

Source

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.

Source

pub fn with_drain_timeout_secs(self, secs: u64) -> Self

Sets the drain timeout in seconds.

Trait Implementations§

Source§

impl Clone for ServerConfig

Source§

fn clone(&self) -> ServerConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ServerConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ServerConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T