Skip to main content

LinoApp

Struct LinoApp 

Source
pub struct LinoApp {
    pub info: ServiceInfo,
    pub cors: Option<CorsPolicy>,
    pub supported: Option<Vec<String>>,
    pub max_body_bytes: usize,
    pub query_defaults: QueryDefaults,
    /* private fields */
}
Expand description

An application that speaks Links Notation.

Fields§

§info: ServiceInfo

Title, version and prose description of the service.

§cors: Option<CorsPolicy>

CORS policy, when cross-origin requests are allowed.

§supported: Option<Vec<String>>

Representations the server may produce; None means all of them.

§max_body_bytes: usize

Largest accepted request body.

§query_defaults: QueryDefaults

Page-size defaults for collection queries.

Implementations§

Source§

impl LinoApp

Source

pub fn new() -> Self

A new application serving its own description.

§Examples
use lino_rest_api::app::{LinoApp, RawRequest};
use lino_rest_api::value::{object, string};

let mut app = LinoApp::new();
app.get_fn("/health", "Health", |_request| {
    Ok(object([("status", string("ok"))]))
});

let response = app.respond(RawRequest::new("GET", "/health")).await;
assert_eq!(response.status, 200);
assert_eq!(response.body, "(\n  status \"ok\"\n)");
Source

pub fn with_info(self, info: ServiceInfo) -> Self

The same application with another title and version.

Source

pub fn with_cors(self, cors: CorsPolicy) -> Self

The same application with a CORS policy.

Source

pub fn with_supported<I: IntoIterator<Item = String>>( self, supported: I, ) -> Self

The same application producing only these representations.

Source

pub fn with_max_body_bytes(self, max_body_bytes: usize) -> Self

The same application refusing request bodies larger than this.

Source

pub fn with_query_defaults(self, query_defaults: QueryDefaults) -> Self

The same application with other collection page-size defaults.

Source

pub fn without_description(self) -> Self

The same application without the description routes.

Source

pub fn route<F, Fut, R>( &mut self, method: &str, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static, R: Into<LinoResult> + Send + 'static,

Register an asynchronous handler for a method and a path.

§Panics

When the method is not one of HTTP_METHODS, which is a mistake in the program rather than in a request.

Source

pub fn route_fn<F, R>( &mut self, method: &str, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static, R: Into<LinoResult> + Send + 'static,

Register a synchronous handler for a method and a path.

Source

pub fn get<F, Fut, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static, R: Into<LinoResult> + Send + 'static,

Register an asynchronous GET handler.

Source

pub fn post<F, Fut, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static, R: Into<LinoResult> + Send + 'static,

Register an asynchronous POST handler.

Source

pub fn put<F, Fut, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static, R: Into<LinoResult> + Send + 'static,

Register an asynchronous PUT handler.

Source

pub fn patch<F, Fut, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static, R: Into<LinoResult> + Send + 'static,

Register an asynchronous PATCH handler.

Source

pub fn delete<F, Fut, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static, R: Into<LinoResult> + Send + 'static,

Register an asynchronous DELETE handler.

Source

pub fn get_fn<F, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static, R: Into<LinoResult> + Send + 'static,

Register a synchronous GET handler.

Source

pub fn post_fn<F, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static, R: Into<LinoResult> + Send + 'static,

Register a synchronous POST handler.

Source

pub fn put_fn<F, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static, R: Into<LinoResult> + Send + 'static,

Register a synchronous PUT handler.

Source

pub fn patch_fn<F, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static, R: Into<LinoResult> + Send + 'static,

Register a synchronous PATCH handler.

Source

pub fn delete_fn<F, R>( &mut self, path: &str, summary: &str, handler: F, ) -> &mut Self
where F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static, R: Into<LinoResult> + Send + 'static,

Register a synchronous DELETE handler.

Source

pub fn resource<S: Store + 'static>( &mut self, path: &str, store: Arc<S>, options: ResourceOptions, ) -> &mut Self

Register the six CRUD routes of a collection backed by a store.

See crate::resource::register_resource for the options.

Source

pub fn describe(&self) -> LinoValue

The native service description of specification section 9.

Source

pub fn openapi(&self) -> LinoValue

The OpenAPI 3.1 rendering of the service description.

Source

pub fn routes(&self) -> &RouteTable

The route table, for inspection.

Source

pub async fn handle( &self, request: LinoRequest, ) -> Result<ResponseParts, LinoHttpError>

Run one decoded request through routing and its handler.

§Errors

A 404 when no route matches, a 405 when the route does not serve the method, and anything the handler reports.

Source

pub async fn respond(&self, raw: RawRequest) -> ResponseParts

Answer one raw request, decoding it and rendering any error as problem details.

This is the whole application in one call: negotiation, body decoding, routing, CORS and problem rendering. No error escapes it.

Trait Implementations§

Source§

impl Default for LinoApp

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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