[][src]Struct roa_core::Context

pub struct Context<S = ()> {
    pub req: Request,
    pub resp: Response,
    pub exec: Executor,
    pub remote_addr: SocketAddr,
    // some fields omitted
}

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

Example

use roa_core::{App, Context, Next, Result};
use log::info;
use async_std::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.

Methods

impl<S> Context<S>[src]

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

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(())
}

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

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(())
}

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

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(())
}

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

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(())
}

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

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(())
}

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

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(())
}

pub fn store_scoped<SC, K, V>(
    &mut self,
    scope: SC,
    key: K,
    value: V
) -> Option<Arc<V>> where
    SC: Any,
    K: Into<Cow<'static, str>>,
    V: Value, 
[src]

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);

pub fn store<K, V>(&mut self, key: K, value: V) -> Option<Arc<V>> where
    K: Into<Cow<'static, str>>,
    V: Value, 
[src]

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);

pub fn load_scoped<'a, SC, V>(&self, key: &'a str) -> Option<Variable<'a, V>> where
    SC: Any,
    V: Value, 
[src]

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);

pub fn load<'a, V>(&self, key: &'a str) -> Option<Variable<'a, V>> where
    V: Value, 
[src]

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

impl<S: Clone> Clone for Context<S>[src]

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

type Target = 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> 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

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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.