group.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package versioning
  2. import (
  3. "github.com/kataras/iris/context"
  4. "github.com/kataras/iris/core/router"
  5. )
  6. // Group is a group of version-based routes.
  7. // One version per one or more routes.
  8. type Group struct {
  9. router.Party
  10. // Information not currently in-use.
  11. version string
  12. deprecation DeprecationOptions
  13. }
  14. // NewGroup returns a ptr to Group based on the given "version".
  15. // It sets the API Version for the "r" Party.
  16. //
  17. // See `Handle` and `RegisterGroups` for more.
  18. func NewGroup(r router.Party, version string) *Group {
  19. // Note that this feature alters the RouteRegisterRule to RouteOverlap
  20. // the RouteOverlap rule does not contain any performance downside
  21. // but it's good to know that if you registered other mode, this wanna change it.
  22. r.SetRegisterRule(router.RouteOverlap)
  23. r.UseOnce(Handler(version)) // this is required in order to not populate this middleware to the next group.
  24. return &Group{
  25. Party: r,
  26. version: version,
  27. }
  28. }
  29. // Deprecated marks this group and all its versioned routes
  30. // as deprecated versions of that endpoint.
  31. func (g *Group) Deprecated(options DeprecationOptions) *Group {
  32. // store it for future use, e.g. collect all deprecated APIs and notify the developer.
  33. g.deprecation = options
  34. g.Party.UseOnce(func(ctx *context.Context) {
  35. WriteDeprecated(ctx, options)
  36. ctx.Next()
  37. })
  38. return g
  39. }