services.go 1.6 KB

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