main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "context"
  4. "io"
  5. "log"
  6. "os"
  7. "github.com/fgm/izidic"
  8. "code.osinet.fr/fgm/sqs_demo/services"
  9. )
  10. func main() {
  11. os.Exit(int(main2(os.Stdout, os.Args[0], os.Args[1:])))
  12. }
  13. func main2(w io.Writer, name string, args []string) (exitCode byte) {
  14. ctx := context.Background()
  15. dic := Resolve(w, name, args)
  16. lister := dic.MustService(services.SvcLister).(func(ctx context.Context) string)
  17. qURL := lister(ctx)
  18. if false {
  19. receiver := dic.MustService(services.SvcReceiver).(func(ctx context.Context, qURL string))
  20. receiver(ctx, qURL)
  21. }
  22. consumer := dic.MustService(services.SvcConsumer).(func(ctx context.Context, qURL string) error)
  23. err := consumer(ctx, qURL)
  24. if err != nil {
  25. log.Printf("error in consumer: %v, aborting", err)
  26. exitCode = 1
  27. }
  28. log.Printf("exiting cleanly")
  29. return 0
  30. }
  31. func Resolve(w io.Writer, name string, args []string) *izidic.Container {
  32. dic := izidic.New()
  33. dic.Store(services.PName, name)
  34. dic.Store(services.PArgs, args)
  35. dic.Store(services.PWriter, w)
  36. dic.Store("handler", services.Handler(HandleDummy))
  37. dic.Register(services.SvcClient, services.SQSClientService)
  38. dic.Register(services.SvcConsumer, services.ConsumerService)
  39. dic.Register(services.SvcFlags, services.FlagsService)
  40. dic.Register(services.SvcLister, services.ListerService)
  41. dic.Register(services.SvcLogger, services.LoggerService)
  42. dic.Register(services.SvcReceiver, services.ReceiverService)
  43. dic.MustService(services.SvcFlags) // Store generated params before freeze.
  44. dic.Freeze()
  45. return dic
  46. }