Skip to content

Commit 2db317e

Browse files
Refactor video handler: integrate S3 client and update initialization
1 parent a0fdd1e commit 2db317e

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

cmd/worker/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/Prateet-Github/worker-go/internal/config"
77
"github.com/Prateet-Github/worker-go/internal/handlers"
88
"github.com/Prateet-Github/worker-go/internal/queue"
9+
"github.com/Prateet-Github/worker-go/internal/s3"
910

1011
"github.com/hibiken/asynq"
1112
)
@@ -15,7 +16,10 @@ func main() {
1516

1617
server := queue.NewServer(cfg)
1718

18-
videoHandler := handlers.NewVideoHandler()
19+
s3Client := s3.NewClient(cfg)
20+
videoHandler := handlers.NewVideoHandler(
21+
s3Client,
22+
)
1923

2024
mux := asynq.NewServeMux()
2125

internal/handlers/video.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import (
66
"log"
77

88
"github.com/Prateet-Github/worker-go/internal/queue"
9+
"github.com/aws/aws-sdk-go-v2/service/s3"
910
"github.com/hibiken/asynq"
1011
)
1112

12-
type VideoHandler struct{}
13+
type VideoHandler struct {
14+
s3Client *s3.Client
15+
}
1316

14-
func NewVideoHandler() *VideoHandler {
15-
return &VideoHandler{}
17+
func NewVideoHandler(
18+
s3Client *s3.Client,
19+
) *VideoHandler {
20+
return &VideoHandler{
21+
s3Client: s3Client,
22+
}
1623
}
1724

1825
func (h *VideoHandler) ProcessVideo(

internal/s3/client.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package s3
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/Prateet-Github/worker-go/internal/config"
8+
9+
awsconfig "github.com/aws/aws-sdk-go-v2/config"
10+
"github.com/aws/aws-sdk-go-v2/credentials"
11+
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
12+
)
13+
14+
func NewClient(cfg *config.Config) *awss3.Client {
15+
awsCfg, err := awsconfig.LoadDefaultConfig(
16+
context.Background(),
17+
awsconfig.WithRegion(cfg.AWSRegion),
18+
awsconfig.WithCredentialsProvider(
19+
credentials.NewStaticCredentialsProvider(
20+
cfg.AWSAccessKeyID,
21+
cfg.AWSSecretAccessKey,
22+
"",
23+
),
24+
),
25+
)
26+
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
return awss3.NewFromConfig(awsCfg)
32+
}

0 commit comments

Comments
 (0)