mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
initialize support for versioning as requested, per route -- not finished yet
Former-commit-id: ade66610125f06a0b5ce3e90bcafe349f216a616
This commit is contained in:
87
versioning/versioning.go
Normal file
87
versioning/versioning.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package versioning
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/hashicorp/go-version"
|
||||
)
|
||||
|
||||
type Map map[string]context.Handler
|
||||
|
||||
func Handler(versions Map) context.Handler {
|
||||
constraintsHandlers, notFoundHandler := buildConstraints(versions)
|
||||
|
||||
return func(ctx context.Context) {
|
||||
versionString := GetVersion(ctx)
|
||||
if versionString == NotFound {
|
||||
notFoundHandler(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
version, err := version.NewVersion(versionString)
|
||||
if err != nil {
|
||||
notFoundHandler(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
for _, ch := range constraintsHandlers {
|
||||
if ch.constraints.Check(version) {
|
||||
ctx.Header("X-API-Version", version.String())
|
||||
ch.handler(ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
notFoundHandler(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
type constraintsHandler struct {
|
||||
constraints version.Constraints
|
||||
handler context.Handler
|
||||
}
|
||||
|
||||
func buildConstraints(versionsHandler Map) (constraintsHandlers []*constraintsHandler, notfoundHandler context.Handler) {
|
||||
for v, h := range versionsHandler {
|
||||
if v == NotFound {
|
||||
notfoundHandler = h
|
||||
continue
|
||||
}
|
||||
|
||||
constraints, err := version.NewConstraint(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
constraintsHandlers = append(constraintsHandlers, &constraintsHandler{
|
||||
constraints: constraints,
|
||||
handler: h,
|
||||
})
|
||||
}
|
||||
|
||||
if notfoundHandler == nil {
|
||||
notfoundHandler = NotFoundHandler
|
||||
}
|
||||
|
||||
// no sort, the end-dev should declare
|
||||
// all version constraint, i.e < 4.0 may be catch 1.0 if not something like
|
||||
// >= 3.0, < 4.0.
|
||||
// I can make it ordered but I do NOT like the final API of it:
|
||||
/*
|
||||
app.Get("/api/user", Handler( // accepts an array, ordered, see last elem.
|
||||
V("1.0", vHandler("v1 here")),
|
||||
V("2.0", vHandler("v2 here")),
|
||||
V("< 4.0", vHandler("v3.x here")),
|
||||
))
|
||||
instead we have:
|
||||
|
||||
app.Get("/api/user", Handler(Map{ // accepts a map, unordered, see last elem.
|
||||
"1.0": Deprecated(vHandler("v1 here")),
|
||||
"2.0": vHandler("v2 here"),
|
||||
">= 3.0, < 4.0": vHandler("v3.x here"),
|
||||
VersionUnknown: customHandlerForNotMatchingVersion,
|
||||
}))
|
||||
*/
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user