client.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/aws/aws-sdk-go-v2/aws"
  6. "github.com/aws/aws-sdk-go-v2/config"
  7. "github.com/aws/aws-sdk-go-v2/service/sqs"
  8. "github.com/fgm/izidic"
  9. )
  10. func SQSClientService(dic *izidic.Container) (any, error) {
  11. ctx := context.Background()
  12. profile := dic.MustParam(PProfile).(string)
  13. region := dic.MustParam(PRegion).(string)
  14. epr := endpointResolver{region: region}
  15. cfg, err := config.LoadDefaultConfig(ctx,
  16. config.WithRegion(region),
  17. config.WithSharedConfigProfile(profile),
  18. config.WithEndpointResolverWithOptions(epr),
  19. )
  20. if err != nil {
  21. return nil, fmt.Errorf("failed loading default AWS config: %w", err)
  22. }
  23. client := sqs.NewFromConfig(cfg)
  24. return client, nil
  25. }
  26. type endpointResolver struct {
  27. region string
  28. }
  29. func (e endpointResolver) ResolveEndpoint(service, region string, options ...interface{}) (aws.Endpoint, error) {
  30. if service != `SQS` {
  31. return aws.Endpoint{}, fmt.Errorf("trying to Resolve non-SQS service: %s", service)
  32. }
  33. ep := aws.Endpoint{
  34. URL: "http://localhost:4566/",
  35. HostnameImmutable: false,
  36. PartitionID: "000000000000",
  37. SigningName: "",
  38. SigningRegion: e.region,
  39. SigningMethod: "",
  40. Source: 0,
  41. }
  42. if region != "" {
  43. ep.URL = fmt.Sprintf("https://sqs.%s.amazonaws.com/", region)
  44. }
  45. return ep, nil
  46. }