services.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package services
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "log"
  7. "github.com/fgm/izidic"
  8. )
  9. const (
  10. // Flags
  11. PAddr = "addr"
  12. PProfile = "profile"
  13. PQName = "queue-name"
  14. PRegion = "region"
  15. PURL = "url"
  16. PWait = "wait"
  17. // Non-flags
  18. PArgs = "args"
  19. PHandler = "handler"
  20. PName = "name"
  21. PWriter = "writer"
  22. // Services
  23. SvcClient = "sqs"
  24. SvcConsumer = "consumeMessage"
  25. SvcFlags = "flags"
  26. SvcHttp = "http"
  27. SvcLister = "lister"
  28. SvcLogger = "logger"
  29. SvcProducer = "producer"
  30. SvcReceiver = "receiver"
  31. SvcRedriver = "redriver"
  32. )
  33. func FlagsService(dic *izidic.Container) (any, error) {
  34. fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError)
  35. addr := fs.String(PAddr, ":8080", "The IP address on which to listen")
  36. profile := fs.String(PProfile, "test-profile", "The AWS profile")
  37. region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to")
  38. qName := fs.String(PQName, "dummy-queue", "The queue name")
  39. sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL")
  40. wait := fs.Int(PWait, 3, "The maximum number of seconds to wait when receiving messages")
  41. if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil {
  42. return nil, fmt.Errorf("cannot obtain CLI args")
  43. }
  44. dic.Store(PAddr, *addr)
  45. dic.Store(PProfile, *profile)
  46. dic.Store(PQName, *qName)
  47. dic.Store(PRegion, *region)
  48. dic.Store(PURL, *sqsURL)
  49. dic.Store(PWait, *wait)
  50. return fs, nil
  51. }
  52. // LoggerService is an izidic.Service also containing a one-time initialization action.
  53. func LoggerService(dic *izidic.Container) (any, error) {
  54. w := dic.MustParam(PWriter).(io.Writer)
  55. log.SetOutput(w) // Support dependency code not taking an injected logger.
  56. logger := log.New(w, "", log.LstdFlags)
  57. return logger, nil
  58. }