-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudfront.go
144 lines (128 loc) · 4.51 KB
/
cloudfront.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package external
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
"github.com/google/uuid"
"github.com/google/wire"
"github.com/nao1215/rainbow/app/domain/model"
"github.com/nao1215/rainbow/app/domain/service"
"github.com/nao1215/rainbow/utils/errfmt"
)
// NewCloudFrontClient returns a new CloudFront client.
func NewCloudFrontClient(cfg *model.AWSConfig) (*cloudfront.Client, error) {
return cloudfront.NewFromConfig(*cfg.Config), nil
}
// CloudFrontCreatorSet is a provider set for CloudFrontCreator.
var CloudFrontCreatorSet = wire.NewSet(
wire.Bind(new(service.CloudFrontCreator), new(*CloudFrontCreator)),
NewCloudFrontCreator,
)
// CloudFrontCreator is an implementation for CloudFrontCreator.
type CloudFrontCreator struct {
*cloudfront.Client
}
var _ service.CloudFrontCreator = &CloudFrontCreator{}
// NewCloudFrontCreator creates a new CloudFrontCreator.
func NewCloudFrontCreator(c *cloudfront.Client) *CloudFrontCreator {
return &CloudFrontCreator{
Client: c,
}
}
// CreateCloudFront creates a CDN.
func (c *CloudFrontCreator) CreateCloudFront(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
createDistributionInput := &cloudfront.CreateDistributionInput{
DistributionConfig: &types.DistributionConfig{
Comment: aws.String("CloudFront Distribution Generated by Rainbow Project"),
CallerReference: aws.String(uuid.New().String()),
DefaultCacheBehavior: &types.DefaultCacheBehavior{
TargetOriginId: aws.String("S3 Origin ID Generated by Rainbow Project"),
ViewerProtocolPolicy: types.ViewerProtocolPolicyRedirectToHttps,
MinTTL: aws.Int64(300), //nolint:gomnd
MaxTTL: aws.Int64(300), //nolint:gomnd
DefaultTTL: aws.Int64(300), //nolint:gomnd
AllowedMethods: &types.AllowedMethods{
Items: []types.Method{
types.Method("GET"),
types.Method("HEAD"),
types.Method("OPTIONS"),
},
Quantity: aws.Int32(3),
CachedMethods: &types.CachedMethods{
Items: []types.Method{
types.Method("GET"),
types.Method("HEAD"),
},
Quantity: aws.Int32(2), //nolint:gomnd
},
},
// Deprecated fields
ForwardedValues: &types.ForwardedValues{
QueryString: aws.Bool(true),
Cookies: &types.CookiePreference{
Forward: types.ItemSelection("none"),
},
},
},
DefaultRootObject: aws.String("index.html"),
HttpVersion: types.HttpVersion("http2and3"),
PriceClass: types.PriceClass("PriceClass_100"),
Origins: &types.Origins{
Items: []types.Origin{
{
Id: aws.String("S3 Origin ID Generated by Spare"),
DomainName: aws.String(input.Bucket.Domain()),
S3OriginConfig: &types.S3OriginConfig{
OriginAccessIdentity: aws.String(
fmt.Sprintf("origin-access-identity/cloudfront/%s", *input.OAIID),
),
},
},
},
Quantity: aws.Int32(1),
},
Enabled: aws.Bool(true),
},
}
output, err := c.CreateDistribution(ctx, createDistributionInput)
if err != nil {
return nil, errfmt.Wrap(err, "failed to create a CloudFront distribution")
}
return &service.CloudFrontCreatorOutput{
Domain: model.Domain(*output.Distribution.DomainName),
}, nil
}
// OAICreatorSet is a provider set for OAICreator.
var OAICreatorSet = wire.NewSet(
NewCloudFrontOAICreator,
wire.Bind(new(service.OAICreator), new(*CloudFrontOAICreator)),
)
// CloudFrontOAICreator is an implementation for OAICreator.
type CloudFrontOAICreator struct {
*cloudfront.Client
}
var _ service.OAICreator = &CloudFrontOAICreator{}
// NewCloudFrontOAICreator creates a new CloudFrontOAICreator.
func NewCloudFrontOAICreator(c *cloudfront.Client) *CloudFrontOAICreator {
return &CloudFrontOAICreator{
Client: c,
}
}
// CreateOAI creates a new OAI.
func (c *CloudFrontOAICreator) CreateOAI(ctx context.Context, _ *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
createOAIInput := &cloudfront.CreateCloudFrontOriginAccessIdentityInput{
CloudFrontOriginAccessIdentityConfig: &types.CloudFrontOriginAccessIdentityConfig{
CallerReference: aws.String(uuid.NewString()),
Comment: aws.String("Origin Access Identity (OAI) Generated by Spare"),
},
}
output, err := c.CreateCloudFrontOriginAccessIdentity(ctx, createOAIInput)
if err != nil {
return nil, err
}
return &service.OAICreatorOutput{
ID: output.CloudFrontOriginAccessIdentity.Id,
}, nil
}