Skip to main content

AppServeExt

Trait AppServeExt 

Source
pub trait AppServeExt {
    // Required methods
    fn serve(
        self,
        addr: impl Into<String>,
    ) -> impl Future<Output = Result<(), ServeError>> + Send;
    fn serve_with_config(
        self,
        config: ServerConfig,
    ) -> impl Future<Output = Result<(), ServeError>> + Send;
}
Expand description

Extension trait to add serve capability to App.

This trait provides the serve() method that wires an App to the HTTP server, enabling it to handle incoming HTTP requests.

§Example

use fastapi::prelude::*;
use fastapi_http::AppServeExt;

let app = App::builder()
    .get("/", |_, _| async { Response::ok().body_text("Hello!") })
    .build();

// Run the server
app.serve("0.0.0.0:8080").await?;

Required Methods§

Source

fn serve( self, addr: impl Into<String>, ) -> impl Future<Output = Result<(), ServeError>> + Send

Starts the HTTP server and begins accepting connections.

This method:

  1. Runs all registered startup hooks
  2. Binds to the specified address
  3. Accepts connections and routes requests to handlers
  4. Runs shutdown hooks when the server stops
§Arguments
  • addr - The address to bind to (e.g., “0.0.0.0:8080” or “127.0.0.1:3000”)
§Errors

Returns an error if:

  • A startup hook fails with abort: true
  • Binding to the address fails
  • An unrecoverable IO error occurs
§Example
use fastapi::prelude::*;
use fastapi_http::AppServeExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app = App::builder()
        .get("/health", |_, _| async { Response::ok() })
        .build();

    app.serve("0.0.0.0:8080").await?;
    Ok(())
}
Source

fn serve_with_config( self, config: ServerConfig, ) -> impl Future<Output = Result<(), ServeError>> + Send

Starts the HTTP server with custom configuration.

This method allows fine-grained control over server behavior including timeouts, connection limits, and keep-alive settings.

§Arguments
  • config - Server configuration options
§Example
use fastapi::prelude::*;
use fastapi_http::{AppServeExt, ServerConfig};

let config = ServerConfig::new("0.0.0.0:8080")
    .with_request_timeout_secs(60)
    .with_max_connections(1000)
    .with_keep_alive_timeout_secs(120);

app.serve_with_config(config).await?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AppServeExt for App

Source§

fn serve( self, addr: impl Into<String>, ) -> impl Future<Output = Result<(), ServeError>> + Send

Source§

fn serve_with_config( self, config: ServerConfig, ) -> impl Future<Output = Result<(), ServeError>> + Send

Implementors§