ServerConfig

Struct ServerConfig 

Source
pub struct ServerConfig {
    pub http1_enabled: bool,
    pub http2: Http2Config,
    pub auto_protocol_detection: bool,
    pub tls: Option<TlsConfig>,
    pub redirect_http_to_https: bool,
    pub https_port: Option<u16>,
    pub max_request_body_size: usize,
}
Expand description

Main server configuration containing all protocol and security settings.

ServerConfig is the primary configuration structure that combines HTTP/1.1, HTTP/2, TLS, and general server settings. It supports both development and production scenarios with flexible configuration options.

§Protocol Support

The server can operate in several modes:

  • HTTP/1.1 only: Traditional HTTP with optional upgrade support
  • HTTP/2 only: Modern binary protocol with multiplexing
  • Dual protocol: Automatic negotiation between HTTP/1.1 and HTTP/2
  • HTTPS: TLS encryption with ALPN protocol negotiation

§Configuration Patterns

§Development Server

use ignitia::ServerConfig;

let dev_config = ServerConfig {
    http1_enabled: true,
    auto_protocol_detection: true,
    ..Default::default()
};

§Production HTTPS Server

use ignitia::{ServerConfig, TlsConfig};

let tls_config = TlsConfig::new("cert.pem", "key.pem")
    .with_alpn_protocols(vec!["h2", "http/1.1"]);

let prod_config = ServerConfig::default()
    .with_tls(tls_config);

§HTTP to HTTPS Redirect

use ignitia::ServerConfig;

let redirect_config = ServerConfig::default()
    .redirect_to_https(443);

Fields§

§http1_enabled: bool

Enable HTTP/1.1 protocol support.

When true, the server accepts HTTP/1.1 connections and can handle traditional HTTP requests. Can be combined with HTTP/2 for dual-protocol support.

§Default

true

§Use Cases

  • Legacy client support: Some clients only support HTTP/1.1
  • Debugging: HTTP/1.1 is easier to debug with standard tools
  • Gradual migration: Maintain compatibility during HTTP/2 rollout

§Performance Considerations

HTTP/1.1 uses more connections for parallelism, which can exhaust server resources faster than HTTP/2’s multiplexing.

§http2: Http2Config

HTTP/2 configuration settings.

Contains all HTTP/2-specific options including flow control, concurrency, and performance tuning parameters.

§Examples

use ignitia::{ServerConfig, Http2Config};
use std::time::Duration;

let custom_http2 = Http2Config {
    max_concurrent_streams: Some(2000),
    keep_alive_interval: Some(Duration::from_secs(30)),
    ..Default::default()
};

let config = ServerConfig {
    http2: custom_http2,
    ..Default::default()
};
§auto_protocol_detection: bool

Enable automatic protocol detection and negotiation.

When true, the server automatically determines whether to use HTTP/1.1 or HTTP/2 based on client capabilities and negotiation results.

§Default

true

§Negotiation Methods

  • ALPN (TLS): Client and server negotiate protocol during TLS handshake
  • HTTP/2 Prior Knowledge: Client sends HTTP/2 directly without upgrade
  • HTTP/1.1 Upgrade: Client requests upgrade via HTTP/1.1 headers

§When to Disable

  • Force specific protocol version
  • Simplified deployment scenarios
  • Legacy system integration
§tls: Option<TlsConfig>
Available on crate feature tls only.

TLS (Transport Layer Security) configuration.

When provided, enables HTTPS support with customizable cipher suites, protocol versions, and certificate settings.

§Security Best Practices

  • Use TLS 1.2 or higher for production
  • Configure strong cipher suites
  • Implement proper certificate management
  • Consider HSTS headers for enhanced security

§Examples

use ignitia::{ServerConfig, TlsConfig, TlsVersion};

let tls_config = TlsConfig::new("server.crt", "server.key")
    .tls_versions(TlsVersion::TlsV12, TlsVersion::TlsV13)
    .with_alpn_protocols(vec!["h2", "http/1.1"]);

let config = ServerConfig::default().with_tls(tls_config);
§redirect_http_to_https: bool

Automatically redirect HTTP requests to HTTPS.

When true, all HTTP requests receive a 301 redirect to the equivalent HTTPS URL. Useful for enforcing encrypted connections.

§Default

false

§Requirements

  • Must specify https_port
  • HTTPS server must be running on the specified port
  • Consider HSTS headers for additional security

§Security Benefits

  • Prevents accidental plaintext communication
  • Helps meet compliance requirements
  • Protects against protocol downgrade attacks
§https_port: Option<u16>

HTTPS port number for redirect responses.

Used when redirect_http_to_https is true to construct redirect URLs. Standard HTTPS port is 443, but custom ports are supported.

§Default

None

§Examples

use ignitia::ServerConfig;

// Standard HTTPS port
let config = ServerConfig::default().redirect_to_https(443);

// Custom HTTPS port
let dev_config = ServerConfig::default().redirect_to_https(8443);
§max_request_body_size: usize

Maximum size of HTTP request body in bytes.

Controls the server-level limit on incoming request body size. Requests with bodies larger than this limit are immediately rejected with a 413 Payload Too Large response before reaching any middleware or handlers.

§Server vs Middleware Limits

This server-level limit acts as a hard ceiling and should be set higher than or equal to any middleware body size limits. The processing order is:

  1. Server limit (this setting) - Applied first during request parsing
  2. Middleware limits - Applied during middleware processing
  3. Handler processing - Only reached if all limits pass

§Default

10 * 1024 * 1024 (10MB)

§Security Considerations

  • DoS Protection: Prevents memory exhaustion from extremely large requests
  • Resource Management: Limits server memory usage per connection
  • Early Rejection: Saves processing cycles by rejecting oversized requests immediately

§Performance Impact

  • Low limits (1-10MB): Good for APIs with small payloads (JSON, forms)
  • Medium limits (10-100MB): Suitable for file uploads, media processing
  • High limits (100MB-1GB+): For bulk data transfer, video uploads

§Examples

§API Server (Small Payloads)

use ignitia::ServerConfig;

let api_config = ServerConfig::default()
    .with_max_request_body_size(5 * 1024 * 1024); // 5MB

§File Upload Server

use ignitia::ServerConfig;

let upload_config = ServerConfig::default()
    .with_max_request_body_size(500 * 1024 * 1024); // 500MB

§High-Volume Data Processing

use ignitia::ServerConfig;

let bulk_config = ServerConfig::default()
    .with_max_request_body_size(2 * 1024 * 1024 * 1024); // 2GB

§Middleware Coordination

use ignitia::{ServerConfig, Router, middleware::BodySizeLimitMiddleware};

// Server allows up to 100MB (safety net)
let config = ServerConfig::default()
    .with_max_request_body_size(100 * 1024 * 1024);

// Middleware enforces 50MB business logic limit
let router = Router::new()
    .middleware(BodySizeLimitMiddleware::new(50 * 1024 * 1024));

§Error Handling

When this limit is exceeded, the server returns:

  • Status: 413 Payload Too Large
  • Body: "Request too large"
  • Timing: Before any middleware or handlers execute

For custom error handling, ensure middleware limits are lower than this value.

Implementations§

Source§

impl ServerConfig

Source

pub fn with_tls(self, tls_config: TlsConfig) -> Self

Available on crate feature tls only.

Configure the server to use TLS encryption.

Enables HTTPS support with the provided TLS configuration. The TLS config includes certificate paths, cipher suites, protocol versions, and ALPN settings.

§Arguments
  • tls_config - Complete TLS configuration including certificates and security settings
§Returns

Modified ServerConfig with TLS enabled

§Examples
use ignitia::{ServerConfig, TlsConfig};

let tls_config = TlsConfig::new("server.crt", "private.key")
    .with_alpn_protocols(vec!["h2", "http/1.1"]);

let server_config = ServerConfig::default()
    .with_tls(tls_config);

assert!(server_config.is_tls_enabled());
§Security Considerations
  • Ensure certificate files are properly secured
  • Use strong cipher suites and recent TLS versions
  • Consider certificate rotation and renewal processes
  • Implement proper key management practices
Source

pub fn redirect_to_https(self, https_port: u16) -> Self

Enable automatic HTTP to HTTPS redirection.

Configures the server to send 301 Moved Permanently responses for all HTTP requests, redirecting clients to the equivalent HTTPS URL.

§Arguments
  • https_port - Port number where HTTPS server is listening
§Returns

Modified ServerConfig with redirect enabled

§Redirect Behavior
  • Preserves the original path and query string
  • Uses 301 status code for permanent redirect
  • Handles both standard (443) and custom HTTPS ports
§Examples
use ignitia::ServerConfig;

// Redirect to standard HTTPS port
let config = ServerConfig::default().redirect_to_https(443);

// Redirect to custom port for development
let dev_config = ServerConfig::default().redirect_to_https(8443);

assert_eq!(config.redirect_http_to_https, true);
assert_eq!(config.https_port, Some(443));
§Production Deployment
use ignitia::{ServerConfig, TlsConfig};

let production_config = ServerConfig::default()
    .with_tls(TlsConfig::new("cert.pem", "key.pem"))
    .redirect_to_https(443);
§Security Benefits
  • Enforces encrypted communication
  • Prevents accidental plaintext data transmission
  • Improves compliance with security standards
  • Protects against man-in-the-middle attacks
Source

pub fn is_tls_enabled(&self) -> bool

Available on crate feature tls only.

Check if TLS/HTTPS is enabled in the current configuration.

Returns true if a TLS configuration has been provided, indicating that the server will support HTTPS connections.

§Returns

true if TLS is configured, false otherwise

§Examples
use ignitia::ServerConfig;

let http_config = ServerConfig::default();
assert_eq!(http_config.is_tls_enabled(), false);

{
    use ignitia::TlsConfig;

    let https_config = ServerConfig::default()
        .with_tls(TlsConfig::new("cert.pem", "key.pem"));
    assert_eq!(https_config.is_tls_enabled(), true);
}
§Use Cases
  • Conditional logic based on security configuration
  • Validation of server setup
  • Dynamic port binding decisions
  • Logging and monitoring setup
Source

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

Configure the maximum allowed request body size in bytes.

Sets the server-level hard limit for HTTP request body size. This limit is enforced before any middleware processing and provides DoS protection against extremely large requests.

§Arguments
  • size - Maximum body size in bytes
§Returns

Modified ServerConfig with the new body size limit

§Recommendations

Choose based on your use case:

  • REST APIs: 1-10MB for JSON payloads
  • File uploads: 50-500MB depending on file types
  • Bulk data: 1GB+ for data processing applications
  • Development: Higher limits for testing flexibility

Always set higher than middleware limits:

use ignitia::{ServerConfig, Router};

// Server: 100MB safety limit
let config = ServerConfig::default()
    .with_max_request_body_size(100 * 1024 * 1024);

// Middleware: 50MB business logic limit
let router = Router::new()
    .middleware(/* body size middleware with 50MB limit */);
§Examples
§Conservative API Server
use ignitia::ServerConfig;

let config = ServerConfig::default()
    .with_max_request_body_size(1024 * 1024); // 1MB
§Media Upload Service
use ignitia::ServerConfig;

let config = ServerConfig::default()
    .with_max_request_body_size(1024 * 1024 * 1024); // 1GB
§Development Environment
use ignitia::ServerConfig;

let config = ServerConfig::default()
    .with_max_request_body_size(5 * 1024 * 1024 * 1024); // 5GB for testing
§Security Impact
  • Too Low: Legitimate requests may be rejected
  • Too High: Server vulnerable to memory exhaustion attacks
  • Balanced: Protects server while allowing intended use cases

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

Creates a default server configuration suitable for development and testing.

The default configuration enables both HTTP/1.1 and HTTP/2 with automatic protocol detection. TLS is disabled by default for easier development setup.

§Default Values
  • http1_enabled: true - Support legacy clients
  • http2: Http2Config::default() - Optimized HTTP/2 settings
  • auto_protocol_detection: true - Automatic negotiation
  • tls: None - No TLS (HTTP only)
  • redirect_http_to_https: false - No automatic redirect
  • https_port: None - No HTTPS port specified
§Production Considerations

For production deployments, consider:

  • Enabling TLS with proper certificates
  • Configuring HTTP to HTTPS redirect
  • Tuning HTTP/2 settings for your workload
  • Implementing proper logging and monitoring
§Examples
use ignitia::ServerConfig;

let config = ServerConfig::default();
assert_eq!(config.http1_enabled, true);
assert_eq!(config.auto_protocol_detection, true);
assert!(config.tls.is_none());

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: 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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> ErasedDestructor for T
where T: 'static,