queue.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package web
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "time"
  7. "github.com/gin-contrib/sessions"
  8. "github.com/gin-gonic/gin"
  9. "code.osinet.fr/fgm/sqs_demo/back/services/redriver"
  10. )
  11. func makeQueueHandler(rd redriver.Redriver) gin.HandlerFunc {
  12. return func(c *gin.Context) {
  13. ctx := c.Request.Context()
  14. qName := c.Param("name")
  15. sess := sessions.Default(c)
  16. flashes := sess.Flashes()
  17. sess.Clear()
  18. sess.AddFlash(fmt.Sprintf("Previous info acquired at: %v", time.Now()))
  19. sess.Save()
  20. t0 := time.Now()
  21. qi, err := rd.GetQueueInfo(ctx, qName)
  22. if err != nil {
  23. log.Printf("failed getting info for queue %q: %v", qName, err)
  24. c.JSON(http.StatusInternalServerError, nil)
  25. return
  26. }
  27. infoLatency := time.Since(t0)
  28. items, err := rd.GetQueueItems(ctx, qName)
  29. if err != nil {
  30. log.Printf("failed getting items for queue %q: %v", qName, err)
  31. c.JSON(http.StatusInternalServerError, nil)
  32. return
  33. }
  34. itemsLatency := time.Since(t0) - infoLatency
  35. c.HTML(http.StatusOK, "queue-get", map[string]any{
  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. }