RustApi

Struct RustApi 

Source
pub struct RustApi { /* private fields */ }
Expand description

Main application builder for RustAPI

§Example

use rustapi_rs::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    RustApi::new()
        .state(AppState::new())
        .route("/", get(hello))
        .route("/users/{id}", get(get_user))
        .run("127.0.0.1:8080")
        .await
}

Implementations§

Source§

impl RustApi

Source

pub fn new() -> Self

Create a new RustAPI application

Source

pub fn state<S>(self, _state: S) -> Self
where S: Clone + Send + Sync + 'static,

Add application state

State is shared across all handlers and can be extracted using State<T>.

§Example
#[derive(Clone)]
struct AppState {
    db: DbPool,
}

RustApi::new()
    .state(AppState::new())
Source

pub fn register_schema<T: for<'a> Schema<'a>>(self) -> Self

Register an OpenAPI schema

§Example
#[derive(Schema)]
struct User { ... }

RustApi::new()
    .register_schema::<User>()
Source

pub fn openapi_info( self, title: &str, version: &str, description: Option<&str>, ) -> Self

Configure OpenAPI info (title, version, description)

Source

pub fn route(self, path: &str, method_router: MethodRouter) -> Self

Add a route

§Example
RustApi::new()
    .route("/", get(index))
    .route("/users", get(list_users).post(create_user))
    .route("/users/{id}", get(get_user).delete(delete_user))
Source

pub fn mount(self, path: &str, method_router: MethodRouter) -> Self

👎Deprecated: Use route() directly or mount_route() for macro-based routing

Mount a handler (convenience method)

Alias for .route(path, method_router) for a single handler.

Source

pub fn mount_route(self, route: Route) -> Self

Mount a route created with #[rustapi::get], #[rustapi::post], etc.

§Example
use rustapi_rs::prelude::*;

#[rustapi::get("/users")]
async fn list_users() -> Json<Vec<User>> {
    Json(vec![])
}

RustApi::new()
    .mount_route(route!(list_users))
    .run("127.0.0.1:8080")
    .await
Source

pub fn nest(self, prefix: &str, router: Router) -> Self

Nest a router under a prefix

§Example
let api_v1 = Router::new()
    .route("/users", get(list_users));

RustApi::new()
    .nest("/api/v1", api_v1)
Source

pub fn docs(self, path: &str) -> Self

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")
    .await
Source

pub fn docs_with_info( self, path: &str, title: &str, version: &str, description: Option<&str>, ) -> Self

Enable Swagger UI documentation with custom API info

§Example
RustApi::new()
    .docs_with_info("/docs", "My API", "2.0.0", Some("API for managing users"))
Source

pub async fn run(self, addr: &str) -> Result<(), Box<dyn Error + Send + Sync>>

Run the server

§Example
RustApi::new()
    .route("/", get(hello))
    .run("127.0.0.1:8080")
    .await
Source

pub fn into_router(self) -> Router

Get the inner router (for testing or advanced usage)

Trait Implementations§

Source§

impl Default for RustApi

Source§

fn default() -> Self

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

Auto Trait Implementations§

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