From b8addac888042ac966b592af79f51ae97d7277d6 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 18 Nov 2018 03:06:59 +0200 Subject: [PATCH] add versioning example Former-commit-id: 54c903135d1c2ba1838b47fa419433dbb010c317 --- _examples/README.md | 5 +++ _examples/versioning/README.md | 1 + _examples/versioning/main.go | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 _examples/versioning/README.md create mode 100644 _examples/versioning/main.go diff --git a/_examples/README.md b/_examples/README.md index fd0f4f7c..38a3faa2 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -158,6 +158,11 @@ Navigate through examples for a better understanding. * [per-route](routing/writing-a-middleware/per-route/main.go) * [globally](routing/writing-a-middleware/globally/main.go) +### Versioning + +- [How it works](https://github.com/kataras/iris/blob/master/versioning/README.md) +- [Example](versioning/main.go) + ### hero - [Basic](hero/basic/main.go) diff --git a/_examples/versioning/README.md b/_examples/versioning/README.md new file mode 100644 index 00000000..d45f42d3 --- /dev/null +++ b/_examples/versioning/README.md @@ -0,0 +1 @@ +Head over to the [kataras/iris/versioning/README.md](https://github.com/kataras/iris/blob/master/versioning/README.md) instead. \ No newline at end of file diff --git a/_examples/versioning/main.go b/_examples/versioning/main.go new file mode 100644 index 00000000..8516b190 --- /dev/null +++ b/_examples/versioning/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "github.com/kataras/iris" + "github.com/kataras/iris/versioning" +) + +func main() { + app := iris.New() + + examplePerRoute(app) + examplePerParty(app) + + // Read the README.md before any action. + app.Run(iris.Addr(":8080")) +} + +// How to test: +// Open Postman +// GET: localhost:8080/api/cats +// Headers[1] = Accept-Version: "1" and repeat with +// Headers[1] = Accept-Version: "2.5" +// or even "Accept": "application/json; version=2.5" +func examplePerRoute(app *iris.Application) { + app.Get("/api/cats", versioning.NewMatcher(versioning.Map{ + "1": catsVersionExactly1Handler, + ">= 2, < 3": catsV2Handler, + versioning.NotFound: versioning.NotFoundHandler, + })) +} + +// How to test: +// Open Postman +// GET: localhost:8080/api/users +// Headers[1] = Accept-Version: "1.9.9" and repeat with +// Headers[1] = Accept-Version: "2.5" +// +// POST: localhost:8080/api/users/new +// Headers[1] = Accept-Version: "1.8.3" +// +// POST: localhost:8080/api/users +// Headers[1] = Accept-Version: "2" +func examplePerParty(app *iris.Application) { + usersAPI := app.Party("/api/users") + + // version 1. + usersAPIV1 := versioning.NewGroup(">= 1, < 2") + usersAPIV1.Get("/", func(ctx iris.Context) { + ctx.Writef("v1 resource: /api/users handler") + }) + usersAPIV1.Post("/new", func(ctx iris.Context) { + ctx.Writef("v1 resource: /api/users/new post handler") + }) + + // version 2. + usersAPIV2 := versioning.NewGroup(">= 2, < 3") + usersAPIV2.Get("/", func(ctx iris.Context) { + ctx.Writef("v2 resource: /api/users handler") + }) + usersAPIV2.Post("/", func(ctx iris.Context) { + ctx.Writef("v2 resource: /api/users post handler") + }) + + versioning.RegisterGroups(usersAPI, versioning.NotFoundHandler, usersAPIV1, usersAPIV2) +} + +func catsVersionExactly1Handler(ctx iris.Context) { + ctx.Writef("v1 exactly resource: /api/cats handler") +} + +func catsV2Handler(ctx iris.Context) { + ctx.Writef("v2 resource: /api/cats handler") +}