pub struct RustApi { /* private fields */ }Expand description
Implementations§
Source§impl RustApi
impl RustApi
Sourcepub fn auto() -> RustApi
pub fn auto() -> RustApi
Create a zero-config RustAPI application.
All routes decorated with #[rustapi::get], #[rustapi::post], etc.
are automatically registered. Swagger UI is enabled at /docs by default.
§Example
use rustapi_rs::prelude::*;
#[rustapi::get("/users")]
async fn list_users() -> Json<Vec<User>> {
Json(vec![])
}
#[rustapi::main]
async fn main() -> Result<()> {
// Zero config - routes are auto-registered!
RustApi::auto()
.run("0.0.0.0:8080")
.await
}Sourcepub fn config() -> RustApiConfig
pub fn config() -> RustApiConfig
Create a configurable RustAPI application with auto-routes.
Provides builder methods for customization while still auto-registering all decorated routes.
§Example
use rustapi_rs::prelude::*;
RustApi::config()
.docs_path("/api-docs")
.body_limit(5 * 1024 * 1024) // 5MB
.openapi_info("My API", "2.0.0", Some("API Description"))
.run("0.0.0.0:8080")
.await?;Sourcepub fn body_limit(self, limit: usize) -> RustApi
pub fn body_limit(self, limit: usize) -> RustApi
Set the global body size limit for request bodies
This protects against denial-of-service attacks via large payloads. The default limit is 1MB (1024 * 1024 bytes).
§Arguments
limit- Maximum body size in bytes
§Example
use rustapi_rs::prelude::*;
RustApi::new()
.body_limit(5 * 1024 * 1024) // 5MB limit
.route("/upload", post(upload_handler))
.run("127.0.0.1:8080")
.awaitSourcepub fn no_body_limit(self) -> RustApi
pub fn no_body_limit(self) -> RustApi
Sourcepub fn layer<L>(self, layer: L) -> RustApiwhere
L: MiddlewareLayer,
pub fn layer<L>(self, layer: L) -> RustApiwhere
L: MiddlewareLayer,
Add a middleware layer to the application
Layers are executed in the order they are added (outermost first). The first layer added will be the first to process the request and the last to process the response.
§Example
use rustapi_rs::prelude::*;
use rustapi_core::middleware::{RequestIdLayer, TracingLayer};
RustApi::new()
.layer(RequestIdLayer::new()) // First to process request
.layer(TracingLayer::new()) // Second to process request
.route("/", get(handler))
.run("127.0.0.1:8080")
.awaitSourcepub fn register_schema<T>(self) -> RustApiwhere
T: for<'a> ToSchema<'a>,
pub fn register_schema<T>(self) -> RustApiwhere
T: for<'a> ToSchema<'a>,
Sourcepub fn openapi_info(
self,
title: &str,
version: &str,
description: Option<&str>,
) -> RustApi
pub fn openapi_info( self, title: &str, version: &str, description: Option<&str>, ) -> RustApi
Configure OpenAPI info (title, version, description)
Sourcepub fn openapi_spec(&self) -> &OpenApiSpec
pub fn openapi_spec(&self) -> &OpenApiSpec
Get the current OpenAPI spec (for advanced usage/testing).
Sourcepub fn route(self, path: &str, method_router: MethodRouter) -> RustApi
pub fn route(self, path: &str, method_router: MethodRouter) -> RustApi
Sourcepub fn mount(self, path: &str, method_router: MethodRouter) -> RustApi
👎Deprecated: Use route() directly or mount_route() for macro-based routing
pub fn mount(self, path: &str, method_router: MethodRouter) -> RustApi
Mount a handler (convenience method)
Alias for .route(path, method_router) for a single handler.
Sourcepub fn mount_route(self, route: Route) -> RustApi
pub fn mount_route(self, route: Route) -> RustApi
Sourcepub fn nest(self, prefix: &str, router: Router) -> RustApi
pub fn nest(self, prefix: &str, router: Router) -> RustApi
Nest a router under a prefix
All routes from the nested router will be registered with the prefix prepended to their paths. OpenAPI operations from the nested router are also propagated to the parent’s OpenAPI spec with prefixed paths.
§Example
let api_v1 = Router::new()
.route("/users", get(list_users));
RustApi::new()
.nest("/api/v1", api_v1)Sourcepub fn serve_static(self, prefix: &str, root: impl Into<PathBuf>) -> RustApi
pub fn serve_static(self, prefix: &str, root: impl Into<PathBuf>) -> RustApi
Serve static files from a directory
Maps a URL path prefix to a filesystem directory. Requests to paths under the prefix will serve files from the corresponding location in the directory.
§Arguments
prefix- URL path prefix (e.g., “/static”, “/assets”)root- Filesystem directory path
§Features
- Automatic MIME type detection
- ETag and Last-Modified headers for caching
- Index file serving for directories
- Path traversal prevention
§Example
use rustapi_rs::prelude::*;
RustApi::new()
.serve_static("/assets", "./public")
.serve_static("/uploads", "./uploads")
.run("127.0.0.1:8080")
.awaitSourcepub fn serve_static_with_config(self, config: StaticFileConfig) -> RustApi
pub fn serve_static_with_config(self, config: StaticFileConfig) -> RustApi
Serve static files with custom configuration
§Example
use rustapi_core::static_files::StaticFileConfig;
let config = StaticFileConfig::new("./public", "/assets")
.max_age(86400) // Cache for 1 day
.fallback("index.html"); // SPA fallback
RustApi::new()
.serve_static_with_config(config)
.run("127.0.0.1:8080")
.awaitSourcepub fn docs(self, path: &str) -> RustApi
pub fn docs(self, path: &str) -> RustApi
Enable Swagger UI documentation
This adds two endpoints:
{path}- Swagger UI interface{path}/openapi.json- OpenAPI JSON specification
§Example
RustApi::new()
.route("/users", get(list_users))
.docs("/docs") // Swagger UI at /docs, spec at /docs/openapi.json
.run("127.0.0.1:8080")
.awaitSourcepub fn docs_with_info(
self,
path: &str,
title: &str,
version: &str,
description: Option<&str>,
) -> RustApi
pub fn docs_with_info( self, path: &str, title: &str, version: &str, description: Option<&str>, ) -> RustApi
Sourcepub fn docs_with_auth(
self,
path: &str,
username: &str,
password: &str,
) -> RustApi
pub fn docs_with_auth( self, path: &str, username: &str, password: &str, ) -> RustApi
Enable Swagger UI documentation with Basic Auth protection
When username and password are provided, the docs endpoint will require Basic Authentication. This is useful for protecting API documentation in production environments.
§Example
RustApi::new()
.route("/users", get(list_users))
.docs_with_auth("/docs", "admin", "secret123")
.run("127.0.0.1:8080")
.awaitSourcepub fn docs_with_auth_and_info(
self,
path: &str,
username: &str,
password: &str,
title: &str,
version: &str,
description: Option<&str>,
) -> RustApi
pub fn docs_with_auth_and_info( self, path: &str, username: &str, password: &str, title: &str, version: &str, description: Option<&str>, ) -> RustApi
Sourcepub fn into_router(self) -> Router
pub fn into_router(self) -> Router
Get the inner router (for testing or advanced usage)
Sourcepub fn layers(&self) -> &LayerStack
pub fn layers(&self) -> &LayerStack
Get the layer stack (for testing)