#version #path #header #prefix #middleware #axum #optional

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

5 unstable releases

Uses new Rust 2024

new 0.3.0 Mar 17, 2025
0.2.0 Mar 16, 2025
0.1.2 Mar 16, 2025
0.1.1 Mar 14, 2025
0.1.0 Mar 14, 2025

#476 in HTTP server

Download history 167/week @ 2025-03-09

167 downloads per month

Apache-2.0

20KB
267 lines

api-version

license build

Axum middleware to rewrite a request such that a version prefix is added to the path. This is based on a set of 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.

Paths must not start with a version prefix, e.g. "/v0".

Example

use api_version::api_version;

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

/// Create an [ApiVersionLayer] correctly initialized with non-empty and strictly monotonically
/// increasing versions in the given inclusive range as well as an [ApiVersionFilter] making all
/// requests be rewritten.
let app = api_version!(0..=1).layer(app);

License

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

Dependencies

~8–16MB
~209K SLoC