Module context
Expand description
Context passed to and between services as input.
§State
rama
supports two kinds of states:
- static state: this state can be a part of the service struct or captured by a closure
- dynamic state: these can be injected as
Extensions
s using methods such asContext::insert
Any state that is optional, and especially optional state injected by middleware, can be inserted using extensions.
It is however important to try as much as possible to then also consume this state in an approach that deals
gracefully with its absence. Good examples of this are header-related inputs. Headers might be set or not,
and so absence of Extensions
s that might be created as a result of these might reasonably not exist.
It might of course still mean the app returns an error response when it is absent, but it should not unwrap/panic.
§Examples
§Example: Extensions
The Context
can be extended with additional data using the Extensions
type.
use rama_core::Context;
let mut ctx = Context::default();
ctx.insert(5i32);
assert_eq!(ctx.get::<i32>(), Some(&5i32));
Structs§
- Context
- Context passed to and between services as input.
- Extensions
- A type map of protocol extensions.
- Parts
- Component parts of
Context
. - Request
Context Ext - Wrapper type that can be injected into the dynamic extensions of a “Response”,
in order to preserve the
Context
’s extensions of the Request which was used to produce the Response.