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:
  • "=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 match 1.2.3, 1.9.9, 3.1.1, but not 4.2.1, 2.1.1.

Getting Started

Import the versioning package.
1
import (
2
// [...]
3
4
"github.com/kataras/iris/v12"
5
"github.com/kataras/iris/v12/versioning"
6
)
Copied!
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:
1
package main
2
3
import (
4
"github.com/kataras/iris/v12"
5
"github.com/kataras/iris/v12/versioning"
6
)
7
8
func main() {
9
app := iris.New()
10
11
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
12
ctx.WriteString(`Root not found handler.
13
This will be applied everywhere except the /api/* requests.`)
14
})
15
16
api := app.Party("/api")
17
// Optional, set version aliases (literal strings).
18
// We use `UseRouter` instead of `Use`
19
// to handle HTTP errors per version, but it's up to you.
20
api.UseRouter(versioning.Aliases(versioning.AliasMap{
21
// If no version provided by the client, default it to the "1.0.0".
22
versioning.Empty: "1.0.0",
23
// If a "latest" version is provided by the client,
24
// set the version to be compared to "3.0.0".
25
"latest": "3.0.0",
26
}))
27
28
// |----------------|
29
// | The fun begins |
30
// |----------------|
31
32
// Create a new Group, which is a compatible Party,
33
// based on version constraints.
34
v1 := versioning.NewGroup(api, ">=1.0.0 <2.0.0")
35
36
// Optionally, set custom view engine and path
37
// for templates based on the version.
38
v1.RegisterView(iris.HTML("./v1", ".html"))
39
40
// Optionally, set custom error handler(s) based on the version.
41
// Keep in mind that if you do this, you will
42
// have to register error handlers
43
// for the rest of the parties as well.
44
v1.OnErrorCode(iris.StatusNotFound, testError("v1"))
45
46
// Register resources based on the version.
47
v1.Get("/", testHandler("v1"))
48
v1.Get("/render", testView)
49
50
// Do the same for version 2 and version 3,
51
// for the sake of the example.
52
v2 := versioning.NewGroup(api, ">=2.0.0 <3.0.0")
53
v2.RegisterView(iris.HTML("./v2", ".html"))
54
v2.OnErrorCode(iris.StatusNotFound, testError("v2"))
55
v2.Get("/", testHandler("v2"))
56
v2.Get("/render", testView)
57
58
v3 := versioning.NewGroup(api, ">=3.0.0 <4.0.0")
59
v3.RegisterView(iris.HTML("./v3", ".html"))
60
v3.OnErrorCode(iris.StatusNotFound, testError("v3"))
61
v3.Get("/", testHandler("v3"))
62
v3.Get("/render", testView)
63
64
app.Listen(":8080")
65
}
66
67
func testHandler(v string) iris.Handler {
68
return func(ctx iris.Context) {
69
ctx.JSON(iris.Map{
70
"version": v,
71
"message": "Hello, world!",
72
})
73
}
74
}
75
76
func testError(v string) iris.Handler {
77
return func(ctx iris.Context) {
78
ctx.Writef("not found: %s", v)
79
}
80
}
81
82
func testView(ctx iris.Context) {
83
ctx.View("index.html")
84
}
Copied!

Version Aliases

Optional, set version aliases (literal strings).
1
api.UseRouter(versioning.Aliases(versioning.AliasMap{
2
// If no version provided by the client, default it to the "1.0.0".
3
versioning.Empty: "1.0.0",
4
// If a "latest" version is provided by the client,
5
// set the version to be compared to "3.0.0".
6
"latest": "3.0.0",
7
}))
Copied!
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:
1
api.Use(func(ctx *context.Context) {
2
if version := ctx.URLParam("version"); version != "" {
3
versioning.SetVersion(ctx, version)
4
}
5
6
ctx.Next()
7
})
Copied!
Or by using the FromQuery(version, default) helper:
1
api.Use(versioning.FromQuery("version", "1.0.0"))
Copied!

Deprecation

To mark an API version as deprecated use the Deprecated method.
1
v1.Deprecated(versioning.DefaultDeprecationOptions)
Copied!
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 modified 10mo ago