API Versioning
The versioning subpackage provides semver versioning for your APIs.
All best practice conventions are implemented, including the suggestions written in the api-guidelines document.
The version is retrieved from the
"Accept"
and/or"Accept-Version"
request header(s).If a version matched, the server responds with a custom
"X-Api-Version"
header with the normalized version inside.If a version was not found (missing version header or unimplemented version) the server responds with status code of 501 and plain text of "version not found" by default.
If a specific version of the resource is deprecated by the server, the client receives the custom
"X-Api-Warn"
,"X-Api-Deprecation-Date"
and"X-Api-Deprecation-Info"
headers, the request is handled as expected.
Introduction
Internally, the version validation is done by the regex-free and fast github.com/blang/semver/v4 third-party package.
Valid version ranges are:
"<1.0.0"
"<=1.0.0"
">1.0.0"
">=1.0.0"
"1.0.0", "=1.0.0", "==1.0.0"
"!1.0.0", "!=1.0.0"
A Range can consist of multiple ranges separated by space: Ranges can be linked by logical AND:
">1.0.0 <2.0.0" would match between both ranges, so "1.1.1" and "1.8.7" but not "1.0.0" or "2.0.0"
">1.0.0 <3.0.0 !2.0.3-beta.2" would match every version between 1.0.0 and 3.0.0 except 2.0.3-beta.2
Ranges can also be linked by logical OR:
"<2.0.0 || >=3.0.0" would match "1.x.x" and "3.x.x" but not "2.x.x"
AND has a higher precedence than OR. It's not possible to use brackets.
Ranges can be combined by both AND and OR
>1.0.0 <2.0.0 || >3.0.0 !4.2.1
would match1.2.3
,1.9.9
,3.1.1
, but not4.2.1
,2.1.1
.
Getting Started
Import the versioning
package.
Using the versioning.NewGroup(version string) *versioning.Group
function you can create a group of routes to register the application's routes based on version requested by clients. The versioning.Group
completes the iris.Party
interface.
Example Code:
Version Aliases
Optional, set version aliases (literal strings).
We use UseRouter
instead of Use
to handle HTTP errors per version, but it's up to you.
Get Current Version
The version is extracted through the versioning.GetVersion
function. You can use it anywhere, even if you don't use the versioning feature. By default GetVersion
will try to read from:
Accept
header, e.g.Accept: "application/json; version=1.0.0"
Accept-Version
header, e.g.Accept-Version: "1.0.0"
You can customize it by setting a version based on the request context:
Or by using the FromQuery(version, default)
helper:
Deprecation
To mark an API version as deprecated use the Deprecated
method.
This will make the route handlers to send some extra headers to the client.
"X-API-Warn": options.WarnMessage
"X-API-Deprecation-Date": context.FormatTime(ctx, options.DeprecationDate))
"X-API-Deprecation-Info": options.DeprecationInfo
Last updated