services.go 2.5 KB

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