Skip to main content

AFast

Struct AFast 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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"),
    ])
Source

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).

Source

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.

Source

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.

Source

pub async fn run(self) -> Result<(), Error>

Starts all configured servers and runs until Ctrl+C.

Server startup ordering:

  1. Documentation HTML is written to disk if an output directory is set.
  2. Client code is generated for each configured target.
  3. Transport servers (WS, HTTP, TCP) are launched on their respective addresses.
  4. 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.

Trait Implementations§

Source§

impl Default for AFast

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for AFast

§

impl !UnwindSafe for AFast

§

impl Freeze for AFast

§

impl Send for AFast

§

impl Sync for AFast

§

impl Unpin for AFast

§

impl UnsafeUnpin for AFast

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.