queue.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package web
  2. import (
  3. "log"
  4. "net/http"
  5. "time"
  6. "github.com/gin-contrib/sessions"
  7. "github.com/gin-gonic/gin"
  8. "code.osinet.fr/fgm/sqs_demo/back/services/redriver"
  9. )
  10. func makeQueueHandler(rd redriver.Redriver, ms redriver.MessageStore) gin.HandlerFunc {
  11. return func(c *gin.Context) {
  12. ctx := c.Request.Context()
  13. qName := c.Param("name")
  14. sess := sessions.Default(c)
  15. flashes := sess.Flashes()
  16. _ = sess.Save()
  17. t0 := time.Now()
  18. qi, err := rd.GetQueueInfo(ctx, qName)
  19. infoLatency := time.Since(t0)
  20. if err != nil {
  21. log.Printf("failed getting info for queue %q: %v", qName, err)
  22. c.HTML(http.StatusInternalServerError, "500", nil)
  23. return
  24. }
  25. items, err := rd.GetQueueItems(ctx, qName)
  26. if err != nil {
  27. log.Printf("failed getting items for queue %q: %v", qName, err)
  28. c.HTML(http.StatusInternalServerError, "500", nil)
  29. return
  30. }
  31. for _, item := range items {
  32. ms.Set(item)
  33. }
  34. itemsLatency := time.Since(t0) - infoLatency
  35. c.HTML(http.StatusOK, "queue-get", gin.H{
  36. "flashes": flashes,
  37. "info": qi,
  38. "isDLQ": qi.IsDLQ(),
  39. "items": items,
  40. "latency": map[string]time.Duration{
  41. "info": infoLatency,
  42. "items": itemsLatency,
  43. },
  44. })
  45. }
  46. }