versioning.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package mvc
  2. import (
  3. "github.com/kataras/iris/v12/context"
  4. "github.com/kataras/iris/v12/core/router"
  5. "github.com/kataras/iris/v12/versioning"
  6. )
  7. // Version returns a valid `Option` that can be passed to the `Application.Handle` method.
  8. // It requires a specific "version" constraint for a Controller,
  9. // e.g. ">1.0.0 <=2.0.0".
  10. //
  11. // Usage:
  12. //
  13. // m := mvc.New(dataRouter)
  14. // m.Handle(new(v1Controller), mvc.Version("1.0.0"), mvc.Deprecated(mvc.DeprecationOptions{}))
  15. // m.Handle(new(v2Controller), mvc.Version("2.3.0"))
  16. // m.Handle(new(v3Controller), mvc.Version(">=3.0.0 <4.0.0"))
  17. // m.Handle(new(noVersionController))
  18. //
  19. // See the `versioning` package's documentation for more information on
  20. // how the version is extracted from incoming requests.
  21. //
  22. // Note that this Option will set the route register rule to `RouteOverlap`.
  23. func Version(version string) OptionFunc {
  24. return func(c *ControllerActivator) {
  25. c.Router().SetRegisterRule(router.RouteOverlap) // required for this feature.
  26. // Note: Do not use a group, we need c.Use for the specific controller's routes.
  27. c.Use(versioning.Handler(version))
  28. }
  29. }
  30. // Deprecated marks a specific Controller as a deprecated one.
  31. // Deprecated can be used to tell the clients that
  32. // a newer version of that specific resource is available instead.
  33. func Deprecated(options DeprecationOptions) OptionFunc {
  34. return func(c *ControllerActivator) {
  35. c.Use(func(ctx *context.Context) {
  36. versioning.WriteDeprecated(ctx, options)
  37. ctx.Next()
  38. })
  39. }
  40. }