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: ServiceInfoTitle, 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: usizeLargest accepted request body.
query_defaults: QueryDefaultsPage-size defaults for collection queries.
Implementations§
Source§impl LinoApp
impl LinoApp
Sourcepub fn new() -> Self
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)");Sourcepub fn with_info(self, info: ServiceInfo) -> Self
pub fn with_info(self, info: ServiceInfo) -> Self
The same application with another title and version.
Sourcepub fn with_cors(self, cors: CorsPolicy) -> Self
pub fn with_cors(self, cors: CorsPolicy) -> Self
The same application with a CORS policy.
Sourcepub fn with_supported<I: IntoIterator<Item = String>>(
self,
supported: I,
) -> Self
pub fn with_supported<I: IntoIterator<Item = String>>( self, supported: I, ) -> Self
The same application producing only these representations.
Sourcepub fn with_max_body_bytes(self, max_body_bytes: usize) -> Self
pub fn with_max_body_bytes(self, max_body_bytes: usize) -> Self
The same application refusing request bodies larger than this.
Sourcepub fn with_query_defaults(self, query_defaults: QueryDefaults) -> Self
pub fn with_query_defaults(self, query_defaults: QueryDefaults) -> Self
The same application with other collection page-size defaults.
Sourcepub fn without_description(self) -> Self
pub fn without_description(self) -> Self
The same application without the description routes.
Sourcepub fn route<F, Fut, R>(
&mut self,
method: &str,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn route<F, Fut, R>(
&mut self,
method: &str,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
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.
Sourcepub fn route_fn<F, R>(
&mut self,
method: &str,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn route_fn<F, R>(
&mut self,
method: &str,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
Register a synchronous handler for a method and a path.
Sourcepub fn get<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn get<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
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.
Sourcepub fn post<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn post<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
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.
Sourcepub fn put<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn put<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
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.
Sourcepub fn patch<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn patch<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
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.
Sourcepub fn delete<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, LinoHttpError>> + Send + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn delete<F, Fut, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
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.
Sourcepub fn get_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn get_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
Register a synchronous GET handler.
Sourcepub fn post_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn post_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
Register a synchronous POST handler.
Sourcepub fn put_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn put_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
Register a synchronous PUT handler.
Sourcepub fn patch_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn patch_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
Register a synchronous PATCH handler.
Sourcepub fn delete_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
pub fn delete_fn<F, R>(
&mut self,
path: &str,
summary: &str,
handler: F,
) -> &mut Selfwhere
F: Fn(LinoRequest) -> Result<R, LinoHttpError> + Send + Sync + 'static,
R: Into<LinoResult> + Send + 'static,
Register a synchronous DELETE handler.
Sourcepub fn resource<S: Store + 'static>(
&mut self,
path: &str,
store: Arc<S>,
options: ResourceOptions,
) -> &mut Self
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.
Sourcepub fn routes(&self) -> &RouteTable
pub fn routes(&self) -> &RouteTable
The route table, for inspection.
Sourcepub async fn handle(
&self,
request: LinoRequest,
) -> Result<ResponseParts, LinoHttpError>
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.
Sourcepub async fn respond(&self, raw: RawRequest) -> ResponseParts
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.