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 new() -> AppConfig
pub fn new() -> AppConfig
Creates a new configuration with defaults.
Examples found in repository?
24fn main() {
25 println!("Getting Started Guide - Code Validation\n");
26
27 // === Basic App Example ===
28 println!("1. Basic app with two routes:");
29 let app = App::builder()
30 .get("/", hello)
31 .get("/health", health)
32 .build();
33
34 println!(" Routes: {}", app.route_count());
35 let client = TestClient::new(app);
36
37 let response = client.get("/").send();
38 println!(
39 " GET / -> {} ({})",
40 response.status().as_u16(),
41 response.text()
42 );
43 assert_eq!(response.status().as_u16(), 200);
44 assert_eq!(response.text(), "Hello, World!");
45
46 let response = client.get("/health").send();
47 println!(
48 " GET /health -> {} ({})",
49 response.status().as_u16(),
50 response.text()
51 );
52 assert_eq!(response.status().as_u16(), 200);
53
54 // === App with Middleware ===
55 println!("\n2. App with middleware:");
56 let app = App::builder()
57 .middleware(RequestIdMiddleware::new())
58 .middleware(SecurityHeaders::new())
59 .get("/", hello)
60 .build();
61
62 let client = TestClient::new(app);
63 let response = client.get("/").send();
64 println!(" GET / -> {}", response.status().as_u16());
65 assert_eq!(response.status().as_u16(), 200);
66
67 // === App with Configuration ===
68 println!("\n3. App with configuration:");
69 let config = AppConfig::new()
70 .name("My API")
71 .version("1.0.0")
72 .debug(true)
73 .max_body_size(10 * 1024 * 1024)
74 .request_timeout_ms(30_000);
75
76 let app = App::builder().config(config).get("/", hello).build();
77
78 println!(" App name: {}", app.config().name);
79 println!(" Version: {}", app.config().version);
80 assert_eq!(app.config().name, "My API");
81 assert_eq!(app.config().version, "1.0.0");
82
83 // === 404 for unknown routes ===
84 println!("\n4. 404 for unknown routes:");
85 let app = App::builder().get("/", hello).build();
86
87 let client = TestClient::new(app);
88 let response = client.get("/nonexistent").send();
89 println!(" GET /nonexistent -> {}", response.status().as_u16());
90 assert_eq!(response.status().as_u16(), 404);
91
92 println!("\nAll getting started examples validated successfully!");
93}Sourcepub fn name(self, name: impl Into<String>) -> AppConfig
pub fn name(self, name: impl Into<String>) -> AppConfig
Sets the application name.
Examples found in repository?
24fn main() {
25 println!("Getting Started Guide - Code Validation\n");
26
27 // === Basic App Example ===
28 println!("1. Basic app with two routes:");
29 let app = App::builder()
30 .get("/", hello)
31 .get("/health", health)
32 .build();
33
34 println!(" Routes: {}", app.route_count());
35 let client = TestClient::new(app);
36
37 let response = client.get("/").send();
38 println!(
39 " GET / -> {} ({})",
40 response.status().as_u16(),
41 response.text()
42 );
43 assert_eq!(response.status().as_u16(), 200);
44 assert_eq!(response.text(), "Hello, World!");
45
46 let response = client.get("/health").send();
47 println!(
48 " GET /health -> {} ({})",
49 response.status().as_u16(),
50 response.text()
51 );
52 assert_eq!(response.status().as_u16(), 200);
53
54 // === App with Middleware ===
55 println!("\n2. App with middleware:");
56 let app = App::builder()
57 .middleware(RequestIdMiddleware::new())
58 .middleware(SecurityHeaders::new())
59 .get("/", hello)
60 .build();
61
62 let client = TestClient::new(app);
63 let response = client.get("/").send();
64 println!(" GET / -> {}", response.status().as_u16());
65 assert_eq!(response.status().as_u16(), 200);
66
67 // === App with Configuration ===
68 println!("\n3. App with configuration:");
69 let config = AppConfig::new()
70 .name("My API")
71 .version("1.0.0")
72 .debug(true)
73 .max_body_size(10 * 1024 * 1024)
74 .request_timeout_ms(30_000);
75
76 let app = App::builder().config(config).get("/", hello).build();
77
78 println!(" App name: {}", app.config().name);
79 println!(" Version: {}", app.config().version);
80 assert_eq!(app.config().name, "My API");
81 assert_eq!(app.config().version, "1.0.0");
82
83 // === 404 for unknown routes ===
84 println!("\n4. 404 for unknown routes:");
85 let app = App::builder().get("/", hello).build();
86
87 let client = TestClient::new(app);
88 let response = client.get("/nonexistent").send();
89 println!(" GET /nonexistent -> {}", response.status().as_u16());
90 assert_eq!(response.status().as_u16(), 404);
91
92 println!("\nAll getting started examples validated successfully!");
93}Sourcepub fn version(self, version: impl Into<String>) -> AppConfig
pub fn version(self, version: impl Into<String>) -> AppConfig
Sets the application version.
Examples found in repository?
24fn main() {
25 println!("Getting Started Guide - Code Validation\n");
26
27 // === Basic App Example ===
28 println!("1. Basic app with two routes:");
29 let app = App::builder()
30 .get("/", hello)
31 .get("/health", health)
32 .build();
33
34 println!(" Routes: {}", app.route_count());
35 let client = TestClient::new(app);
36
37 let response = client.get("/").send();
38 println!(
39 " GET / -> {} ({})",
40 response.status().as_u16(),
41 response.text()
42 );
43 assert_eq!(response.status().as_u16(), 200);
44 assert_eq!(response.text(), "Hello, World!");
45
46 let response = client.get("/health").send();
47 println!(
48 " GET /health -> {} ({})",
49 response.status().as_u16(),
50 response.text()
51 );
52 assert_eq!(response.status().as_u16(), 200);
53
54 // === App with Middleware ===
55 println!("\n2. App with middleware:");
56 let app = App::builder()
57 .middleware(RequestIdMiddleware::new())
58 .middleware(SecurityHeaders::new())
59 .get("/", hello)
60 .build();
61
62 let client = TestClient::new(app);
63 let response = client.get("/").send();
64 println!(" GET / -> {}", response.status().as_u16());
65 assert_eq!(response.status().as_u16(), 200);
66
67 // === App with Configuration ===
68 println!("\n3. App with configuration:");
69 let config = AppConfig::new()
70 .name("My API")
71 .version("1.0.0")
72 .debug(true)
73 .max_body_size(10 * 1024 * 1024)
74 .request_timeout_ms(30_000);
75
76 let app = App::builder().config(config).get("/", hello).build();
77
78 println!(" App name: {}", app.config().name);
79 println!(" Version: {}", app.config().version);
80 assert_eq!(app.config().name, "My API");
81 assert_eq!(app.config().version, "1.0.0");
82
83 // === 404 for unknown routes ===
84 println!("\n4. 404 for unknown routes:");
85 let app = App::builder().get("/", hello).build();
86
87 let client = TestClient::new(app);
88 let response = client.get("/nonexistent").send();
89 println!(" GET /nonexistent -> {}", response.status().as_u16());
90 assert_eq!(response.status().as_u16(), 404);
91
92 println!("\nAll getting started examples validated successfully!");
93}Sourcepub fn debug(self, debug: bool) -> AppConfig
pub fn debug(self, debug: bool) -> AppConfig
Enables or disables debug mode.
Examples found in repository?
24fn main() {
25 println!("Getting Started Guide - Code Validation\n");
26
27 // === Basic App Example ===
28 println!("1. Basic app with two routes:");
29 let app = App::builder()
30 .get("/", hello)
31 .get("/health", health)
32 .build();
33
34 println!(" Routes: {}", app.route_count());
35 let client = TestClient::new(app);
36
37 let response = client.get("/").send();
38 println!(
39 " GET / -> {} ({})",
40 response.status().as_u16(),
41 response.text()
42 );
43 assert_eq!(response.status().as_u16(), 200);
44 assert_eq!(response.text(), "Hello, World!");
45
46 let response = client.get("/health").send();
47 println!(
48 " GET /health -> {} ({})",
49 response.status().as_u16(),
50 response.text()
51 );
52 assert_eq!(response.status().as_u16(), 200);
53
54 // === App with Middleware ===
55 println!("\n2. App with middleware:");
56 let app = App::builder()
57 .middleware(RequestIdMiddleware::new())
58 .middleware(SecurityHeaders::new())
59 .get("/", hello)
60 .build();
61
62 let client = TestClient::new(app);
63 let response = client.get("/").send();
64 println!(" GET / -> {}", response.status().as_u16());
65 assert_eq!(response.status().as_u16(), 200);
66
67 // === App with Configuration ===
68 println!("\n3. App with configuration:");
69 let config = AppConfig::new()
70 .name("My API")
71 .version("1.0.0")
72 .debug(true)
73 .max_body_size(10 * 1024 * 1024)
74 .request_timeout_ms(30_000);
75
76 let app = App::builder().config(config).get("/", hello).build();
77
78 println!(" App name: {}", app.config().name);
79 println!(" Version: {}", app.config().version);
80 assert_eq!(app.config().name, "My API");
81 assert_eq!(app.config().version, "1.0.0");
82
83 // === 404 for unknown routes ===
84 println!("\n4. 404 for unknown routes:");
85 let app = App::builder().get("/", hello).build();
86
87 let client = TestClient::new(app);
88 let response = client.get("/nonexistent").send();
89 println!(" GET /nonexistent -> {}", response.status().as_u16());
90 assert_eq!(response.status().as_u16(), 404);
91
92 println!("\nAll getting started examples validated successfully!");
93}Sourcepub fn max_body_size(self, size: usize) -> AppConfig
pub fn max_body_size(self, size: usize) -> AppConfig
Sets the maximum request body size.
Examples found in repository?
24fn main() {
25 println!("Getting Started Guide - Code Validation\n");
26
27 // === Basic App Example ===
28 println!("1. Basic app with two routes:");
29 let app = App::builder()
30 .get("/", hello)
31 .get("/health", health)
32 .build();
33
34 println!(" Routes: {}", app.route_count());
35 let client = TestClient::new(app);
36
37 let response = client.get("/").send();
38 println!(
39 " GET / -> {} ({})",
40 response.status().as_u16(),
41 response.text()
42 );
43 assert_eq!(response.status().as_u16(), 200);
44 assert_eq!(response.text(), "Hello, World!");
45
46 let response = client.get("/health").send();
47 println!(
48 " GET /health -> {} ({})",
49 response.status().as_u16(),
50 response.text()
51 );
52 assert_eq!(response.status().as_u16(), 200);
53
54 // === App with Middleware ===
55 println!("\n2. App with middleware:");
56 let app = App::builder()
57 .middleware(RequestIdMiddleware::new())
58 .middleware(SecurityHeaders::new())
59 .get("/", hello)
60 .build();
61
62 let client = TestClient::new(app);
63 let response = client.get("/").send();
64 println!(" GET / -> {}", response.status().as_u16());
65 assert_eq!(response.status().as_u16(), 200);
66
67 // === App with Configuration ===
68 println!("\n3. App with configuration:");
69 let config = AppConfig::new()
70 .name("My API")
71 .version("1.0.0")
72 .debug(true)
73 .max_body_size(10 * 1024 * 1024)
74 .request_timeout_ms(30_000);
75
76 let app = App::builder().config(config).get("/", hello).build();
77
78 println!(" App name: {}", app.config().name);
79 println!(" Version: {}", app.config().version);
80 assert_eq!(app.config().name, "My API");
81 assert_eq!(app.config().version, "1.0.0");
82
83 // === 404 for unknown routes ===
84 println!("\n4. 404 for unknown routes:");
85 let app = App::builder().get("/", hello).build();
86
87 let client = TestClient::new(app);
88 let response = client.get("/nonexistent").send();
89 println!(" GET /nonexistent -> {}", response.status().as_u16());
90 assert_eq!(response.status().as_u16(), 404);
91
92 println!("\nAll getting started examples validated successfully!");
93}Sourcepub fn request_timeout_ms(self, timeout: u64) -> AppConfig
pub fn request_timeout_ms(self, timeout: u64) -> AppConfig
Sets the default request timeout in milliseconds.
Examples found in repository?
24fn main() {
25 println!("Getting Started Guide - Code Validation\n");
26
27 // === Basic App Example ===
28 println!("1. Basic app with two routes:");
29 let app = App::builder()
30 .get("/", hello)
31 .get("/health", health)
32 .build();
33
34 println!(" Routes: {}", app.route_count());
35 let client = TestClient::new(app);
36
37 let response = client.get("/").send();
38 println!(
39 " GET / -> {} ({})",
40 response.status().as_u16(),
41 response.text()
42 );
43 assert_eq!(response.status().as_u16(), 200);
44 assert_eq!(response.text(), "Hello, World!");
45
46 let response = client.get("/health").send();
47 println!(
48 " GET /health -> {} ({})",
49 response.status().as_u16(),
50 response.text()
51 );
52 assert_eq!(response.status().as_u16(), 200);
53
54 // === App with Middleware ===
55 println!("\n2. App with middleware:");
56 let app = App::builder()
57 .middleware(RequestIdMiddleware::new())
58 .middleware(SecurityHeaders::new())
59 .get("/", hello)
60 .build();
61
62 let client = TestClient::new(app);
63 let response = client.get("/").send();
64 println!(" GET / -> {}", response.status().as_u16());
65 assert_eq!(response.status().as_u16(), 200);
66
67 // === App with Configuration ===
68 println!("\n3. App with configuration:");
69 let config = AppConfig::new()
70 .name("My API")
71 .version("1.0.0")
72 .debug(true)
73 .max_body_size(10 * 1024 * 1024)
74 .request_timeout_ms(30_000);
75
76 let app = App::builder().config(config).get("/", hello).build();
77
78 println!(" App name: {}", app.config().name);
79 println!(" Version: {}", app.config().version);
80 assert_eq!(app.config().name, "My API");
81 assert_eq!(app.config().version, "1.0.0");
82
83 // === 404 for unknown routes ===
84 println!("\n4. 404 for unknown routes:");
85 let app = App::builder().get("/", hello).build();
86
87 let client = TestClient::new(app);
88 let response = client.get("/nonexistent").send();
89 println!(" GET /nonexistent -> {}", response.status().as_u16());
90 assert_eq!(response.status().as_u16(), 404);
91
92 println!("\nAll getting started examples validated successfully!");
93}Sourcepub fn root_path_in_servers(self, include: bool) -> AppConfig
pub fn root_path_in_servers(self, include: bool) -> AppConfig
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) -> AppConfig
pub fn trailing_slash_mode(self, mode: TrailingSlashMode) -> AppConfig
Sourcepub fn debug_config(self, config: DebugConfig) -> AppConfig
pub fn debug_config(self, config: DebugConfig) -> AppConfig
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<AppConfig, ConfigError>
pub fn from_env() -> Result<AppConfig, 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<AppConfig, ConfigError>
pub fn from_env_with_prefix(prefix: &str) -> Result<AppConfig, ConfigError>
Load configuration from environment variables using a custom prefix.
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<AppConfig, ConfigError>
pub fn from_file(path: impl AsRef<Path>) -> Result<AppConfig, 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<AppConfig, ConfigError>
pub fn from_env_and_file( path: impl AsRef<Path>, ) -> Result<AppConfig, 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.