pub struct Context<S = ()> {
pub req: Request,
pub resp: Response,
pub exec: Executor,
pub remote_addr: SocketAddr,
/* private fields */
}
Expand description
A structure to share request, response and other data between middlewares.
Example
use roa_core::{App, Context, Next, Result};
use tracing::info;
use tokio::fs::File;
let app = App::new().gate(gate).end(end);
async fn gate(ctx: &mut Context, next: Next<'_>) -> Result {
info!("{} {}", ctx.method(), ctx.uri());
next.await
}
async fn end(ctx: &mut Context) -> Result {
ctx.resp.write_reader(File::open("assets/welcome.html").await?);
Ok(())
}
Fields
req: Request
The request, to read http method, uri, version, headers and body.
resp: Response
The response, to set http status, version, headers and body.
exec: Executor
The executor, to spawn futures or blocking works.
remote_addr: SocketAddr
Socket addr of last client or proxy.
Implementations
Clone URI.
Example
use roa_core::{App, Context, Result};
let app = App::new().end(get);
async fn get(ctx: &mut Context) -> Result {
assert_eq!("/", ctx.uri().to_string());
Ok(())
}
Clone request::method.
Example
use roa_core::{App, Context, Result};
use roa_core::http::Method;
let app = App::new().end(get);
async fn get(ctx: &mut Context) -> Result {
assert_eq!(Method::GET, ctx.method());
Ok(())
}
Search for a header value and try to get its string reference.
Example
use roa_core::{App, Context, Result};
use roa_core::http::header::CONTENT_TYPE;
let app = App::new().end(get);
async fn get(ctx: &mut Context) -> Result {
assert_eq!(
Some("text/plain"),
ctx.get(CONTENT_TYPE),
);
Ok(())
}
Search for a header value and get its string reference.
Otherwise return a 400 BAD REQUEST.
Example
use roa_core::{App, Context, Result};
use roa_core::http::header::CONTENT_TYPE;
let app = App::new().end(get);
async fn get(ctx: &mut Context) -> Result {
assert_eq!(
"text/plain",
ctx.must_get(CONTENT_TYPE)?,
);
Ok(())
}
Clone response::status.
Example
use roa_core::{App, Context, Result};
use roa_core::http::StatusCode;
let app = App::new().end(get);
async fn get(ctx: &mut Context) -> Result {
assert_eq!(StatusCode::OK, ctx.status());
Ok(())
}
Clone request::version.
Example
use roa_core::{App, Context, Result};
use roa_core::http::Version;
let app = App::new().end(get);
async fn get(ctx: &mut Context) -> Result {
assert_eq!(Version::HTTP_11, ctx.version());
Ok(())
}
Store key-value pair in specific scope.
Example
use roa_core::{App, Context, Result, Next};
struct Scope;
struct AnotherScope;
async fn gate(ctx: &mut Context, next: Next<'_>) -> Result {
ctx.store_scoped(Scope, "id", "1".to_string());
next.await
}
async fn end(ctx: &mut Context) -> Result {
assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?);
assert!(ctx.load_scoped::<AnotherScope, String>("id").is_none());
Ok(())
}
let app = App::new().gate(gate).end(end);
Store key-value pair in public scope.
Example
use roa_core::{App, Context, Result, Next};
async fn gate(ctx: &mut Context, next: Next<'_>) -> Result {
ctx.store("id", "1".to_string());
next.await
}
async fn end(ctx: &mut Context) -> Result {
assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?);
Ok(())
}
let app = App::new().gate(gate).end(end);
Search for value by key in specific scope.
Example
use roa_core::{App, Context, Result, Next};
struct Scope;
async fn gate(ctx: &mut Context, next: Next<'_>) -> Result {
ctx.store_scoped(Scope, "id", "1".to_owned());
next.await
}
async fn end(ctx: &mut Context) -> Result {
assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?);
Ok(())
}
let app = App::new().gate(gate).end(end);
Search for value by key in public scope.
Example
use roa_core::{App, Context, Result, Next};
async fn gate(ctx: &mut Context, next: Next<'_>) -> Result {
ctx.store("id", "1".to_string());
next.await
}
async fn end(ctx: &mut Context) -> Result {
assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?);
Ok(())
}
let app = App::new().gate(gate).end(end);
Trait Implementations
Auto Trait Implementations
impl<S = ()> !RefUnwindSafe for Context<S>
impl<S = ()> !UnwindSafe for Context<S>
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more