debug.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package api
  2. import (
  3. "encoding/json"
  4. "os"
  5. "time"
  6. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
  7. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/sse"
  8. context "github.com/kataras/iris/v12/context"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. type DebugStage struct {
  12. DebugEvent `json:",inline"`
  13. Events []*DebugEvent `json:"events"`
  14. }
  15. type DebugEvent struct {
  16. ID string `json:"id"`
  17. Type string `json:"type"`
  18. Status string `json:"status"`
  19. Created int64 `json:"created"`
  20. Error *errs.Error `json:"error"`
  21. Data interface{} `json:"data"`
  22. }
  23. type DebugTaks struct {
  24. ID string `json:"id"`
  25. Status string `json:"status"`
  26. Created int64 `json:"created"`
  27. Stages []*DebugStage `json:"stages"`
  28. CurrentStage *DebugStage `json:"-"`
  29. Debug *Debugger `json:"-"`
  30. }
  31. func NewDebugTaks() *DebugTaks {
  32. return &DebugTaks{
  33. Stages: []*DebugStage{},
  34. CurrentStage: &DebugStage{},
  35. }
  36. }
  37. func (debug *DebugTaks) Stage(id string) *DebugStage {
  38. stage := &DebugStage{}
  39. stage.ID = id
  40. debug.CurrentStage = stage
  41. return stage
  42. }
  43. func (stage *DebugStage) PushEvent(event *DebugEvent) {
  44. stage.Events = append(stage.Events, event)
  45. }
  46. func (debug *DebugTaks) Event(eventType, eventId string) *DebugEvent {
  47. event := &DebugEvent{
  48. ID: eventId,
  49. Type: eventType,
  50. Created: time.Now().Unix(),
  51. }
  52. debug.CurrentStage.PushEvent(event)
  53. return event
  54. }
  55. func (task *DebugTaks) Finalize() {
  56. var (
  57. debug = task.Debug
  58. out []byte
  59. err error
  60. channel = debug.Hub.GetChannel(debug.ChannelID)
  61. )
  62. if out, err = json.Marshal(task); err != nil {
  63. channel.Emit(&sse.Event{
  64. Kind: "debugger.error",
  65. Payload: err.Error(),
  66. })
  67. } else {
  68. channel.Emit(&sse.Event{
  69. Kind: "request",
  70. Payload: string(out),
  71. })
  72. }
  73. }
  74. type Debugger struct {
  75. ChannelID string
  76. Tasks []*DebugTaks
  77. Hub *sse.SSEHub
  78. }
  79. func (debug *Debugger) Handler() func(context.Context) {
  80. return func(ctx context.Context) {
  81. ctx.Values().Set("#debug", debug.CreateTask())
  82. ctx.Next()
  83. }
  84. }
  85. func (debug *Debugger) CreateTask() *DebugTaks {
  86. task := &DebugTaks{
  87. ID: primitive.NewObjectID().Hex(),
  88. Status: "",
  89. Created: time.Now().Unix(),
  90. Debug: debug,
  91. }
  92. debug.Tasks = append(debug.Tasks, task)
  93. return task
  94. }
  95. func (debug *Debugger) EventStream() func(context.Context) (interface{}, *errs.Error) {
  96. return func(ctx context.Context) (resp interface{}, err *errs.Error) {
  97. if err = debug.Hub.UpgradeConnection(
  98. ctx,
  99. debug.ChannelID,
  100. ); err != nil {
  101. return
  102. }
  103. resp = true
  104. return
  105. }
  106. }
  107. func NewDebug() *Debugger {
  108. return &Debugger{
  109. ChannelID: "debug",
  110. Tasks: []*DebugTaks{},
  111. Hub: sse.NewSSEHub(&sse.SSEOptions{
  112. URI: os.Getenv("REDIS_URI"),
  113. Password: os.Getenv("REDIS_PASSWD"),
  114. ChannelCollection: "debugger",
  115. }),
  116. }
  117. }