mirror of
https://github.com/kataras/iris.git
synced 2026-01-21 02:45:59 +00:00
replace ioutil with io package and other minor improvements
This commit is contained in:
@@ -30,25 +30,31 @@ type Group struct {
|
||||
// any changes to its parent won't affect this one (e.g. register global middlewares afterwards).
|
||||
//
|
||||
// A version is extracted through the versioning.GetVersion function:
|
||||
// Accept-Version: 1.0.0
|
||||
// Accept: application/json; version=1.0.0
|
||||
// You can customize it by setting a version based on the request context:
|
||||
// api.Use(func(ctx *context.Context) {
|
||||
// if version := ctx.URLParam("version"); version != "" {
|
||||
// SetVersion(ctx, version)
|
||||
// }
|
||||
//
|
||||
// ctx.Next()
|
||||
// })
|
||||
// Accept-Version: 1.0.0
|
||||
// Accept: application/json; version=1.0.0
|
||||
//
|
||||
// You can customize it by setting a version based on the request context:
|
||||
//
|
||||
// api.Use(func(ctx *context.Context) {
|
||||
// if version := ctx.URLParam("version"); version != "" {
|
||||
// SetVersion(ctx, version)
|
||||
// }
|
||||
//
|
||||
// ctx.Next()
|
||||
// })
|
||||
//
|
||||
// OR:
|
||||
// api.Use(versioning.FromQuery("version", ""))
|
||||
//
|
||||
// api.Use(versioning.FromQuery("version", ""))
|
||||
//
|
||||
// Examples at: _examples/routing/versioning
|
||||
// Usage:
|
||||
// app := iris.New()
|
||||
// api := app.Party("/api")
|
||||
// v1 := versioning.NewGroup(api, ">=1.0.0 <2.0.0")
|
||||
// v1.Get/Post/Put/Delete...
|
||||
//
|
||||
// app := iris.New()
|
||||
// api := app.Party("/api")
|
||||
// v1 := versioning.NewGroup(api, ">=1.0.0 <2.0.0")
|
||||
// v1.Get/Post/Put/Delete...
|
||||
//
|
||||
// Valid ranges are:
|
||||
// - "<1.0.0"
|
||||
@@ -61,8 +67,10 @@ type Group struct {
|
||||
// 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:
|
||||
@@ -72,7 +80,8 @@ type Group struct {
|
||||
//
|
||||
// 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`,
|
||||
// - `>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`
|
||||
func NewGroup(r API, version string) *Group {
|
||||
version = strings.ReplaceAll(version, ",", " ")
|
||||
|
||||
@@ -187,11 +187,13 @@ func GetVersion(ctx *context.Context) string {
|
||||
// It can be used inside a middleware.
|
||||
// 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
|
||||
// version := ctx.URLParamDefault("version", "1.0.0")
|
||||
// versioning.SetVersion(ctx, version)
|
||||
// ctx.Next()
|
||||
// }
|
||||
//
|
||||
// func(ctx iris.Context) { // &version=1
|
||||
// version := ctx.URLParamDefault("version", "1.0.0")
|
||||
// versioning.SetVersion(ctx, version)
|
||||
// ctx.Next()
|
||||
// }
|
||||
//
|
||||
// See `GetVersion` too.
|
||||
func SetVersion(ctx *context.Context, constraint string) {
|
||||
ctx.Values().Set(ctx.Application().ConfigurationReadOnly().GetVersionContextKey(), constraint)
|
||||
@@ -205,23 +207,24 @@ type AliasMap = map[string]string
|
||||
// for the children Parties(routers). It's respected by versioning Groups.
|
||||
//
|
||||
// Example Code:
|
||||
// app := iris.New()
|
||||
//
|
||||
// api := app.Party("/api")
|
||||
// api.Use(Aliases(map[string]string{
|
||||
// versioning.Empty: "1.0.0", // when no version was provided by the client.
|
||||
// "beta": "4.0.0",
|
||||
// "stage": "5.0.0-alpha"
|
||||
// }))
|
||||
// app := iris.New()
|
||||
//
|
||||
// v1 := NewGroup(api, ">=1.0.0 < 2.0.0")
|
||||
// v1.Get/Post...
|
||||
// api := app.Party("/api")
|
||||
// api.Use(Aliases(map[string]string{
|
||||
// versioning.Empty: "1.0.0", // when no version was provided by the client.
|
||||
// "beta": "4.0.0",
|
||||
// "stage": "5.0.0-alpha"
|
||||
// }))
|
||||
//
|
||||
// v4 := NewGroup(api, ">=4.0.0 < 5.0.0")
|
||||
// v4.Get/Post...
|
||||
// v1 := NewGroup(api, ">=1.0.0 < 2.0.0")
|
||||
// v1.Get/Post...
|
||||
//
|
||||
// stage := NewGroup(api, "5.0.0-alpha")
|
||||
// stage.Get/Post...
|
||||
// v4 := NewGroup(api, ">=4.0.0 < 5.0.0")
|
||||
// v4.Get/Post...
|
||||
//
|
||||
// stage := NewGroup(api, "5.0.0-alpha")
|
||||
// stage.Get/Post...
|
||||
func Aliases(aliases AliasMap) context.Handler {
|
||||
cp := make(AliasMap, len(aliases)) // copy the map here so we are safe of later modifications by end-dev.
|
||||
for k, v := range aliases {
|
||||
|
||||
Reference in New Issue
Block a user