services.go 2.1 KB

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