Skip to main content

APIApp

Struct APIApp 

Source
pub struct APIApp<S = ()>
where S: Clone + Send + Sync + 'static,
{ /* private fields */ }
Expand description

The main application struct for fastrust.

APIApp is the entry point for building a fastrust web application. It provides a builder pattern for configuring the application and automatically generates OpenAPI documentation.

§Type Parameters

  • S - The application state type. Use () for no state.

§Examples

§Basic usage without state

use fastrust::{APIApp, APIRouter, RouteConfig};

#[tokio::main]
async fn main() {
    let mut api = APIRouter::new("/api");
    // Add routes...

    APIApp::new()
        .set_title("My API")
        .set_port(8080)
        .register_router(api)
        .run().await;
}

§With application state

use fastrust::{APIApp, APIRouter, RouteConfig};

#[derive(Clone)]
struct AppState {
    counter: std::sync::Arc<std::sync::Mutex<i32>>,
}

#[tokio::main]
async fn main() {
    let state = AppState {
        counter: std::sync::Arc::new(std::sync::Mutex::new(0)),
    };

    let mut api = APIRouter::new("/api");
    // Add routes...

    APIApp::new_with_state(state)
        .set_title("My API")
        .register_router(api)
        .run().await;
}

§OpenAPI Documentation

By default, the OpenAPI specification is available at /openapi.json and the Swagger UI is available at /docs. These paths can be customized using set_openapi_path and set_docs_path.

Implementations§

Source§

impl APIApp<()>

Source

pub fn new() -> Self

Creates a new APIApp with no state.

§Examples
use fastrust::APIApp;

let app = APIApp::new();
Source§

impl<S> APIApp<S>
where S: Clone + Send + Sync + 'static,

Source

pub fn new_with_state(state: S) -> Self

Creates a new APIApp with the provided state.

§Arguments
  • state - The application state to be shared across handlers
§Examples
use fastrust::APIApp;

#[derive(Clone)]
struct AppState {
    value: i32,
}

let state = AppState { value: 42 };
let app = APIApp::new_with_state(state);
Source

pub fn set_title(self, title: &str) -> Self

Sets the API title.

The title is displayed in the Swagger UI and used in the OpenAPI specification.

§Arguments
  • title - The API title
§Examples
use fastrust::APIApp;

let app = APIApp::new().set_title("My Awesome API");
Source

pub fn set_summary(self, summary: &str) -> Self

Sets the API summary.

A short description of the API, displayed in the Swagger UI.

§Arguments
  • summary - The API summary
Source

pub fn set_description(self, description: &str) -> Self

Sets the API description.

A longer description of the API. Supports Markdown formatting.

§Arguments
  • description - The API description
Source

pub fn set_version(self, version: &str) -> Self

Sets the API version.

§Arguments
  • version - The API version string (e.g., “1.0.0”)
§Examples
use fastrust::APIApp;

let app = APIApp::new().set_version("1.0.0");
Source

pub fn set_openapi_path(self, openapi_path: &str) -> Self

Sets the OpenAPI specification endpoint path.

§Arguments
  • openapi_path - The path for the OpenAPI JSON (default: “/openapi.json”)
§Examples
use fastrust::APIApp;

let app = APIApp::new().set_openapi_path("/api-spec.json");
Source

pub fn set_docs_path(self, docs_path: &str) -> Self

Sets the Swagger UI documentation path.

§Arguments
  • docs_path - The path for the Swagger UI (default: “/docs”)
§Examples
use fastrust::APIApp;

let app = APIApp::new().set_docs_path("/swagger");
Source

pub fn set_host(self, host: &str) -> Self

Sets the server host.

The host is used in the OpenAPI specification to generate the server URL.

§Arguments
  • host - The server host (e.g., “localhost”, “api.example.com”)
Source

pub fn set_port(self, port: i32) -> Self

Sets the server port.

§Arguments
  • port - The server port number (default: 6969)
Source

pub fn register_router(self, router: APIRouter<S>) -> Self

Registers a router with the application.

Multiple routers can be registered. They are processed in the order they are registered.

§Arguments
  • router - An APIRouter containing route definitions
§Examples
use fastrust::{APIApp, APIRouter, RouteConfig};

let mut api = APIRouter::new("/api");
// Add routes to api...

let app = APIApp::new().register_router(api);
Source§

impl<S> APIApp<S>
where S: Clone + Send + Sync + 'static,

Source

pub fn generate_openapi_str(&self) -> String

Generates the OpenAPI 3.0 specification as a JSON string.

This method iterates through all registered routers and their routes, converting them into an OpenAPI 3.0 specification. The generated specification includes:

  • All registered paths and HTTP methods
  • Parameter definitions extracted from handler signatures
  • Request/response schemas when configured via RouteConfig
  • Server information if host/port are set
§Returns

A JSON-formatted string containing the OpenAPI 3.0 specification. Returns an empty string if serialization fails.

§Examples
use fastrust::{APIApp, APIRouter, RouteConfig};

let mut api = APIRouter::new("/api");
// Add routes...

let app = APIApp::new().register_router(api);
let openapi_json = app.generate_openapi_str();
Source§

impl<S> APIApp<S>
where S: Clone + Send + Sync + 'static,

Source

pub fn swagger_html(openapi_path: &str) -> String

Generates the Swagger UI HTML page.

This method creates an HTML page that embeds Swagger UI, configured to load the OpenAPI specification from the given path. The Swagger UI provides an interactive interface for exploring and testing API endpoints.

§Arguments
  • openapi_path - The path to the OpenAPI JSON specification
§Returns

A complete HTML document as a string that includes Swagger UI via CDN and is configured to load the OpenAPI spec from the specified path.

§Examples
use fastrust::APIApp;

let html = APIApp::<()>::swagger_html("/openapi.json");
assert!(html.contains("Swagger UI"));
Source§

impl<S> APIApp<S>
where S: Clone + Send + Sync + 'static,

Source

pub async fn run(self)

Runs the HTTP server and starts listening for requests.

This is the main entry point for starting the fastrust application. It performs the following steps:

  1. Initializes the tracing subscriber for logging
  2. Generates the OpenAPI specification
  3. Registers all user-defined routes
  4. Registers internal routes (OpenAPI JSON and Swagger UI)
  5. Applies middleware (logging, path normalization)
  6. Binds to the configured host and port
  7. Starts serving requests
§Panics

This method will panic if:

  • The host:port address is invalid
  • The server fails to bind to the address
  • The server encounters a fatal error while running
§Examples
use fastrust::{APIApp, APIRouter, RouteConfig};

#[tokio::main]
async fn main() {
    let mut api = APIRouter::new("/api");
    // Add routes...

    APIApp::new()
        .set_title("My API")
        .set_port(8080)
        .register_router(api)
        .run().await;
}
§Default Configuration
  • Host: 127.0.0.1
  • Port: 6969
  • OpenAPI path: /openapi.json
  • Docs path: /docs

Trait Implementations§

Source§

impl<S> Debug for APIApp<S>
where S: Clone + Send + Sync + 'static + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for APIApp<()>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<S> Freeze for APIApp<S>
where S: Freeze,

§

impl<S = ()> !RefUnwindSafe for APIApp<S>

§

impl<S> Send for APIApp<S>

§

impl<S> Sync for APIApp<S>

§

impl<S> Unpin for APIApp<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for APIApp<S>
where S: UnsafeUnpin,

§

impl<S = ()> !UnwindSafe for APIApp<S>

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,