services.go 1.9 KB

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