Skip to content

Commit

Permalink
Make LoadAWSConfig a shared function
Browse files Browse the repository at this point in the history
  • Loading branch information
vmilovanovicc committed Sep 28, 2023
1 parent 6c48550 commit 86d56c5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
19 changes: 19 additions & 0 deletions backend/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package backend

import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"log"
)

var region = "eu-central-1"

// LoadAWSConfig loads the Shared AWS Configuration (~/.aws/config)
func LoadAWSConfig() (cfg aws.Config) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
log.Fatalf("failed to load configuration, %v\n", err)
}
return cfg
}
15 changes: 2 additions & 13 deletions backend/text_to_speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package backend
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/polly"
"github.com/aws/aws-sdk-go-v2/service/polly/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
Expand Down Expand Up @@ -32,12 +31,7 @@ func GetTargetVoice(language string) (string, error) {

// CreateBucket creates an S3 bucket to store the output of Amazon Polly Synthesis Tasks i.e. audio streams.
func CreateBucket(bucketName string) (location string, err error) {
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("eu-central-1"))
if err != nil {
log.Fatalf("failed to load configuration, %v\n", err)
}

cfg := LoadAWSConfig()
client := s3.NewFromConfig(cfg)
bucketLocationConstraint := types2.BucketLocationConstraintEuCentral1

Expand All @@ -60,12 +54,7 @@ func CreateBucket(bucketName string) (location string, err error) {

// GetSpeechSynthesisTaskId function creates a synthesis task which converts provided text into an audio stream.
func GetSpeechSynthesisTaskId(text, bucketName, languageCode, targetVoice string) (taskId, objectName string, err error) {
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("eu-central-1"))
if err != nil {
log.Fatalf("failed to load configuration, %v\n", err)
}

cfg := LoadAWSConfig()
client := polly.NewFromConfig(cfg)

outputFormat := types.OutputFormatMp3
Expand Down
10 changes: 2 additions & 8 deletions backend/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package backend
import (
context "context"
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/translate"
"log"
)

// TranslateText detects the source language and translates the text into a desired language.
func TranslateText(text, language string) (string, error) {
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("eu-central-1"))
if err != nil {
log.Fatalf("failed to load configuration, %v\n", err)
}

cfg := LoadAWSConfig()
client := translate.NewFromConfig(cfg)

// If auto is specified, Amazon Translate will call Amazon Comprehend to detect the source language.
Expand All @@ -30,7 +24,7 @@ func TranslateText(text, language string) (string, error) {
ctx := context.TODO()
resp, err := client.TranslateText(ctx, params)
if err != nil {
log.Fatalf("cannot translate, %v\n", err)
log.Fatalf("failed to translate, error: %v\n", err)
return "", err
}
fmt.Println(*resp.TranslatedText)
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"zobot/backend"
)
import "zobot/backend"

func main() {
// placeholder
Expand Down

0 comments on commit 86d56c5

Please sign in to comment.