Skip to content

Commit

Permalink
Extract out from cfg the s3 parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Leduc-Hamel committed Mar 9, 2021
1 parent aa1d725 commit e003c99
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
27 changes: 24 additions & 3 deletions pkg/app/trieugene.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,29 @@ func NewTrieugeneStore(cfg *config.Config, kind string, key string, value string
}

func (t *trieugene) Run(ctx context.Context) error {
store := store.NewS3Production(t.cfg)
store := store.NewS3(&store.S3Params{
AccessKey: t.cfg.S3AccessKey(),
SecretKey: t.cfg.S3SecretKey(),
URL: t.cfg.S3URL(),
Bucket: t.cfg.S3Bucket(),
Region: t.cfg.S3Region(),
DisableSSL: true,
S3ForcePathStyle: true,
})

err := NewFaktory(t.cfg, store).Run(ctx)

return err
}

func (t *trieugeneDev) Run(ctx context.Context) error {
store := store.NewS3(t.cfg)
store := store.NewS3(&store.S3Params{
AccessKey: t.cfg.S3AccessKey(),
SecretKey: t.cfg.S3SecretKey(),
URL: t.cfg.S3URL(),
Bucket: t.cfg.S3Bucket(),
Region: t.cfg.S3Region(),
})

run(setupDevelopment(t.cfg))

Expand All @@ -61,7 +76,13 @@ func (t *trieugeneDev) Run(ctx context.Context) error {
}

func (t *trieugeneStore) Run(ctx context.Context) error {
store := store.NewS3(t.cfg)
store := store.NewS3(&store.S3Params{
AccessKey: t.cfg.S3AccessKey(),
SecretKey: t.cfg.S3SecretKey(),
URL: t.cfg.S3URL(),
Bucket: t.cfg.S3Bucket(),
Region: t.cfg.S3Region(),
})

run(setupDevelopment(t.cfg))

Expand Down
50 changes: 20 additions & 30 deletions pkg/store/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,41 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/mlhamel/trieugene/pkg/config"
)

type S3Params struct {
AccessKey string
SecretKey string
URL string
Bucket string
Region string
DisableSSL bool
S3ForcePathStyle bool
}

type S3 struct {
cfg *config.Config
params *S3Params
client *s3.S3
}

const shortDuration = 100 * time.Millisecond

func NewS3(cfg *config.Config) Store {
conf := aws.Config{
Credentials: credentials.NewStaticCredentials(cfg.S3AccessKey(), cfg.S3SecretKey(), ""),
Endpoint: aws.String(cfg.S3URL()),
Region: aws.String(cfg.S3Region()),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
}

client := s3.New(session.New(&conf))

return &S3{cfg: cfg, client: client}
}

func NewS3Production(cfg *config.Config) Store {
func NewS3(params *S3Params) Store {
conf := aws.Config{
Credentials: credentials.NewStaticCredentials(cfg.S3AccessKey(), cfg.S3SecretKey(), ""),
Endpoint: aws.String(cfg.S3URL()),
Region: aws.String(cfg.S3Region()),
Credentials: credentials.NewStaticCredentials(params.AccessKey, params.SecretKey, ""),
Endpoint: aws.String(params.URL),
Region: aws.String(params.Region),
DisableSSL: aws.Bool(params.DisableSSL),
S3ForcePathStyle: aws.Bool(params.S3ForcePathStyle),
}

client := s3.New(session.New(&conf))

return &S3{cfg: cfg, client: client}
return &S3{client: client, params: params}
}

func (s *S3) Setup(ctx context.Context) error {
s.cfg.Logger().Debug().Str("bucket", s.cfg.S3Bucket()).Msg("Starting Creating bucket")
input := &s3.CreateBucketInput{Bucket: aws.String(s.cfg.S3Bucket())}
input := &s3.CreateBucketInput{Bucket: aws.String(s.params.Bucket)}
_, err := s.client.CreateBucket(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
Expand All @@ -65,7 +61,6 @@ func (s *S3) Setup(ctx context.Context) error {
return err
}
}
s.cfg.Logger().Debug().Str("bucket", s.cfg.S3Bucket()).Msg("Succeed Creating bucket")
return nil
}

Expand All @@ -75,24 +70,19 @@ func (s *S3) Persist(ctx context.Context, data *Data) error {
body, err := json.Marshal(data)

if err != nil {
s.cfg.Logger().Error().Err(err).Str("key", key).Msg("Failed marshaling data for persistence")
return err
}

bodyStr := string(body)

s.cfg.Logger().Debug().Str("key", key).Str("body", bodyStr).Msg("Starting Persistence")
_, err = s.client.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.cfg.S3Bucket()),
Bucket: aws.String(s.params.Bucket),
Key: aws.String(key),
Body: strings.NewReader(bodyStr),
})
if err != nil {
s.cfg.Logger().Error().Err(err).Str("key", key).Msg("Failed persistence")
return err
}

s.cfg.Logger().Debug().Str("key", key).Msg("Succeed Persistence")

return nil
}
10 changes: 9 additions & 1 deletion services/rougecombien/pkg/apps/rougecombien.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ type Rougecombien struct {
}

func NewRougecombien(cfg *config.Config) *Rougecombien {
store := store.NewS3(cfg)
store := store.NewS3(&store.S3Params{
AccessKey: cfg.S3AccessKey(),
SecretKey: cfg.S3SecretKey(),
URL: cfg.S3URL(),
Bucket: cfg.S3Bucket(),
Region: cfg.S3Region(),
DisableSSL: true,
S3ForcePathStyle: true,
})

return &Rougecombien{
cfg: cfg,
Expand Down

0 comments on commit e003c99

Please sign in to comment.