mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
minor: versioning: Match: store the matched version and revert the last change
Former-commit-id: e7aa04671d3f54650bb194a97300b6a89e1b0d2b
This commit is contained in:
parent
3c86fa8010
commit
878484204e
|
@ -430,7 +430,6 @@ New Context Methods:
|
||||||
- `Context.GetDomain() string` returns the domain.
|
- `Context.GetDomain() string` returns the domain.
|
||||||
- `Context.AddCookieOptions(...CookieOption)` adds options for `SetCookie`, `SetCookieKV, UpsertCookie` and `RemoveCookie` methods for the current request.
|
- `Context.AddCookieOptions(...CookieOption)` adds options for `SetCookie`, `SetCookieKV, UpsertCookie` and `RemoveCookie` methods for the current request.
|
||||||
- `Context.ClearCookieOptions()` clears any cookie options registered through `AddCookieOptions`.
|
- `Context.ClearCookieOptions()` clears any cookie options registered through `AddCookieOptions`.
|
||||||
- `Context.SetVersion(constraint string)` force-sets an [API Version](https://github.com/kataras/iris/wiki/API-versioning)
|
|
||||||
- `Context.SetLanguage(langCode string)` force-sets a language code from inside a middleare, similar to the `app.I18n.ExtractFunc`
|
- `Context.SetLanguage(langCode string)` force-sets a language code from inside a middleare, similar to the `app.I18n.ExtractFunc`
|
||||||
- `Context.ServeContentWithRate`, `ServeFileWithRate` and `SendFileWithRate` methods to throttle the "download" speed of the client
|
- `Context.ServeContentWithRate`, `ServeFileWithRate` and `SendFileWithRate` methods to throttle the "download" speed of the client
|
||||||
- `Context.IsHTTP2() bool` reports whether the protocol version for incoming request was HTTP/2
|
- `Context.IsHTTP2() bool` reports whether the protocol version for incoming request was HTTP/2
|
||||||
|
|
|
@ -944,7 +944,7 @@ type Configuration struct {
|
||||||
// Defaults to "iris.locale.language".
|
// Defaults to "iris.locale.language".
|
||||||
LanguageContextKey string `json:"languageContextKey,omitempty" yaml:"LanguageContextKey" toml:"LanguageContextKey"`
|
LanguageContextKey string `json:"languageContextKey,omitempty" yaml:"LanguageContextKey" toml:"LanguageContextKey"`
|
||||||
// VersionContextKey is the context key which an API Version can be modified
|
// VersionContextKey is the context key which an API Version can be modified
|
||||||
// via a middleware through `SetVersion` method, e.g. `ctx.SetVersion("1.0, 1.1")`.
|
// via a middleware through `SetVersion` method, e.g. `versioning.SetVersion(ctx, "1.0, 1.1")`.
|
||||||
// Defaults to "iris.api.version".
|
// Defaults to "iris.api.version".
|
||||||
VersionContextKey string `json:"versionContextKey" yaml:"VersionContextKey" toml:"VersionContextKey"`
|
VersionContextKey string `json:"versionContextKey" yaml:"VersionContextKey" toml:"VersionContextKey"`
|
||||||
// GetViewLayoutContextKey is the key of the context's user values' key
|
// GetViewLayoutContextKey is the key of the context's user values' key
|
||||||
|
|
|
@ -429,9 +429,6 @@ type Context interface {
|
||||||
// See `GetLocale` too.
|
// See `GetLocale` too.
|
||||||
// Example: https://github.com/kataras/iris/tree/master/_examples/i18n
|
// Example: https://github.com/kataras/iris/tree/master/_examples/i18n
|
||||||
Tr(format string, args ...interface{}) string
|
Tr(format string, args ...interface{}) string
|
||||||
// SetVersion force-sets the API Version integrated with the "iris/versioning" subpackage.
|
|
||||||
// It can be used inside a middleare.
|
|
||||||
SetVersion(constraint string)
|
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
// | Headers helpers |
|
// | Headers helpers |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
@ -2145,12 +2142,6 @@ func (ctx *context) Tr(format string, args ...interface{}) string { // other nam
|
||||||
return fmt.Sprintf(format, args...)
|
return fmt.Sprintf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetVersion force-sets the API Version integrated with the "iris/versioning" subpackage.
|
|
||||||
// It can be used inside a middleare.
|
|
||||||
func (ctx *context) SetVersion(constraint string) {
|
|
||||||
ctx.values.Set(ctx.app.ConfigurationReadOnly().GetVersionContextKey(), constraint)
|
|
||||||
}
|
|
||||||
|
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
// | Response Headers helpers |
|
// | Response Headers helpers |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
|
|
@ -22,7 +22,9 @@ const (
|
||||||
// ctx.Next()
|
// ctx.Next()
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// DEPRECATED: May 06 2020: Use `ctx.SetVersion(ctx.URLParamDefault("version", "1"))` instead.
|
// DEPRECATED: Use:
|
||||||
|
// version := ctx.URLParamDefault("version", "1")
|
||||||
|
// versioning.SetVersion(ctx, version) instead.
|
||||||
Key = "iris.api.version"
|
Key = "iris.api.version"
|
||||||
// NotFound is the key that can be used inside a `Map` or inside `ctx.SetVersion(versioning.NotFound)`
|
// NotFound is the key that can be used inside a `Map` or inside `ctx.SetVersion(versioning.NotFound)`
|
||||||
// to tell that a version wasn't found, therefore the not found handler should handle the request instead.
|
// to tell that a version wasn't found, therefore the not found handler should handle the request instead.
|
||||||
|
@ -56,6 +58,8 @@ var NotFoundHandler = func(ctx context.Context) {
|
||||||
//
|
//
|
||||||
// However, the end developer can also set a custom version for a handler via a middleware by using the context's store key
|
// However, the end developer can also set a custom version for a handler via a middleware by using the context's store key
|
||||||
// for versions (see `Key` for further details on that).
|
// for versions (see `Key` for further details on that).
|
||||||
|
//
|
||||||
|
// See `SetVersion` too.
|
||||||
func GetVersion(ctx context.Context) string {
|
func GetVersion(ctx context.Context) string {
|
||||||
// firstly by context store, if manually set by a middleware.
|
// firstly by context store, if manually set by a middleware.
|
||||||
version := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetVersionContextKey())
|
version := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetVersionContextKey())
|
||||||
|
@ -96,3 +100,10 @@ func GetVersion(ctx context.Context) string {
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetVersion force-sets the API Version.
|
||||||
|
// It can be used inside a middleware.
|
||||||
|
// See `GetVersion` too.
|
||||||
|
func SetVersion(ctx context.Context, constraint string) {
|
||||||
|
ctx.Values().Set(ctx.Application().ConfigurationReadOnly().GetVersionContextKey(), constraint)
|
||||||
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ func TestGetVersion(t *testing.T) {
|
||||||
|
|
||||||
app.Get("/", writeVesion)
|
app.Get("/", writeVesion)
|
||||||
app.Get("/manual", func(ctx iris.Context) {
|
app.Get("/manual", func(ctx iris.Context) {
|
||||||
ctx.SetVersion("11.0.5")
|
versioning.SetVersion(ctx, "11.0.5")
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
}, writeVesion)
|
}, writeVesion)
|
||||||
|
|
||||||
|
|
|
@ -9,23 +9,39 @@ import (
|
||||||
// If reports whether the "version" is matching to the "is".
|
// If reports whether the "version" is matching to the "is".
|
||||||
// the "is" can be a constraint like ">= 1, < 3".
|
// the "is" can be a constraint like ">= 1, < 3".
|
||||||
func If(v string, is string) bool {
|
func If(v string, is string) bool {
|
||||||
|
_, ok := check(v, is)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(v string, is string) (string, bool) {
|
||||||
ver, err := version.NewVersion(v)
|
ver, err := version.NewVersion(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
constraints, err := version.NewConstraint(is)
|
constraints, err := version.NewConstraint(is)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
return constraints.Check(ver)
|
// return the extracted version from request, even if not matched.
|
||||||
|
return ver.String(), constraints.Check(ver)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match acts exactly the same as `If` does but instead it accepts
|
// Match acts exactly the same as `If` does but instead it accepts
|
||||||
// a Context, so it can be called by a handler to determinate the requested version.
|
// a Context, so it can be called by a handler to determinate the requested version.
|
||||||
|
//
|
||||||
|
// If matched then it sets the "X-API-Version" response header and
|
||||||
|
// stores the matched version into Context (see `GetVersion` too).
|
||||||
func Match(ctx context.Context, expectedVersion string) bool {
|
func Match(ctx context.Context, expectedVersion string) bool {
|
||||||
return If(GetVersion(ctx), expectedVersion)
|
versionString, matched := check(GetVersion(ctx), expectedVersion)
|
||||||
|
if !matched {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
SetVersion(ctx, versionString)
|
||||||
|
ctx.Header("X-API-Version", versionString)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map is a map of versions targets to a handlers,
|
// Map is a map of versions targets to a handlers,
|
||||||
|
@ -63,7 +79,7 @@ func NewMatcher(versions Map) context.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// pass the not matched version so the not found handler can have knowedge about it.
|
// pass the not matched version so the not found handler can have knowedge about it.
|
||||||
// ctx.SetVersion(versionString)
|
// SetVersion(ctx, versionString)
|
||||||
// or let a manual cal of GetVersion(ctx) do that instead.
|
// or let a manual cal of GetVersion(ctx) do that instead.
|
||||||
notFoundHandler(ctx)
|
notFoundHandler(ctx)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user