debug.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  83. }
  84. func (debug *Debugger) CreateTask() *DebugTaks {
  85. task := &DebugTaks{
  86. ID: primitive.NewObjectID().Hex(),
  87. Status: "",
  88. Created: time.Now().Unix(),
  89. Debug: debug,
  90. }
  91. debug.Tasks = append(debug.Tasks, task)
  92. return task
  93. }
  94. func (debug *Debugger) EventStream() func(context.Context) (interface{}, *errs.Error) {
  95. return func(ctx context.Context) (resp interface{}, err *errs.Error) {
  96. if err = debug.Hub.UpgradeConnection(
  97. ctx,
  98. debug.ChannelID,
  99. ); err != nil {
  100. return
  101. }
  102. resp = true
  103. return
  104. }
  105. }
  106. func NewDebug() *Debugger {
  107. return &Debugger{
  108. ChannelID: "debug",
  109. Tasks: []*DebugTaks{},
  110. Hub: sse.NewSSEHub(&sse.SSEOptions{
  111. URI: os.Getenv("REDIS_URI"),
  112. Password: os.Getenv("REDIS_PASSWD"),
  113. ChannelCollection: "debugger",
  114. }),
  115. }
  116. }