pub struct APIApp<S = ()>{ /* 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<S> APIApp<S>
impl<S> APIApp<S>
Sourcepub fn new_with_state(state: S) -> Self
pub fn new_with_state(state: S) -> Self
Sourcepub fn set_summary(self, summary: &str) -> Self
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
Sourcepub fn set_description(self, description: &str) -> Self
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
Sourcepub fn set_version(self, version: &str) -> Self
pub fn set_version(self, version: &str) -> Self
Sourcepub fn set_openapi_path(self, openapi_path: &str) -> Self
pub fn set_openapi_path(self, openapi_path: &str) -> Self
Sourcepub fn set_docs_path(self, docs_path: &str) -> Self
pub fn set_docs_path(self, docs_path: &str) -> Self
Sourcepub fn set_host(self, host: &str) -> Self
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”)
Sourcepub fn register_router(self, router: APIRouter<S>) -> Self
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- AnAPIRoutercontaining 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>
impl<S> APIApp<S>
Sourcepub fn generate_openapi_str(&self) -> String
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>
impl<S> APIApp<S>
Sourcepub fn swagger_html(openapi_path: &str) -> String
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>
impl<S> APIApp<S>
Sourcepub async fn run(self)
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:
- Initializes the tracing subscriber for logging
- Generates the OpenAPI specification
- Registers all user-defined routes
- Registers internal routes (OpenAPI JSON and Swagger UI)
- Applies middleware (logging, path normalization)
- Binds to the configured host and port
- 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