delete.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 makeDeleteHandler(rd redriver.Redriver) gin.HandlerFunc {
  12. return func(c *gin.Context) {
  13. ctx := c.Request.Context()
  14. qName := c.Param("name")
  15. redirect := "/queue/" + qName
  16. sess := sessions.Default(c)
  17. // Do not consume sess.Flashes(): this is a redirect-only handler, and they would be lost.
  18. defer func() {
  19. _ = sess.Save()
  20. c.Redirect(http.StatusSeeOther, redirect)
  21. }()
  22. req := c.Request
  23. if err := req.ParseForm(); err != nil {
  24. log.Printf("Failed to parse deletion confirm form for queue %s: %v",
  25. qName, err)
  26. sess.AddFlash(fmt.Sprintf("Failed to parsed deletion confirmm form for queue %s",
  27. qName))
  28. return
  29. }
  30. ids := parseIDs(req.Form, validateUint)
  31. var messages []redriver.Message
  32. if len(ids) == 0 {
  33. flash := fmt.Sprintf("Got no message to delete from queue %q", qName)
  34. log.Print(flash)
  35. sess.AddFlash(flash)
  36. return
  37. }
  38. messages = parseMessages(req.Form, ids)
  39. keys := make([]redriver.ItemsKeys, len(messages))
  40. for i, msg := range messages {
  41. keys[i] = redriver.ItemsKeys{
  42. MessageID: msg.MessageId,
  43. ReceiptHandle: msg.ReceiptHandle,
  44. }
  45. }
  46. t0 := time.Now()
  47. err := rd.DeleteItems(ctx, qName, keys)
  48. latency := time.Since(t0)
  49. if err != nil {
  50. log.Printf("failed deleting %d items from queue %q: %v",
  51. len(keys), qName, err)
  52. sess.AddFlash(fmt.Sprintf("Failed deleting %d items from queue %q",
  53. len(keys), qName))
  54. return
  55. }
  56. sess.AddFlash(fmt.Sprintf("Deleted %d items from queue %s in %v", len(keys),
  57. qName, latency))
  58. }
  59. }