[][src]Struct roa_core::Context

pub struct Context<S>(_);

A structure to share request, response and other data between middlewares.

Type of the first parameter in a middleware.

Example

use roa_core::App;
use log::info;
use async_std::fs::File;

let mut app = App::new(());
app.gate_fn(|ctx, next| async move {
    info!("{} {}", ctx.method(), ctx.uri());
    next.await
});
app.end(|mut ctx| async move {
    ctx.resp_mut().write(File::open("assets/welcome.html").await?);
    Ok(())
});

Methods

impl<S> Context<S>[src]

pub fn req(&self) -> &Request[src]

Get an immutable reference of request.

Example

use roa_core::App;
use roa_core::http::Method;

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!(Method::GET, ctx.req().method);
    Ok(())
});

pub fn resp(&self) -> &Response[src]

Get an immutable reference of response.

Example

use roa_core::App;
use roa_core::http::StatusCode;

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!(StatusCode::OK, ctx.resp().status);
    Ok(())
});

pub fn req_mut(&mut self) -> &mut Request[src]

Get a mutable reference of request.

Example

use roa_core::App;
use roa_core::http::Method;

let mut app = App::new(());
app.gate_fn(|mut ctx, next| async move {
    ctx.req_mut().method = Method::POST;
    next.await
});
app.end(|ctx| async move {
    assert_eq!(Method::POST, ctx.req().method);
    Ok(())
});

pub fn resp_mut(&mut self) -> &mut Response[src]

Get a mutable reference of response.

Example

use roa_core::App;

let mut app = App::new(());
app.end(|mut ctx| async move {
    ctx.resp_mut().write(b"Hello, World!".as_ref());
    Ok(())
});

pub fn uri(&self) -> &Uri[src]

Clone URI.

Example

use roa_core::App;

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!("/", ctx.uri().to_string());
    Ok(())
});

pub fn method(&self) -> &Method[src]

Clone request::method.

Example

use roa_core::App;
use http::Method;

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!(Method::GET, ctx.method());
    Ok(())
});

pub fn header(
    &self,
    name: impl AsHeaderName
) -> Option<Result<&str, ToStrError>>
[src]

Search for a header value and try to get its string copy.

Example

use roa_core::App;
use roa_core::http::{StatusCode, header};

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!(
        "text/plain",
        ctx.header(&header::CONTENT_TYPE).unwrap().unwrap()
    );
    Ok(())
});

pub fn status(&self) -> StatusCode[src]

Clone response::status.

Example

use roa_core::{App, http::StatusCode};

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!(StatusCode::OK, ctx.status());
    Ok(())
});

pub fn version(&self) -> Version[src]

Clone request::version.

Example

use roa_core::App;
use roa_core::http::{StatusCode, Version};

let mut app = App::new(());
app.end(|ctx| async move {
    assert_eq!(Version::HTTP_11, ctx.version());
    Ok(())
});

Methods from Deref<Target = SyncContext<S>>

pub fn store_scoped<'a, SC, T>(
    &mut self,
    _scope: SC,
    name: &'a str,
    value: T
) -> Option<Variable<'a, T>> where
    SC: Any,
    T: Any + Send + Sync
[src]

Store key-value pair in specific scope.

Example

use roa_core::App;
use roa_core::http::{StatusCode, Method};

struct Scope;
struct AnotherScope;

let mut app = App::new(());
app.gate_fn(|mut ctx, next| async move {
    ctx.store_scoped(Scope, "id", "1".to_string());
    next.await
});
app.end(|ctx| async move {
    assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?);
    assert!(ctx.load_scoped::<AnotherScope, String>("id").is_none());
    Ok(())
});

pub fn store<'a, T>(
    &mut self,
    name: &'a str,
    value: T
) -> Option<Variable<'a, T>> where
    T: Any + Send + Sync
[src]

Store key-value pair in public scope.

Example

use roa_core::App;
use roa_core::http::{StatusCode, Method};

let mut app = App::new(());
app.gate_fn(|mut ctx, next| async move {
    ctx.store("id", "1".to_string());
    next.await
});
app.end(|ctx| async move {
    assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?);
    Ok(())
});

pub fn load_scoped<'a, SC, T>(&self, name: &'a str) -> Option<Variable<'a, T>> where
    SC: Any,
    T: Any + Send + Sync
[src]

Search for value by key in specific scope.

Example

use roa_core::App;

struct Scope;

let mut app = App::new(());
app.gate_fn(|mut ctx, next| async move {
    ctx.store_scoped(Scope, "id", "1".to_owned());
    next.await
});
app.end(|ctx| async move {
    assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?);
    Ok(())
});

pub fn load<'a, T>(&self, name: &'a str) -> Option<Variable<'a, T>> where
    T: Any + Send + Sync
[src]

Search for value by key in public scope.

Example

use roa_core::App;

let mut app = App::new(());
app.gate_fn(|mut ctx, next| async move {
    ctx.store("id", "1".to_string());
    next.await
});
app.end(|ctx| async move {
    assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?);
    Ok(())
});

Trait Implementations

impl<S> Deref for Context<S>[src]

type Target = SyncContext<S>

The resulting type after dereferencing.

impl<S> DerefMut for Context<S>[src]

Auto Trait Implementations

impl<S> !RefUnwindSafe for Context<S>

impl<S> !Send for Context<S>

impl<S> !Sync for Context<S>

impl<S> Unpin for Context<S>

impl<S> !UnwindSafe for Context<S>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.