package services import ( "flag" "fmt" "io" "log" "github.com/fgm/izidic" ) const ( // Flags PAddr = "addr" PProfile = "profile" PQName = "queue-name" PRegion = "region" PCSRFSecret = "csrf-secret" PStoreSecret = "store-secret" PURL = "url" PWait = "wait" // Non-flags PArgs = "args" PHandler = "handler" PName = "name" PWriter = "writer" // Services SvcClient = "sqs" SvcConsumer = "consume_message" SvcFlags = "flags" SvcHttp = "http" SvcLister = "lister" SvcLogger = "logger" SvcMessageStore = "message_store" SvcProducer = "producer" SvcReceiver = "receiver" SvcRedriver = "redriver" SvcRenderer = "renderer" ) func FlagsService(dic *izidic.Container) (any, error) { fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError) addr := fs.String(PAddr, ":8080", "The IP address on which to listen") profile := fs.String(PProfile, "test-profile", "The AWS profile") region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to") qName := fs.String(PQName, "dummy-queue", "The queue name") csrfSecret := fs.String(PCSRFSecret, "csrfSecret", "The CSRF secret") storeSecret := fs.String(PStoreSecret, "storeSecret", "The session store secret") sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL") wait := fs.Int(PWait, 3, "The maximum number of seconds to wait when receiving messages") if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil { return nil, fmt.Errorf("cannot obtain CLI args") } dic.Store(PAddr, *addr) dic.Store(PProfile, *profile) dic.Store(PQName, *qName) dic.Store(PRegion, *region) dic.Store(PCSRFSecret, []byte(*csrfSecret)) dic.Store(PStoreSecret, []byte(*storeSecret)) dic.Store(PURL, *sqsURL) dic.Store(PWait, *wait) return fs, nil } // LoggerService is an izidic.Service also containing a one-time initialization action. func LoggerService(dic *izidic.Container) (any, error) { w := dic.MustParam(PWriter).(io.Writer) log.SetOutput(w) // Support dependency code not taking an injected logger. logger := log.New(w, "", log.LstdFlags) return logger, nil }