1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

finalize the API

Former-commit-id: e680a9fc517c02eca66f83e42519c9820122fae8
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-11-18 02:41:24 +02:00
parent 6886fd98c8
commit c74196c6d7
6 changed files with 114 additions and 74 deletions

View File

@@ -7,14 +7,28 @@ import (
)
const (
AcceptVersionHeaderKey = "Accept-Version"
AcceptHeaderKey = "Accept"
// AcceptVersionHeaderKey is the header key of "Accept-Version".
AcceptVersionHeaderKey = "Accept-Version"
// AcceptHeaderKey is the header key of "Accept".
AcceptHeaderKey = "Accept"
// AcceptHeaderVersionValue is the Accept's header value search term the requested version.
AcceptHeaderVersionValue = "version"
Key = "iris.api.version" // for use inside the ctx.Values(), not visible by the user.
// Key is the context key of the version, can be used to manually modify the "requested" version.
// Example of how you can change the default behavior to extract a requested version (which is by headers)
// from a "version" url parameter instead:
// func(ctx iris.Context) { // &version=1
// ctx.Values().Set(versioning.Key, ctx.URLParamDefault("version", "1"))
// ctx.Next()
// }
Key = "iris.api.version" // for use inside the ctx.Values(), not visible by the user.
// NotFound is the key that can be used inside a `Map` or inside `ctx.Values().Set(versioning.Key, versioning.NotFound)`
// to tell that a version wasn't found, therefore the not found handler should handle the request instead.
NotFound = Key + ".notfound"
)
// NotFoundHandler is the default version not found handler that
// is executed from `NewMatcher` when no version is registered as available to dispatch a resource.
var NotFoundHandler = func(ctx context.Context) {
// 303 is an option too,
// end-dev has the chance to change that behavior by using the NotFound in the map:
@@ -32,6 +46,14 @@ var NotFoundHandler = func(ctx context.Context) {
ctx.WriteString("version not found")
}
// GetVersion returns the current request version.
//
// By default the `GetVersion` will try to read from:
// - "Accept" header, i.e Accept: "application/json; version=1.0"
// - "Accept-Version" header, i.e Accept-Version: "1.0"
//
// 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).
func GetVersion(ctx context.Context) string {
// firstly by context store, if manually set-ed by a middleware.
if version := ctx.Values().GetString(Key); version != "" {