Skip to main content

AppConfig

Struct AppConfig 

Source
pub struct AppConfig {
    pub name: String,
    pub version: String,
    pub debug: bool,
    pub max_body_size: usize,
    pub request_timeout_ms: u64,
    pub root_path: String,
    pub root_path_in_servers: bool,
    pub trailing_slash_mode: TrailingSlashMode,
    pub debug_config: DebugConfig,
}
Expand description

Application configuration for the FastAPI Rust framework.

Controls application-level settings including naming, debug mode, body size limits, timeouts, and routing behavior.

§Defaults

SettingDefault
name"FastAPI"
version"0.1.0"
debugfalse
max_body_size1 MB (1,048,576 bytes)
request_timeout_ms30,000 ms
root_path""
trailing_slash_modeStrict

§Example

use fastapi_core::AppConfig;

let config = AppConfig::default()
    .with_name("my-api")
    .with_version("2.0.0")
    .with_debug(true)
    .with_max_body_size(10 * 1024 * 1024); // 10 MB

Fields§

§name: String

Application name (used in logging and OpenAPI).

§version: String

Application version.

§debug: bool

Enable debug mode.

§max_body_size: usize

Maximum request body size in bytes.

§request_timeout_ms: u64

Default request timeout in milliseconds.

§root_path: String

Root path prefix for apps behind a reverse proxy.

When the application is served behind a reverse proxy at a sub-path, this should be set to that sub-path. For example, if the app is proxied at /api/v1, set root_path = "/api/v1".

This affects:

  • URL generation via url_for()
  • OpenAPI servers list (if root_path_in_servers is true)
§root_path_in_servers: bool

Whether to include the root_path in OpenAPI servers list.

When true and root_path is set, a server entry with the root_path will be added to the OpenAPI specification’s servers array.

§trailing_slash_mode: TrailingSlashMode

Trailing slash handling mode.

Controls how the router handles trailing slashes in URLs:

  • Strict (default): /users and /users/ are different routes
  • Redirect: 308 redirect /users/ to /users
  • RedirectWithSlash: 308 redirect /users to /users/
  • MatchBoth: accept both forms without redirect
§debug_config: DebugConfig

Debug mode configuration.

Controls whether error responses include additional diagnostic information such as source location, handler name, and route pattern. When a debug header and token are configured, debug info is only included for requests that present the correct token.

Implementations§

Source§

impl AppConfig

Source

pub fn new() -> Self

Creates a new configuration with defaults.

Source

pub fn name(self, name: impl Into<String>) -> Self

Sets the application name.

Source

pub fn version(self, version: impl Into<String>) -> Self

Sets the application version.

Source

pub fn debug(self, debug: bool) -> Self

Enables or disables debug mode.

Source

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

Sets the maximum request body size.

Source

pub fn request_timeout_ms(self, timeout: u64) -> Self

Sets the default request timeout in milliseconds.

Source

pub fn root_path(self, path: impl Into<String>) -> Self

Sets the root path for apps behind a reverse proxy.

When the application is served behind a reverse proxy at a sub-path, set this to that sub-path. For example, if the app is proxied at /api/v1, set root_path("/api/v1").

§Example
let config = AppConfig::new().root_path("/api/v1");
Source

pub fn root_path_in_servers(self, include: bool) -> Self

Sets whether to include root_path in OpenAPI servers list.

When true and root_path is set, a server entry with the root_path will be added to the OpenAPI specification’s servers array.

Source

pub fn trailing_slash_mode(self, mode: TrailingSlashMode) -> Self

Sets the trailing slash handling mode.

Controls how the router handles trailing slashes in URLs.

§Example
use fastapi_core::TrailingSlashMode;

let config = AppConfig::new()
    .trailing_slash_mode(TrailingSlashMode::Redirect);
Source

pub fn debug_config(self, config: DebugConfig) -> Self

Sets the debug mode configuration.

Controls whether error responses include additional diagnostic information. Use with DebugConfig::new().enable().with_debug_header(...).

§Example
use fastapi_core::{DebugConfig};

let config = AppConfig::new()
    .debug_config(DebugConfig::new()
        .enable()
        .with_debug_header("X-Debug-Token", "my-secret"));
Source

pub fn from_env() -> Result<Self, ConfigError>

Load configuration from environment variables.

Variables (prefix FASTAPI_ by default):

  • FASTAPI_NAME
  • FASTAPI_VERSION
  • FASTAPI_DEBUG (true/false/1/0/yes/no/on/off)
  • FASTAPI_MAX_BODY_SIZE (bytes)
  • FASTAPI_REQUEST_TIMEOUT_MS
  • FASTAPI_ROOT_PATH (path prefix for reverse proxy)
  • FASTAPI_ROOT_PATH_IN_SERVERS (true/false/1/0/yes/no/on/off)
Source

pub fn from_env_with_prefix(prefix: &str) -> Result<Self, ConfigError>

Load configuration from environment variables using a custom prefix.

Source

pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>

Load configuration from a JSON file.

Only JSON is supported for now to keep dependencies minimal.

Source

pub fn from_env_and_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>

Load configuration from a JSON file then override with environment variables.

Source

pub fn openapi_server(&self) -> Option<(String, Option<String>)>

Returns the root_path as an OpenAPI server entry if configured.

Returns Some((url, description)) if:

  • root_path is non-empty
  • root_path_in_servers is true

Returns None otherwise.

§Example
let config = AppConfig::new().root_path("/api/v1");
if let Some((url, description)) = config.openapi_server() {
    builder = builder.server(url, description);
}
Source

pub fn validate(&self) -> Result<(), ConfigError>

Validate configuration values.

Trait Implementations§

Source§

impl Clone for AppConfig

Source§

fn clone(&self) -> AppConfig

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 AppConfig

Source§

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

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

impl Default for AppConfig

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