From 8d00cb7bac776c739f7cc66f0abc70b1f3adebbe Mon Sep 17 00:00:00 2001 From: Maya Sergeeva Date: Mon, 3 Oct 2022 19:42:42 +0300 Subject: [PATCH] Bump version, fix s3 object saving --- Makefile | 6 ++++-- configuration/defaults/info.yaml | 2 +- pkg/repository/config.go | 4 +--- pkg/repository/files/config.go | 6 ++++-- pkg/repository/s3/config.go | 12 ++++++------ pkg/service/config.go | 2 +- pkg/service/page.go | 2 +- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 1808003..e6e606f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ IMAGE_NAME = spacetabio/prerender-go -IMAGE_VERSION = 0.3.1 +IMAGE_VERSION = v1.0.0 deps: go mod vendor @@ -83,4 +83,6 @@ image_build: docker build -t ${IMAGE_NAME}:${IMAGE_VERSION} . image_push: - docker push ${IMAGE_NAME}:${IMAGE_VERSION} \ No newline at end of file + docker push ${IMAGE_NAME}:${IMAGE_VERSION} + +image: image_build image_push \ No newline at end of file diff --git a/configuration/defaults/info.yaml b/configuration/defaults/info.yaml index d7ae940..cd8b63f 100644 --- a/configuration/defaults/info.yaml +++ b/configuration/defaults/info.yaml @@ -4,7 +4,7 @@ defaults: alias: "prerender" name: "SPA prerender" about: "Приложение для рендера SPA приложений как html странички" - version: "0.4.0" + version: "1.0.1" docs: "---" contacts: "ask@spacetab.io" copyright: "SpaceTab © 2022" diff --git a/pkg/repository/config.go b/pkg/repository/config.go index 0780de1..d6b0575 100644 --- a/pkg/repository/config.go +++ b/pkg/repository/config.go @@ -2,7 +2,6 @@ package repository import ( "errors" - "strings" cfg "github.com/spacetab-io/prerender-go/configuration" "github.com/spacetab-io/prerender-go/pkg/repository/files" @@ -20,9 +19,8 @@ var ErrUnknownType = errors.New("storage type is unknown or not set") func NewRepository(storageCfg cfg.StorageConfig) (service.Repository, error) { switch storageCfg.Type { case LocalStorage: - return files.NewStorage(strings.TrimRight(storageCfg.Local.StoragePath, "/")), nil + return files.NewStorage(storageCfg.Local.StoragePath), nil case S3Storage: - //return bucket.NewRepository(storageCfg.S3) return s3.NewStorage(storageCfg.S3) } diff --git a/pkg/repository/files/config.go b/pkg/repository/files/config.go index e0a4e79..583bac2 100644 --- a/pkg/repository/files/config.go +++ b/pkg/repository/files/config.go @@ -1,11 +1,13 @@ package files import ( + "context" "errors" "fmt" "io/ioutil" "os" "path/filepath" + "strings" "github.com/spacetab-io/prerender-go/pkg/models" ) @@ -15,10 +17,10 @@ type storage struct { } func NewStorage(folderPath string) *storage { //nolint:golint - return &storage{folderPath} + return &storage{path: strings.TrimRight(folderPath, "/")} } -func (s storage) SaveData(pd *models.PageData) error { +func (s storage) SaveData(_ context.Context, pd *models.PageData) error { if pd == nil { return errors.New("nil page data") } diff --git a/pkg/repository/s3/config.go b/pkg/repository/s3/config.go index dfe73ae..941f63d 100644 --- a/pkg/repository/s3/config.go +++ b/pkg/repository/s3/config.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" @@ -25,7 +26,7 @@ func NewStorage(cfg cfg.S3Config) (*storage, error) { //nolint:golint if service == s3.ServiceID { return aws.Endpoint{ PartitionID: cfg.Endpoint.PartitionID, - URL: cfg.Endpoint.URL, + URL: strings.TrimRight(cfg.Endpoint.URL, "/"), SigningName: cfg.Endpoint.SigningName, SigningRegion: cfg.Endpoint.SigningRegion, SigningMethod: cfg.Endpoint.SigningMethod, @@ -36,7 +37,6 @@ func NewStorage(cfg cfg.S3Config) (*storage, error) { //nolint:golint return aws.Endpoint{}, fmt.Errorf("unknown endpoint requested") }) - // Подгружаем конфигрурацию из ~/.aws/* awsCfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolverWithOptions(customResolver), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider( @@ -55,11 +55,11 @@ func NewStorage(cfg cfg.S3Config) (*storage, error) { //nolint:golint return s, nil } -func (s storage) SaveData(pd *models.PageData) error { +func (s *storage) SaveData(ctx context.Context, pd *models.PageData) error { // Upload the file to S3. - _, err := s.client.PutObject(context.TODO(), &s3.PutObjectInput{ - Bucket: aws.String(s.cfg.Bucket.Name), - Key: aws.String(s.cfg.Bucket.Folder + pd.FileName), + _, err := s.client.PutObject(ctx, &s3.PutObjectInput{ + Bucket: aws.String(strings.Trim(s.cfg.Bucket.Name, "/")), + Key: aws.String(fmt.Sprintf("%s/%s", strings.Trim(s.cfg.Bucket.Folder, "/"), pd.FileName)), Body: bytes.NewReader(pd.Body), ContentType: aws.String("text/html; charset=utf-8"), }) diff --git a/pkg/service/config.go b/pkg/service/config.go index 20b05e6..2398274 100644 --- a/pkg/service/config.go +++ b/pkg/service/config.go @@ -104,5 +104,5 @@ type Service interface { } type Repository interface { - SaveData(pd *models.PageData) error + SaveData(ctx context.Context, pd *models.PageData) error } diff --git a/pkg/service/page.go b/pkg/service/page.go index 5adc8ca..c1fc194 100644 --- a/pkg/service/page.go +++ b/pkg/service/page.go @@ -150,7 +150,7 @@ func (s *service) RenderPages(pages []*models.PageData, maxWorkers int) error { p.SuccessRender = true - if err := s.r.SaveData(p); err != nil { + if err := s.r.SaveData(ctx, p); err != nil { log.Printf("save data error: %v", err) } else { p.SuccessStoring = true