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: RequestThe request, to read http method, uri, version, headers and body.
resp: ResponseThe response, to set http status, version, headers and body.
exec: ExecutorThe executor, to spawn futures or blocking works.
remote_addr: SocketAddrSocket addr of last client or proxy.
Implementations§
Source§impl<S> Context<S>
impl<S> Context<S>
Sourcepub fn uri(&self) -> &Uri
pub fn uri(&self) -> &Uri
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(())
}Sourcepub fn method(&self) -> &Method
pub fn method(&self) -> &Method
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(())
}Sourcepub fn get(&self, name: impl AsHeaderName) -> Option<&str>
pub fn get(&self, name: impl AsHeaderName) -> Option<&str>
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(())
}Sourcepub fn must_get(&self, name: impl AsHeaderName) -> Result<&str>
pub fn must_get(&self, name: impl AsHeaderName) -> Result<&str>
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(())
}Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
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(())
}Sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
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(())
}Sourcepub fn store_scoped<SC, K, V>(
&mut self,
scope: SC,
key: K,
value: V,
) -> Option<Arc<V>>
pub fn store_scoped<SC, K, V>( &mut self, scope: SC, key: K, value: V, ) -> Option<Arc<V>>
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);Sourcepub fn store<K, V>(&mut self, key: K, value: V) -> Option<Arc<V>>
pub fn store<K, V>(&mut self, key: K, value: V) -> Option<Arc<V>>
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);Sourcepub fn load_scoped<'a, SC, V>(&self, key: &'a str) -> Option<Variable<'a, V>>where
SC: Any,
V: Value,
pub fn load_scoped<'a, SC, V>(&self, key: &'a str) -> Option<Variable<'a, V>>where
SC: Any,
V: Value,
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);Sourcepub fn load<'a, V>(&self, key: &'a str) -> Option<Variable<'a, V>>where
V: Value,
pub fn load<'a, V>(&self, key: &'a str) -> Option<Variable<'a, V>>where
V: Value,
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 = ()> !Freeze for Context<S>
impl<S = ()> !RefUnwindSafe for Context<S>
impl<S> Send for Context<S>where
S: Send,
impl<S> Sync for Context<S>where
S: Sync,
impl<S> Unpin for Context<S>where
S: Unpin,
impl<S = ()> !UnwindSafe for Context<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more