Skip to content

Commit

Permalink
Merge pull request #6 from naohito-T/feature/container-to-crean-arch
Browse files Browse the repository at this point in the history
♻️ Fix: connect crean arch
  • Loading branch information
naohito-T authored Apr 14, 2024
2 parents 472b60a + fcd8ded commit 1531181
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 15 deletions.
10 changes: 7 additions & 3 deletions backend/build/localstack/init-aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ echo "init localstack"

echo "init dynamoDB"
# 指定のURLをhashしたやつをprimary keyにする
awslocal dynamodb create-table --table-name offline-tinyurl \
--attribute-definitions AttributeName=key,AttributeType=S \
--key-schema AttributeName=key,KeyType=HASH \
awslocal dynamodb create-table --table-name offline-tinyurls \
--attribute-definitions \
AttributeName=id,AttributeType=S \
AttributeName=originalURL,AttributeType=S \
AttributeName=createdAt,AttributeType=S \
--key-schema \
AttributeName=id,KeyType=HASH \
--billing-mode PAY_PER_REQUEST

awslocal dynamodb create-table --table-name offline-rate-limit \
Expand Down
3 changes: 1 addition & 2 deletions backend/domain/shorturl.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package domain

type ShortURL struct {
ID int `json:"id"`
ShortURL string `json:"short_url"`
ID string `json:"id"`
OriginalURL string `json:"original"`
CreatedAt string `json:"created_at"`
}
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ require (
require (
github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.13 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.20.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
Expand Down
4 changes: 4 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/aws/aws-sdk-go-v2/config v1.27.11 h1:f47rANd2LQEYHda2ddSCKYId18/8BhSR
github.com/aws/aws-sdk-go-v2/config v1.27.11/go.mod h1:SMsV78RIOYdve1vf36z8LmnszlRWkwMQtomCAI0/mIE=
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 h1:YuIB1dJNf1Re822rriUOTxopaHHvIq0l/pX3fwO+Tzs=
github.com/aws/aws-sdk-go-v2/credentials v1.17.11/go.mod h1:AQtFPsDH9bI2O+71anW6EKL+NcD7LG3dpKGMV4SShgo=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.13 h1:loQ4VSt3hTm9n8ST9jveArwmhqAc5aiRJXlxLPxCNTw=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.13/go.mod h1:RjdeQvzJuUf9jWj+ta+7l3VnVpDZ+RmtP/p+QdwRIpI=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 h1:FVJ0r5XTHSmIHJV6KuDmdYhEpvlHpiSd38RQWhut5J4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1/go.mod h1:zusuAeqezXzAB24LGuzuekqMAEgWkVYukBec3kr3jUg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg=
Expand All @@ -14,6 +16,8 @@ github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1 h1:dZXY07Dm59TxAjJcUfNMJHLDI/gLMxTRZefn2jFAVsw=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1/go.mod h1:lVLqEtX+ezgtfalyJs7Peb0uv9dEpAQP5yuq2O26R44=
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.20.4 h1:hSwDD19/e01z3pfyx+hDeX5T/0Sn+ZEnnTO5pVWKWx8=
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.20.4/go.mod h1:61CuGwE7jYn0g2gl7K3qoT4vCY59ZQEixkPu8PN5IrE=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 h1:Ji0DY1xUsUr3I8cHps0G+XM3WWU16lP6yG8qu1GAZAs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2/go.mod h1:5CsjAbs3NlGQyZNFACh+zztPDI7fU6eW9QsxjfnuBKg=
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.6 h1:6tayEze2Y+hiL3kdnEUxSPsP+pJsUfwLSFspFl1ru9Q=
Expand Down
85 changes: 79 additions & 6 deletions backend/internal/repository/dynamo/shorturl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package dynamo

import (
"context"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"

"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/naohito-T/tinyurl/backend/domain"
DynamoClient "github.com/naohito-T/tinyurl/backend/internal/infrastructures/dynamo"
"github.com/naohito-T/tinyurl/backend/internal/infrastructures/slog"
)

type IShortURLRepository interface {
Get(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
Put(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
}

type ShortURLRepository struct {
Client *DynamoClient.Connection
// インターフェースは既に参照型です。これは、インターフェースが背後でポインタとして機能することを意味し、明示的にポインタとして渡す必要はありません。
Expand All @@ -23,12 +33,75 @@ func NewShortURLRepository(client *DynamoClient.Connection, logger slog.ILogger)
}
}

func (r *ShortURLRepository) Put(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error) {
r.logger.Debug("PutItemInput: %v", params)
return r.Client.Conn.PutItem(ctx, params, optFns...)
type TableItem struct {
ID string `dynamodbav:"id"`
OriginalURL string `dynamodbav:"originalURL"`
CreatedAt string `dynamodbav:"createdAt"`
}

type ItemKey struct {
ID string `dynamodbav:"id"`
}

func (r *ShortURLRepository) GetByShortURL(id string) (domain.ShortURL, error) {
r.logger.Debug("GetItemInput: %v", id)

itemKey := ItemKey{
ID: id,
}

av, err := attributevalue.MarshalMap(itemKey)
if err != nil {
log.Fatal(err)
return domain.ShortURL{}, err
}

result, err := r.Client.Conn.GetItem(context.TODO(), &dynamodb.GetItemInput{
TableName: aws.String("offline-tinyurls"),
Key: av,
})

if err != nil {
log.Fatal(err)
return domain.ShortURL{}, err
}

var item TableItem
err = attributevalue.UnmarshalMap(result.Item, &item)
if err != nil {
log.Fatal(err)
return domain.ShortURL{}, err
}

shortURL := domain.ShortURL{
ID: item.ID,
OriginalURL: item.OriginalURL,
CreatedAt: item.CreatedAt,
}

return shortURL, nil
}

func (r *ShortURLRepository) Get(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error) {
r.logger.Debug("GetItemInput: %v", params)
return r.Client.Conn.GetItem(ctx, params, optFns...)
func (r *ShortURLRepository) CreateShortURL(params *domain.ShortURL) error {
r.logger.Debug("PutItemInput: %v", params)

item := TableItem{
ID: params.ID,
OriginalURL: params.OriginalURL,
CreatedAt: params.CreatedAt,
}
av, err := attributevalue.MarshalMap(item)
if err != nil {
log.Fatal(err)
}

_, err = r.Client.Conn.PutItem(context.TODO(), &dynamodb.PutItemInput{
TableName: aws.String("offline-tinyurls"),
Item: av,
})
if err != nil {
log.Fatal(err)
return err
}
return nil
}
25 changes: 25 additions & 0 deletions backend/internal/rest/container/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package container

import (
"github.com/naohito-T/tinyurl/backend/internal/infrastructures/dynamo"
"github.com/naohito-T/tinyurl/backend/internal/infrastructures/slog"
repoDynamo "github.com/naohito-T/tinyurl/backend/internal/repository/dynamo"
"github.com/naohito-T/tinyurl/backend/internal/usecase"

"sync"
)

type GuestContainer struct {
URLUsecase *usecase.ShortURLUsecase
}

var newGuestContainer = sync.OnceValue(func() *GuestContainer {
dynamoRepo := repoDynamo.NewShortURLRepository(dynamo.NewDynamoConnection(), slog.NewLogger())
return &GuestContainer{
URLUsecase: usecase.NewURLUsecase(dynamoRepo),
}
})

func NewGuestContainer() *GuestContainer {
return newGuestContainer()
}
15 changes: 11 additions & 4 deletions backend/internal/rest/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/naohito-T/tinyurl/backend/internal/infrastructures/slog"
// "github.com/naohito-T/tinyurl/backend/internal/rest/container"
)

// https://tinyurl.com/app/api/url/create"
// NewRouter これもシングルトンにした場合の例が気になる
func NewRouter(e *echo.Echo) {
e.GET("/health", health)
e.GET("/hello", hello)
// container := container.NewGuestContainer()

e.GET("/health", hello)
e.GET("/api/v1/urls/:shortUrl", hello)
e.POST("/api/v1/urls", hello)
}

func hello(c echo.Context) error {
println("Hello, World!")
// {"time":"2024-04-14T02:16:18.08145333Z","level":"INFO","msg":"Hello, World!"}
slog.NewLogger().Info("Hello, World!")
if err := isValid("hello"); err != nil {
return err
}
return c.String(http.StatusOK, "Hello, World!")
return c.String(http.StatusOK, "Hello, World 2!")
}

func health(c echo.Context) error {
Expand Down
32 changes: 32 additions & 0 deletions backend/internal/usecase/shorturl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package usecase

import (
"github.com/naohito-T/tinyurl/backend/domain"
)

type IShortURLRepo interface {
GetByShortURL(id string) (domain.ShortURL, error)
CreateShortURL(params *domain.ShortURL) error
}

type ShortURLUsecase struct {
shortURLRepo IShortURLRepo
}

func NewURLUsecase(u IShortURLRepo) *ShortURLUsecase {
return &ShortURLUsecase{
shortURLRepo: u,
}
}

func (u *ShortURLUsecase) GetByShortURL(id string) domain.ShortURL {
shortURL, err := u.shortURLRepo.GetByShortURL(id)
if err != nil {
panic(err)
}
return shortURL
}

func (u *ShortURLUsecase) CreateShortURL(params *domain.ShortURL) error {
return u.shortURLRepo.CreateShortURL(params)
}

0 comments on commit 1531181

Please sign in to comment.