Introduction
Core components of Roa framework.
If you are new to roa, please go to the documentation of roa framework.
Application
A Roa application is a structure composing and executing middlewares and an endpoint in a stack-like manner.
The obligatory hello world application:
use App;
let app = new.end;
Endpoint
An endpoint is a request handler.
There are some build-in endpoints in roa_core.
-
Functional endpoint
A normal functional endpoint is an async function with signature:
async fn(&mut Context) -> Result
.use ; async let app = new.end;
-
Ok endpoint
()
is an endpoint always returnOk(())
let app = new.end;
-
Status endpoint
Status
is an endpoint always returnErr(Status)
use ; use StatusCode; let app = new.end;
-
String endpoint
Write string to body.
use App; let app = new.end; // static slice let app = new.end; // string
-
Redirect endpoint
Redirect to an uri.
use App; use Uri; let app = new.end;
Cascading
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 endpoint. When a middleware invokes next.await
the function suspends and passes control to the next middleware or endpoint. After the endpoint is called,
the stack will unwind and each middleware is resumed to perform
its upstream behaviour.
use ;
use Instant;
use info;
let app = new.gate.end;
async
Status Handling
You can catch or straightly throw a status returned by next.
use ;
use StatusCode;
let app = new.gate.gate.end;
async
async
async
status_handler
App has an status_handler to handle Status
thrown by the top middleware.
This is the status_handler:
use ;
HTTP Server.
Use roa_core::accept
to construct a http server.
Please refer to roa::tcp
for more information.