#axum-middleware #prefix #http-header #version #request-path #rewritten #v0

api-version

Axum middleware to add a version prefix to request paths based on a set of versions and an optional x-api-version header

7 releases

Uses new Rust 2024

0.3.2 Aug 15, 2025
0.3.1 Jul 16, 2025
0.3.0 Mar 17, 2025
0.2.0 Mar 16, 2025
0.1.2 Mar 16, 2025

#731 in HTTP server

46 downloads per month

Apache-2.0

21KB
267 lines

api-version

license build docs

Axum middleware to rewrite a request such that a version prefix is added to the path. This is based on a set of API versions and an optional "x-api-version" custom HTTP header: if no such header is present, the highest version is used. Yet this only applies to requests the URIs of which pass a filter; others are not rewritten. Also, paths starting with a valid/existing version prefix, e.g. "/v0", are not rewritten.

Example

struct ReadyFilter;

impl ApiVersionFilter for ReadyFilter {
    type Error = Infallible;

    async fn should_rewrite(&self, uri: &Uri) -> Result<bool, Self::Error> {
        Ok(uri.path() != "/")
    }
}

let app = Router::new()
    .route("/", get(ready))
    .route("/v0/test", get(ok_0))
    .route("/v1/test", get(ok_1));

const API_VERSIONS: ApiVersions<2> = ApiVersions::new([0, 1]);

let app = ApiVersionLayer::new(API_VERSIONS, ReadyFilter).layer(app);

License

This code is open source software licensed under the Apache 2.0 License.

Dependencies

~8–13MB
~239K SLoC