pub struct AFast { /* private fields */ }Expand description
The top-level application builder and runtime.
AFast configures shared state, services, and transport servers, then
starts the application via run. All configuration follows
the builder pattern.
§Example
let app = AFast::new()
.state(AppState { db_url: "localhost".into() })
.service(api_svc)
.http("0.0.0.0:5000");
app.run().await.unwrap();Implementations§
Source§impl AFast
impl AFast
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty application with no state, services, or transport
bindings configured. Use the builder methods to add these before
calling run.
Sourcepub fn marker(self, marker: &str) -> Self
pub fn marker(self, marker: &str) -> Self
Sets the marker string used for conditional serialization.
The marker is stored in the application and passed to handlers via
Arc<String> in the StateMap. It controls which
#[afast(skip_with("marker"))] fields are skipped during
serialization/deserialization.
Defaults to "afast" if never called.
Sourcepub fn body_size_limit(self, limit: usize) -> Self
pub fn body_size_limit(self, limit: usize) -> Self
Sets the maximum request/message body size in bytes.
This limit applies to:
- HTTP binary API (
POST /_api) request bodies - Ordinary HTTP handler body reads (
Body<T>extractor) - WebSocket binary frame sizes (both standalone and HTTP-upgraded)
- TCP binary frame sizes
Defaults to 10 MB (10 * 1024 * 1024). The check happens during data reading, so oversized payloads are rejected before full allocation.
Sourcepub fn max_connections(self, max: usize) -> Self
pub fn max_connections(self, max: usize) -> Self
Sets the maximum number of concurrent connections.
When the limit is reached, new connections wait on a semaphore until an existing connection closes. This prevents file descriptor and memory exhaustion from connection floods.
Applies to HTTP, WebSocket, and TCP servers. Defaults to 10 000.
Sourcepub fn request_timeout(self, secs: u64) -> Self
pub fn request_timeout(self, secs: u64) -> Self
Sets the HTTP connection timeout in seconds.
Connections that exceed this duration are silently closed by the server. This prevents slowloris-style attacks and frees resources held by idle connections.
Defaults to 30 seconds. Set to 0 to disable the timeout.
Sourcepub fn sanitize_errors(self, sanitize: bool) -> Self
pub fn sanitize_errors(self, sanitize: bool) -> Self
Controls whether system-level error messages are sanitized before being sent to clients.
When true (default), errors from I/O, serialization, state lookup,
and transport layers are replaced with generic messages. User-defined
errors (Custom, Handler, InvalidParam) are always returned as-is.
Set to false for development/debugging to see full error details.
Sourcepub fn security_headers(
self,
headers: Vec<(&'static str, &'static str)>,
) -> Self
pub fn security_headers( self, headers: Vec<(&'static str, &'static str)>, ) -> Self
Sets custom security headers for HTTP responses.
Replaces the default security headers. Pass an empty Vec to
disable all security headers.
§Example
AFast::new()
.security_headers(vec![
("x-content-type-options", "nosniff"),
("x-frame-options", "SAMEORIGIN"),
])Sourcepub fn doc_code_rate_limit_policy(self, policy: impl Into<String>) -> Self
pub fn doc_code_rate_limit_policy(self, policy: impl Into<String>) -> Self
Sets the rate-limit policy for /code and /doc endpoints.
The policy name must match an existing policy in the
RateLimitConfig registered via
rate_limit. Only effective when the rate-limit
feature is enabled.
Defaults to None (no rate limiting on code/doc).
Sourcepub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
Registers a shared application state value.
Each Rust type may have at most one value in the state map. A later
call with the same type replaces the previous value. Handlers access
state through the State<T> extractor.
Sourcepub fn service(self, service: Service) -> Self
pub fn service(self, service: Service) -> Self
Registers a service and its handlers with the application.
Handler offsets are assigned automatically, continuing from the highest offset of previously registered services. Ordinary HTTP routes are extracted and stored separately for the HTTP server.
If a service with the same name already exists (and the name is non-empty), the new service’s handlers and ordinary routes are merged into the existing one instead of adding a duplicate.
Sourcepub async fn run(self) -> Result<(), Error>
pub async fn run(self) -> Result<(), Error>
Starts all configured servers and runs until Ctrl+C.
Server startup ordering:
- Documentation HTML is written to disk if an output directory is set.
- Client code is generated for each configured target.
- Transport servers (WS, HTTP, TCP) are launched on their respective addresses.
- The main task blocks on
ctrl_c(). On shutdown, a signal is broadcast to all servers, which stop accepting new connections.
If no transport server is configured (code generation only), the application waits for Ctrl+C to keep the process alive.