debug.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Stages = append(debug.Stages, stage)
  41. debug.CurrentStage = stage
  42. return stage
  43. }
  44. func (stage *DebugStage) PushEvent(event *DebugEvent) {
  45. stage.Events = append(stage.Events, event)
  46. }
  47. func (debug *DebugTaks) Event(eventType, eventId string) *DebugEvent {
  48. event := &DebugEvent{
  49. ID: eventId,
  50. Type: eventType,
  51. Created: time.Now().Unix(),
  52. }
  53. debug.CurrentStage.PushEvent(event)
  54. return event
  55. }
  56. func (task *DebugTaks) Finalize() {
  57. var (
  58. debug = task.Debug
  59. out []byte
  60. err error
  61. channel = debug.Hub.GetChannel(debug.ChannelID)
  62. )
  63. if out, err = json.Marshal(task); err != nil {
  64. channel.Emit(&sse.Event{
  65. Kind: "debugger.error",
  66. Payload: err.Error(),
  67. })
  68. } else {
  69. channel.Emit(&sse.Event{
  70. Kind: "request",
  71. Payload: string(out),
  72. })
  73. }
  74. }
  75. type Debugger struct {
  76. ChannelID string
  77. Tasks []*DebugTaks
  78. Hub *sse.SSEHub
  79. }
  80. func (debug *Debugger) Handler() func(context.Context) {
  81. return func(ctx context.Context) {
  82. ctx.Values().Set("#debug", debug.CreateTask())
  83. ctx.Next()
  84. }
  85. }
  86. func (debug *Debugger) CreateTask() *DebugTaks {
  87. task := &DebugTaks{
  88. ID: primitive.NewObjectID().Hex(),
  89. Status: "",
  90. Created: time.Now().Unix(),
  91. Debug: debug,
  92. }
  93. debug.Tasks = append(debug.Tasks, task)
  94. return task
  95. }
  96. func (debug *Debugger) EventStream() func(context.Context) (interface{}, *errs.Error) {
  97. return func(ctx context.Context) (resp interface{}, err *errs.Error) {
  98. if err = debug.Hub.UpgradeConnection(
  99. ctx,
  100. debug.ChannelID,
  101. ); err != nil {
  102. return
  103. }
  104. resp = true
  105. return
  106. }
  107. }
  108. func NewDebug() *Debugger {
  109. return &Debugger{
  110. ChannelID: "debug",
  111. Tasks: []*DebugTaks{},
  112. Hub: sse.NewSSEHub(&sse.SSEOptions{
  113. URI: os.Getenv("REDIS_URI"),
  114. Password: os.Getenv("REDIS_PASSWD"),
  115. ChannelCollection: "debugger",
  116. }),
  117. }
  118. }