2018-11-10 22:29:24 +01:00
|
|
|
package versioning
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/context"
|
|
|
|
"github.com/kataras/iris/v12/core/router"
|
2018-11-10 22:29:24 +01:00
|
|
|
)
|
|
|
|
|
2020-08-06 02:35:58 +02:00
|
|
|
// Group is a group of version-based routes.
|
|
|
|
// One version per one or more routes.
|
|
|
|
type Group struct {
|
|
|
|
router.Party
|
2018-11-10 22:29:24 +01:00
|
|
|
|
2020-08-06 02:35:58 +02:00
|
|
|
// Information not currently in-use.
|
|
|
|
version string
|
|
|
|
deprecation DeprecationOptions
|
|
|
|
}
|
2018-11-10 22:29:24 +01:00
|
|
|
|
2018-11-18 01:41:24 +01:00
|
|
|
// NewGroup returns a ptr to Group based on the given "version".
|
2020-08-06 02:35:58 +02:00
|
|
|
// It sets the API Version for the "r" Party.
|
2018-11-18 01:41:24 +01:00
|
|
|
//
|
|
|
|
// See `Handle` and `RegisterGroups` for more.
|
2020-08-06 02:35:58 +02:00
|
|
|
func NewGroup(r router.Party, version string) *Group {
|
|
|
|
// Note that this feature alters the RouteRegisterRule to RouteOverlap
|
|
|
|
// the RouteOverlap rule does not contain any performance downside
|
|
|
|
// but it's good to know that if you registered other mode, this wanna change it.
|
|
|
|
r.SetRegisterRule(router.RouteOverlap)
|
|
|
|
r.UseOnce(Handler(version)) // this is required in order to not populate this middleware to the next group.
|
|
|
|
|
2018-11-10 22:29:24 +01:00
|
|
|
return &Group{
|
2020-08-06 02:35:58 +02:00
|
|
|
Party: r,
|
2018-11-10 22:29:24 +01:00
|
|
|
version: version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated marks this group and all its versioned routes
|
|
|
|
// as deprecated versions of that endpoint.
|
|
|
|
func (g *Group) Deprecated(options DeprecationOptions) *Group {
|
2020-08-06 02:35:58 +02:00
|
|
|
// store it for future use, e.g. collect all deprecated APIs and notify the developer.
|
2018-11-10 22:29:24 +01:00
|
|
|
g.deprecation = options
|
|
|
|
|
2020-08-06 02:35:58 +02:00
|
|
|
g.Party.UseOnce(func(ctx *context.Context) {
|
|
|
|
WriteDeprecated(ctx, options)
|
|
|
|
ctx.Next()
|
2018-11-11 16:27:31 +01:00
|
|
|
})
|
2020-08-06 02:35:58 +02:00
|
|
|
return g
|
2018-11-10 22:29:24 +01:00
|
|
|
}
|