services.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package services
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "log"
  7. "time"
  8. "github.com/fgm/izidic"
  9. )
  10. const (
  11. // Flags
  12. PAddr = "addr"
  13. PPrefix = "prefix"
  14. PProfile = "profile"
  15. PQName = "queue-name"
  16. PRegion = "region"
  17. PCSRFSecret = "csrf-secret"
  18. PStoreSecret = "store-secret"
  19. PTTL = "ttl"
  20. PURL = "url"
  21. PVTO = "vto"
  22. PWait = "wait"
  23. // Non-flags
  24. PArgs = "args"
  25. PHandler = "handler"
  26. PName = "name"
  27. PWriter = "writer"
  28. // Services
  29. SvcClient = "sqs"
  30. SvcConsumer = "consume-message"
  31. SvcFlags = "flags"
  32. SvcHttp = "http"
  33. SvcLister = "lister"
  34. SvcLogger = "logger"
  35. SvcMessageStore = "message-store"
  36. SvcProducer = "producer"
  37. SvcReceiver = "receiver"
  38. SvcRedriver = "redriver"
  39. SvcRenderer = "renderer"
  40. )
  41. func FlagsService(dic *izidic.Container) (any, error) {
  42. fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError)
  43. addr := fs.String(PAddr, ":8080", "The IP address on which to listen")
  44. profile := fs.String(PProfile, "test-profile", "The AWS profile")
  45. region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to")
  46. qName := fs.String(PQName, "dummy-queue", "The queue name")
  47. csrfSecret := fs.String(PCSRFSecret, "csrfSecret", "The CSRF secret")
  48. prefix := fs.String(PPrefix, "", "The queue prefix to filter the main list")
  49. storeSecret := fs.String(PStoreSecret, "storeSecret", "The session store secret")
  50. sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL")
  51. ttl := fs.Duration(PTTL, 10*time.Minute, "The message store TTL")
  52. vto := fs.Duration(PVTO, 10*time.Minute, "The redrive visibility timeout")
  53. wait := fs.Int(PWait, 3, "The maximum number of seconds to wait when receiving messages")
  54. if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil {
  55. return nil, fmt.Errorf("cannot obtain CLI args")
  56. }
  57. // Durations are signed, and Duration.Round rounds down, but we want positive durations, rounded up.
  58. *vto = (vto.Abs() + 500*time.Millisecond).Round(time.Second)
  59. dic.Store(PAddr, *addr)
  60. dic.Store(PProfile, *profile)
  61. dic.Store(PQName, *qName)
  62. dic.Store(PRegion, *region)
  63. dic.Store(PCSRFSecret, []byte(*csrfSecret))
  64. dic.Store(PPrefix, *prefix)
  65. dic.Store(PStoreSecret, []byte(*storeSecret))
  66. dic.Store(PTTL, *ttl)
  67. dic.Store(PURL, *sqsURL)
  68. dic.Store(PVTO, *vto)
  69. dic.Store(PWait, *wait)
  70. return fs, nil
  71. }
  72. // LoggerService is an izidic.Service also containing a one-time initialization action.
  73. func LoggerService(dic *izidic.Container) (any, error) {
  74. w := dic.MustParam(PWriter).(io.Writer)
  75. log.SetOutput(w) // Support dependency code not taking an injected logger.
  76. logger := log.New(w, "", log.LstdFlags)
  77. return logger, nil
  78. }