queue.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "items": items,
  39. "latency": map[string]time.Duration{
  40. "info": infoLatency,
  41. "items": itemsLatency,
  42. },
  43. })
  44. }
  45. }