debug.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "github.com/kataras/iris"
  9. context "github.com/kataras/iris/v12/context"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. type DebugStage struct {
  13. DebugEvent `json:",inline"`
  14. Events []*DebugEvent `json:"events"`
  15. }
  16. type DebugEvent struct {
  17. ID string `json:"id"`
  18. Type string `json:"type"`
  19. Status string `json:"status"`
  20. Created int64 `json:"created"`
  21. Error *errs.Error `json:"error"`
  22. Data interface{} `json:"data"`
  23. }
  24. type DebugTaks struct {
  25. ID string `json:"id"`
  26. Status string `json:"status"`
  27. Created int64 `json:"created"`
  28. Stages []*DebugStage `json:"stages"`
  29. CurrentStage *DebugStage `json:"-"`
  30. Debug *Debugger `json:"-"`
  31. }
  32. func NewDebugTaks() *DebugTaks {
  33. return &DebugTaks{
  34. Stages: []*DebugStage{},
  35. CurrentStage: &DebugStage{},
  36. }
  37. }
  38. func (debug *DebugTaks) Stage(id string) *DebugStage {
  39. stage := &DebugStage{}
  40. stage.ID = id
  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. }
  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) {
  96. return func(ctx context.Context) {
  97. if err := debug.Hub.UpgradeConnection(
  98. ctx,
  99. debug.ChannelID,
  100. ); err != nil {
  101. ctx.JSON(iris.Map{
  102. "err": err,
  103. })
  104. return
  105. }
  106. ctx.JSON(iris.Map{})
  107. }
  108. }
  109. func NewDebug() *Debugger {
  110. return &Debugger{
  111. ChannelID: "debug",
  112. Tasks: []*DebugTaks{},
  113. Hub: sse.NewSSEHub(&sse.SSEOptions{
  114. URI: os.Getenv("REDIS_URI"),
  115. Password: os.Getenv("REDIS_PASSWD"),
  116. ChannelCollection: "debugger",
  117. }),
  118. }
  119. }