services.go 2.3 KB

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