Roa is an async web framework inspired by koajs, lightweight but powerful.
A Roa application is a structure containing a middleware group which composes and executes middleware functions in a stack-like manner. The obligatory hello world application:
```rust use roa_core::App; use log::info; use std::error::Error as StdError;
async fn main() -> Result<(), Box
The following example responds with "Hello World", however, the request flows through
the logging
middleware to mark when the request started, then continue
to yield control through the response middleware. When a middleware invokes next().await
the function suspends and passes control to the next middleware defined. After there are no more
middleware to execute downstream, the stack will unwind and each middleware is resumed to perform
its upstream behaviour.
```rust use roa_core::App; use log::info; use std::error::Error as StdError; use std::time::Instant;
async fn main() -> Result<(), Box
You can catch or straightly throw an Error returned by next.
```rust use roacore::{App, throw}; use asyncstd::task::spawn; use http::StatusCode;
async fn main() -> Result<(), Box
App has an errorhandler to handle Error
thrown by the top middleware.
This is the errorhandler:
rust
use roa_core::{Context, Error, Result, Model, ErrorKind};
pub async fn error_handler<M: Model>(context: Context<M>, err: Error) -> Result {
context.resp_mut().await.status = err.status_code;
if err.expose {
context.resp_mut().await.write_str(&err.message);
}
if err.kind == ErrorKind::ServerError {
Err(err)
} else {
Ok(())
}
}
The Error thrown by this error_handler will be handled by hyper.
Use hyper::Server::builder
to construct a hyper server with your custom runtime.
```rust use roa_core::{App, Server, AddrIncoming, Executor}; use std::future::Future; /// An implementation of hyper::rt::Executor based on tokio
pub struct Exec;
impl
async fn main() -> Result<(), Box