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
| Setting | Default |
|---|---|
name | "FastAPI" |
version | "0.1.0" |
debug | false |
max_body_size | 1 MB (1,048,576 bytes) |
request_timeout_ms | 30,000 ms |
root_path | "" |
trailing_slash_mode | Strict |
§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 MBFields§
§name: StringApplication name (used in logging and OpenAPI).
version: StringApplication version.
debug: boolEnable debug mode.
max_body_size: usizeMaximum request body size in bytes.
request_timeout_ms: u64Default request timeout in milliseconds.
root_path: StringRoot 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_serversis true)
root_path_in_servers: boolWhether 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: TrailingSlashModeTrailing slash handling mode.
Controls how the router handles trailing slashes in URLs:
Strict(default):/usersand/users/are different routesRedirect: 308 redirect/users/to/usersRedirectWithSlash: 308 redirect/usersto/users/MatchBoth: accept both forms without redirect
debug_config: DebugConfigDebug 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
impl AppConfig
Sourcepub fn max_body_size(self, size: usize) -> Self
pub fn max_body_size(self, size: usize) -> Self
Sets the maximum request body size.
Sourcepub fn request_timeout_ms(self, timeout: u64) -> Self
pub fn request_timeout_ms(self, timeout: u64) -> Self
Sets the default request timeout in milliseconds.
Sourcepub fn root_path_in_servers(self, include: bool) -> Self
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.
Sourcepub fn trailing_slash_mode(self, mode: TrailingSlashMode) -> Self
pub fn trailing_slash_mode(self, mode: TrailingSlashMode) -> Self
Sourcepub fn debug_config(self, config: DebugConfig) -> Self
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"));Sourcepub fn from_env() -> Result<Self, ConfigError>
pub fn from_env() -> Result<Self, ConfigError>
Load configuration from environment variables.
Variables (prefix FASTAPI_ by default):
FASTAPI_NAMEFASTAPI_VERSIONFASTAPI_DEBUG(true/false/1/0/yes/no/on/off)FASTAPI_MAX_BODY_SIZE(bytes)FASTAPI_REQUEST_TIMEOUT_MSFASTAPI_ROOT_PATH(path prefix for reverse proxy)FASTAPI_ROOT_PATH_IN_SERVERS(true/false/1/0/yes/no/on/off)
Sourcepub fn from_env_with_prefix(prefix: &str) -> Result<Self, ConfigError>
pub fn from_env_with_prefix(prefix: &str) -> Result<Self, ConfigError>
Load configuration from environment variables using a custom prefix.
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
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.
Sourcepub fn from_env_and_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
pub fn from_env_and_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
Load configuration from a JSON file then override with environment variables.
Sourcepub fn openapi_server(&self) -> Option<(String, Option<String>)>
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_pathis non-emptyroot_path_in_serversis 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);
}Sourcepub fn validate(&self) -> Result<(), ConfigError>
pub fn validate(&self) -> Result<(), ConfigError>
Validate configuration values.