services.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. SvcRenderer = "renderer"
  33. )
  34. func FlagsService(dic *izidic.Container) (any, error) {
  35. fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError)
  36. addr := fs.String(PAddr, ":8080", "The IP address on which to listen")
  37. profile := fs.String(PProfile, "test-profile", "The AWS profile")
  38. region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to")
  39. qName := fs.String(PQName, "dummy-queue", "The queue name")
  40. sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL")
  41. wait := fs.Int(PWait, 3, "The maximum number of seconds to wait when receiving messages")
  42. if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil {
  43. return nil, fmt.Errorf("cannot obtain CLI args")
  44. }
  45. dic.Store(PAddr, *addr)
  46. dic.Store(PProfile, *profile)
  47. dic.Store(PQName, *qName)
  48. dic.Store(PRegion, *region)
  49. dic.Store(PURL, *sqsURL)
  50. dic.Store(PWait, *wait)
  51. return fs, nil
  52. }
  53. // LoggerService is an izidic.Service also containing a one-time initialization action.
  54. func LoggerService(dic *izidic.Container) (any, error) {
  55. w := dic.MustParam(PWriter).(io.Writer)
  56. log.SetOutput(w) // Support dependency code not taking an injected logger.
  57. logger := log.New(w, "", log.LstdFlags)
  58. return logger, nil
  59. }