Skip to content

Commit

Permalink
Refactor thread related code (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
harryzcy authored May 27, 2023
1 parent 1ad2ff2 commit 89d95f3
Show file tree
Hide file tree
Showing 44 changed files with 339 additions and 352 deletions.
7 changes: 2 additions & 5 deletions api/emails/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
Expand All @@ -15,12 +14,10 @@ import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

type createClient struct {
dynamodbSvc *dynamodb.Client
sesv2Svd *sesv2.Client
Expand Down Expand Up @@ -59,7 +56,7 @@ func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.R

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(400, "invalid input"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
Expand All @@ -14,12 +13,10 @@ import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

type deleteClient struct {
cfg aws.Config
}
Expand All @@ -38,7 +35,7 @@ func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.R
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
9 changes: 3 additions & 6 deletions api/emails/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand All @@ -38,7 +35,7 @@ func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.R
return apiutil.NewErrorResponse(http.StatusBadRequest, "bad request: invalid messageID"), nil
}

result, err := email.Get(ctx, dynamodb.NewFromConfig(cfg), messageID)
result, err := email.GetAndRead(ctx, dynamodb.NewFromConfig(cfg), messageID)
if err != nil {
if err == email.ErrNotFound {
fmt.Println("email not found")
Expand Down
7 changes: 2 additions & 5 deletions api/emails/getContent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"

Expand All @@ -13,19 +12,17 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/getRaw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"

Expand All @@ -14,19 +13,17 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/harryzcy/mailbox/internal/datasource/storage"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"

Expand All @@ -14,19 +13,17 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/read/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"

Expand All @@ -13,19 +12,17 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/save/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
Expand All @@ -15,12 +14,10 @@ import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

type saveClient struct {
dynamodbSvc *dynamodb.Client
sesv2Svc *sesv2.Client
Expand Down Expand Up @@ -55,7 +52,7 @@ func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.R

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/send/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
Expand All @@ -15,12 +14,10 @@ import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

type sendClient struct {
dynamodbSvc *dynamodb.Client
sesv2Svc *sesv2.Client
Expand Down Expand Up @@ -54,7 +51,7 @@ func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.R
messageID := req.PathParameters["messageID"]
fmt.Printf("request params: [messagesID] %s\n", messageID)

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/trash/trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
7 changes: 2 additions & 5 deletions api/emails/untrash/untrash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand Down
10 changes: 4 additions & 6 deletions api/threads/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/harryzcy/mailbox/internal/email"
"github.com/harryzcy/mailbox/internal/env"
"github.com/harryzcy/mailbox/internal/thread"
"github.com/harryzcy/mailbox/internal/util/apiutil"
)

// AWS Region
var region = os.Getenv("REGION")

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

fmt.Println("request received")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(env.Region))
if err != nil {
fmt.Printf("unable to load SDK config, %v\n", err)
return apiutil.NewErrorResponse(http.StatusInternalServerError, "internal error"), nil
Expand All @@ -38,7 +36,7 @@ func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (apiutil.R
return apiutil.NewErrorResponse(http.StatusBadRequest, "bad request: invalid threadID"), nil
}

result, err := email.GetThreadWithEmails(ctx, dynamodb.NewFromConfig(cfg), threadID)
result, err := thread.GetThreadWithEmails(ctx, dynamodb.NewFromConfig(cfg), threadID)
if err != nil {
if err == email.ErrNotFound {
fmt.Println("thread not found")
Expand Down
Loading

0 comments on commit 89d95f3

Please sign in to comment.