Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

101 řádky
2.7KB

  1. package main
  2. import (
  3. "github.com/kataras/iris/v12"
  4. "github.com/kataras/iris/v12/versioning"
  5. )
  6. func main() {
  7. app := iris.New()
  8. app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
  9. ctx.WriteString(`Root not found handler.
  10. This will be applied everywhere except the /api/* requests.`)
  11. })
  12. api := app.Party("/api")
  13. // Optional, set version aliases (literal strings).
  14. // We use `UseRouter` instead of `Use`
  15. // to handle HTTP errors per version, but it's up to you.
  16. api.UseRouter(versioning.Aliases(versioning.AliasMap{
  17. // If no version provided by the client, default it to the "1.0.0".
  18. versioning.Empty: "1.0.0",
  19. // If a "latest" version is provided by the client,
  20. // set the version to be compared to "3.0.0".
  21. "latest": "3.0.0",
  22. }))
  23. /*
  24. A version is extracted through the versioning.GetVersion function,
  25. request headers:
  26. - Accept-Version: 1.0.0
  27. - Accept: application/json; version=1.0.0
  28. You can customize it by setting a version based on the request context:
  29. api.Use(func(ctx *context.Context) {
  30. if version := ctx.URLParam("version"); version != "" {
  31. SetVersion(ctx, version)
  32. }
  33. ctx.Next()
  34. })
  35. OR: api.Use(versioning.FromQuery("version", ""))
  36. */
  37. // |----------------|
  38. // | The fun begins |
  39. // |----------------|
  40. // Create a new Group, which is a compatible Party,
  41. // based on version constraints.
  42. v1 := versioning.NewGroup(api, ">=1.0.0 <2.0.0")
  43. // Optionally, set custom view engine and path
  44. // for templates based on the version.
  45. v1.RegisterView(iris.HTML("./v1", ".html"))
  46. // Optionally, set custom error handler(s) based on the version.
  47. // Keep in mind that if you do this, you will
  48. // have to register error handlers
  49. // for the rest of the parties as well.
  50. v1.OnErrorCode(iris.StatusNotFound, testError("v1"))
  51. // Register resources based on the version.
  52. v1.Get("/", testHandler("v1"))
  53. v1.Get("/render", testView)
  54. // Do the same for version 2 and version 3,
  55. // for the sake of the example.
  56. v2 := versioning.NewGroup(api, ">=2.0.0 <3.0.0")
  57. v2.RegisterView(iris.HTML("./v2", ".html"))
  58. v2.OnErrorCode(iris.StatusNotFound, testError("v2"))
  59. v2.Get("/", testHandler("v2"))
  60. v2.Get("/render", testView)
  61. v3 := versioning.NewGroup(api, ">=3.0.0 <4.0.0")
  62. v3.RegisterView(iris.HTML("./v3", ".html"))
  63. v3.OnErrorCode(iris.StatusNotFound, testError("v3"))
  64. v3.Get("/", testHandler("v3"))
  65. v3.Get("/render", testView)
  66. app.Listen(":8080")
  67. }
  68. func testHandler(v string) iris.Handler {
  69. return func(ctx iris.Context) {
  70. ctx.JSON(iris.Map{
  71. "version": v,
  72. "message": "Hello, world!",
  73. })
  74. }
  75. }
  76. func testError(v string) iris.Handler {
  77. return func(ctx iris.Context) {
  78. ctx.Writef("not found: %s", v)
  79. }
  80. }
  81. func testView(ctx iris.Context) {
  82. ctx.View("index.html")
  83. }