[−][src]Struct roa::Context
A structure to share request, response and other data between middlewares.
Type of the first parameter in a middleware.
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 header(
&self,
name: impl AsHeaderName
) -> Option<Result<&str, ToStrError>>
[src]
&self,
name: impl AsHeaderName
) -> Option<Result<&str, ToStrError>>
Search for a header value and try to get its string copy.
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.header(&CONTENT_TYPE).unwrap().unwrap() ); 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
K: Into<Cow<'static, str>>,
SC: Any,
V: Value,
[src]
&mut self,
scope: SC,
key: K,
value: V
) -> Option<Arc<V>> where
K: Into<Cow<'static, str>>,
SC: Any,
V: Value,
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]
K: Into<Cow<'static, str>>,
V: Value,
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<SC, V>(&self, key: &'a str) -> Option<Variable<'a, V>> where
SC: Any,
V: Value,
[src]
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);
pub fn load<V>(&self, key: &'a str) -> Option<Variable<'a, V>> where
V: Value,
[src]
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
impl<S, Conn> AsyncPool<Conn> for Context<S> where
Conn: Connection + 'static,
S: State + AsRef<Pool<ConnectionManager<Conn>>>,
Conn: Connection + 'static,
S: State + AsRef<Pool<ConnectionManager<Conn>>>,
fn get_conn<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<PooledConnection<ConnectionManager<Conn>>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<PooledConnection<ConnectionManager<Conn>>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
fn get_timeout<'life0, 'async_trait>(
&'life0 self,
timeout: Duration
) -> Pin<Box<dyn Future<Output = Result<PooledConnection<ConnectionManager<Conn>>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
&'life0 self,
timeout: Duration
) -> Pin<Box<dyn Future<Output = Result<PooledConnection<ConnectionManager<Conn>>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
fn pool_state<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = State> + 'async_trait + Send>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
&'life0 self
) -> Pin<Box<dyn Future<Output = State> + 'async_trait + Send>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
impl<S> Clone for Context<S> where
S: Clone,
[src]
S: Clone,
impl<S> CookieGetter for Context<S>
[src]
fn must_cookie(&mut self, name: &str) -> Result<Arc<Cookie<'static>>>
[src]
fn cookie(&self, name: &str) -> Option<Arc<Cookie<'static>>>
[src]
impl<S> CookieSetter for Context<S>
[src]
fn set_cookie(&mut self, cookie: Cookie) -> Result
[src]
impl<S> Deref for Context<S>
[src]
type Target = S
The resulting type after dereferencing.
fn deref(&self) -> &<Context<S> as Deref>::Target
[src]
impl<S> DerefMut for Context<S>
[src]
impl<S: State> Forward for Context<S>
[src]
fn host(&self) -> Result<String>
[src]
fn client_ip(&self) -> IpAddr
[src]
fn forwarded_ips(&self) -> Vec<IpAddr>
[src]
fn forwarded_proto(&self) -> Option<Result<String>>
[src]
impl<S> JwtVerifier<S> for Context<S>
[src]
fn claims<C>(&self) -> Result<C> where
C: 'static + DeserializeOwned,
[src]
C: 'static + DeserializeOwned,
fn verify<C>(&self, validation: &Validation) -> Result<C> where
C: 'static + DeserializeOwned,
[src]
C: 'static + DeserializeOwned,
impl<S> PowerBody for Context<S> where
S: State,
[src]
S: State,
fn body<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + 'async_trait>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + 'async_trait>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
fn read_json<'life0, 'async_trait, B>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
fn read_form<'life0, 'async_trait, B>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
fn write_json<B>(&mut self, data: &B) -> Result<(), Error> where
B: Serialize,
[src]
B: Serialize,
fn render<B>(&mut self, data: &B) -> Result<(), Error> where
B: Template,
[src]
B: Template,
fn write_text<B>(&mut self, data: B) where
B: Into<Bytes>,
[src]
B: Into<Bytes>,
fn write_octet<B>(&mut self, reader: B) where
B: 'static + Send + Sync + Unpin + AsyncRead,
[src]
B: 'static + Send + Sync + Unpin + AsyncRead,
fn write_file<'life0, 'async_trait, P>(
&'life0 mut self,
path: P,
typ: DispositionType
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>> where
'life0: 'async_trait,
P: AsRef<Path> + 'async_trait,
Context<S>: 'async_trait,
[src]
&'life0 mut self,
path: P,
typ: DispositionType
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>> where
'life0: 'async_trait,
P: AsRef<Path> + 'async_trait,
Context<S>: 'async_trait,
impl<S> Query for Context<S>
[src]
fn must_query<'a>(&self, name: &'a str) -> Result<Variable<'a, String>>
[src]
fn query<'a>(&self, name: &'a str) -> Option<Variable<'a, String>>
[src]
impl<S> RouterParam for Context<S>
fn must_param(&self, name: &'a str) -> Result<Variable<'a, String>, Error>
fn param(&self, name: &'a str) -> Option<Variable<'a, String>>
impl<S, Conn> SqlQuery<Conn> for Context<S> where
Conn: 'static + Connection,
S: State + AsRef<Pool<ConnectionManager<Conn>>>,
Conn: 'static + Connection,
S: State + AsRef<Pool<ConnectionManager<Conn>>>,
fn execute<'life0, 'async_trait, E>(
&'life0 self,
exec: E
) -> Pin<Box<dyn Future<Output = Result<usize, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
E: 'static + Send + ExecuteDsl<Conn, <Conn as Connection>::Backend> + 'async_trait,
Context<S>: 'async_trait,
&'life0 self,
exec: E
) -> Pin<Box<dyn Future<Output = Result<usize, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
E: 'static + Send + ExecuteDsl<Conn, <Conn as Connection>::Backend> + 'async_trait,
Context<S>: 'async_trait,
fn load_data<'life0, 'async_trait, U, Q>(
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Vec<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LoadQuery<Conn, U> + 'async_trait,
U: 'static + Send + 'async_trait,
Context<S>: 'async_trait,
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Vec<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LoadQuery<Conn, U> + 'async_trait,
U: 'static + Send + 'async_trait,
Context<S>: 'async_trait,
Executes the given query, returning a Vec
with the returned rows.
When using the query builder,
the return type can be
a tuple of the values,
or a struct which implements Queryable
.
When this method is called on sql_query
,
the return type can only be a struct which implements QueryableByName
For insert, update, and delete operations where only a count of affected is needed,
execute
should be used instead.
fn get_result<'life0, 'async_trait, U, Q>(
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Option<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LoadQuery<Conn, U> + 'async_trait,
U: 'static + Send + 'async_trait,
Context<S>: 'async_trait,
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Option<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LoadQuery<Conn, U> + 'async_trait,
U: 'static + Send + 'async_trait,
Context<S>: 'async_trait,
Runs the command, and returns the affected row.
Err(NotFound)
will be returned if the query affected 0 rows. You can
call .optional()
on the result of this if the command was optional to
get back a Result<Option<U>>
When this method is called on an insert, update, or delete statement,
it will implicitly add a RETURNING *
to the query,
unless a returning clause was already specified.
fn get_results<'life0, 'async_trait, U, Q>(
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Vec<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LoadQuery<Conn, U> + 'async_trait,
U: 'static + Send + 'async_trait,
Context<S>: 'async_trait,
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Vec<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LoadQuery<Conn, U> + 'async_trait,
U: 'static + Send + 'async_trait,
Context<S>: 'async_trait,
Runs the command, returning an Vec
with the affected rows.
This method is an alias for load
, but with a name that makes more
sense for insert, update, and delete statements.
fn first<'life0, 'async_trait, U, Q>(
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Option<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LimitDsl + 'async_trait,
U: 'static + Send + 'async_trait,
<Q as LimitDsl>::Output: LoadQuery<Conn, U>,
Context<S>: 'async_trait,
&'life0 self,
query: Q
) -> Pin<Box<dyn Future<Output = Result<Option<U>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Q: 'static + Send + LimitDsl + 'async_trait,
U: 'static + Send + 'async_trait,
<Q as LimitDsl>::Output: LoadQuery<Conn, U>,
Context<S>: 'async_trait,
Attempts to load a single record.
This method is equivalent to .limit(1).get_result()
Returns Ok(record)
if found, and Err(NotFound)
if no results are
returned. If the query truly is optional, you can call .optional()
on
the result of this to get a Result<Option<U>>
.
Auto Trait Implementations
impl<S> !RefUnwindSafe for Context<S>
impl<S> Send for Context<S> where
S: Send,
S: Send,
impl<S> Sync for Context<S> where
S: Sync,
S: Sync,
impl<S> Unpin for Context<S> where
S: Unpin,
S: Unpin,
impl<S> !UnwindSafe for Context<S>
Blanket Implementations
impl<T, A, P> Access<T> for P where
A: Access<T>,
P: Deref<Target = A>,
[src]
A: Access<T>,
P: Deref<Target = A>,
type Guard = <A as Access<T>>::Guard
A guard object containing the value and keeping it alive. Read more
fn load(&self) -> <P as Access<T>>::Guard
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, A> DynAccess<T> for A where
A: Access<T>,
<A as Access<T>>::Guard: 'static,
[src]
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IntoSql for T
fn into_sql<T>(self) -> Self::Expression where
Self: AsExpression<T>,
Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
&'a Self: AsExpression<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> State for T where
T: 'static + Clone + Send + Sync,
[src]
T: 'static + Clone + Send + Sync,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,