pub struct Context { /* private fields */ }Expand description
Protocol-agnostic request context.
Provides unified access to protocol-specific metadata like headers, user info, and trace IDs.
§Automatic Injection
When using protocol macros (#[http], #[ws], etc.), methods can receive a Context
parameter that is automatically populated with request metadata:
ⓘ
use server_less::{http, Context};
#[http]
impl UserService {
async fn create_user(&self, ctx: Context, name: String) -> Result<User> {
// Access request metadata
let user_id = ctx.user_id()?; // Authenticated user
let request_id = ctx.request_id()?; // Request trace ID
let auth = ctx.authorization(); // Authorization header
// Create user with context...
}
}§Protocol-Specific Metadata
Different protocols populate Context with relevant data:
- HTTP: All headers via
header(), request ID fromx-request-id - gRPC: Metadata fields (not yet implemented)
- CLI: Environment variables via
env()(not yet implemented) - MCP: Conversation context (not yet implemented)
§Name Collision
If you have your own Context type, qualify the server-less version:
ⓘ
fn handler(&self, ctx: server_less::Context) { }See the #[http] macro documentation for details on collision handling.
Implementations§
Source§impl Context
impl Context
Sourcepub fn with_metadata(metadata: HashMap<String, String>) -> Context
pub fn with_metadata(metadata: HashMap<String, String>) -> Context
Create context with metadata
Sourcepub fn set_user_id(&mut self, user_id: impl Into<String>)
pub fn set_user_id(&mut self, user_id: impl Into<String>)
Set the authenticated user ID
Sourcepub fn request_id(&self) -> Option<&str>
pub fn request_id(&self) -> Option<&str>
Get the request ID
Sourcepub fn set_request_id(&mut self, request_id: impl Into<String>)
pub fn set_request_id(&mut self, request_id: impl Into<String>)
Set the request ID
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Get an HTTP header value (case-insensitive lookup)
Get the Authorization header
Sourcepub fn content_type(&self) -> Option<&str>
pub fn content_type(&self) -> Option<&str>
Get the Content-Type header
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
impl UnwindSafe for Context
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