SJA工艺
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.0 KiB

3 years ago
  1. package dispatch
  2. import (
  3. "LAPP_SJA_ME/web/routes/dispatch/di"
  4. "github.com/kataras/golog"
  5. "github.com/kataras/iris/context"
  6. )
  7. // def is the default herp value which can be used for dependencies share.
  8. var def = New()
  9. // Dispater contains the Dependencies which will be binded
  10. // to the controller(s) or handler(s) that can be created
  11. // using the Dispater's `Handler` and `Controller` methods.
  12. //
  13. // This is not exported for being used by everyone, use it only when you want
  14. // to share heroes between multi mvc.go#Application
  15. // or make custom hero handlers that can be used on the standard
  16. // iris' APIBuilder. The last one reason is the most useful here,
  17. // although end-devs can use the `MakeHandler` as well.
  18. //
  19. // For a more high-level structure please take a look at the "mvc.go#Application".
  20. type Dispater struct {
  21. values di.Values
  22. }
  23. // New returns a new Dispater, a container for dependencies and a factory
  24. // for handlers and controllers, this is used internally by the `mvc#Application` structure.
  25. // Please take a look at the structure's documentation for more information.
  26. func New() *Dispater {
  27. return &Dispater{
  28. values: di.NewValues(),
  29. }
  30. }
  31. // Dependencies returns the dependencies collection if the default hero,
  32. // those can be modified at any way but before the consumer `Handler`.
  33. func Dependencies() *di.Values {
  34. return def.Dependencies()
  35. }
  36. // Dependencies returns the dependencies collection of this hero,
  37. // those can be modified at any way but before the consumer `Handler`.
  38. func (h *Dispater) Dependencies() *di.Values {
  39. return &h.values
  40. }
  41. // Register adds one or more values as dependencies.
  42. // The value can be a single struct value-instance or a function
  43. // which has one input and one output, the input should be
  44. // an `iris.Context` and the output can be any type, that output type
  45. // will be binded to the handler's input argument, if matching.
  46. //
  47. // Example: `.Register(loggerService{prefix: "dev"}, func(ctx iris.Context) User {...})`.
  48. func Register(values ...interface{}) *Dispater {
  49. return def.Register(values...)
  50. }
  51. // Register adds one or more values as dependencies.
  52. // The value can be a single struct value-instance or a function
  53. // which has one input and one output, the input should be
  54. // an `iris.Context` and the output can be any type, that output type
  55. // will be binded to the handler's input argument, if matching.
  56. //
  57. // Example: `.Register(loggerService{prefix: "dev"}, func(ctx iris.Context) User {...})`.
  58. func (h *Dispater) Register(values ...interface{}) *Dispater {
  59. h.values.Add(values...)
  60. return h
  61. }
  62. // Clone creates and returns a new hero with the default Dependencies.
  63. // It copies the default's dependencies and returns a new hero.
  64. func Clone() *Dispater {
  65. return def.Clone()
  66. }
  67. // Clone creates and returns a new hero with the parent's(current) Dependencies.
  68. // It copies the current "h" dependencies and returns a new hero.
  69. func (h *Dispater) Clone() *Dispater {
  70. child := New()
  71. child.values = h.values.Clone()
  72. return child
  73. }
  74. // Handler accepts a "handler" function which can accept any input arguments that match
  75. // with the Dispater's `Dependencies` and any output result; like string, int (string,int),
  76. // custom structs, Result(View | Response) and anything you can imagine.
  77. // It returns a standard `iris/context.Handler` which can be used anywhere in an Iris Application,
  78. // as middleware or as simple route handler or subdomain's handler.
  79. func Handler(handler interface{}) context.Handler {
  80. return def.Handler(handler)
  81. }
  82. // Handler accepts a handler "fn" function which can accept any input arguments that match
  83. // with the Dispater's `Dependencies` and any output result; like string, int (string,int),
  84. // custom structs, Result(View | Response) and anything you can imagine.
  85. // It returns a standard `iris/context.Handler` which can be used anywhere in an Iris Application,
  86. // as middleware or as simple route handler or subdomain's handler.
  87. func (h *Dispater) Handler(fn interface{}) context.Handler {
  88. handler, err := makeHandler(fn, h.values.Clone()...)
  89. if err != nil {
  90. golog.Errorf("Dispatch Handler: %v", err)
  91. }
  92. return handler
  93. }