Make Axum a little smoother
(c) d2718; MIT license
Motivation
I use Axum a great deal, both in my personal projects and at work. I have, as one does when one uses anything with frequency, identified a miscellaney of snags and irritations; this crate is an attempt to smooth and soothe those.
This is in no way meant to be a complaint about axum or a criticism of any of the Tokio team's grave contributions to the Rust ecosystem; this is simply an adapter for my habitual use cases which I have chosen to publish in case some part of it might also be of use to others.
Features
Ergonomic Early-Return Error Handling
The [LaxErr] provides a flexible error type which implements
IntoResponse;
a companion [Laxative<T>] trait enables returning error responses
ergonomically from Result<T, LaxErr> with more control than a simple '?'
provides.
use ;
use StatusCode;
use ;
let auth_key = request.get_str
.ok_or_eyre
.with_status?;
Ergonomic Header Extraction
use ;
use ;
async
If the request lacks a comprehensible x-action header, they'll get a
400 response with the message "Request requires 'x-action' header."
A Layer to add Multiple Headers
tower-http has
SetResponseHeaderLayer,
but that will only add one header per layer; the
laxum::AddHeadersLayer can add an arbitrary number
of them.
use Router;
use ;
use AddHeadersLayer;
let add_headers = build
.with
.with
.to_layer;
let router = new
// A bunch of routes go here...
.layer;