/
cdn.go
60 lines (47 loc) · 1.29 KB
/
cdn.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
package models
import (
"context"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
uuid "github.com/uuid6/uuid6go-proto"
)
var DISTRIBUTION_ID string
func init() {
DISTRIBUTION_ID = os.Getenv("CLOUDFRONT_DISTRIBUTION_ID")
if DISTRIBUTION_ID == "" {
log.Fatalf("Environment variable CLOUDFRONT_DISTRIBUTION_ID not set!")
}
}
func CreateCFClient() *cloudfront.Client {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic(err)
}
return cloudfront.NewFromConfig(cfg)
}
func encodeCommentPath(path string) string {
return "/comments" + path
}
func InvalidateCommentCache(cf *cloudfront.Client, path string) error {
encoded := encodeCommentPath(path)
uuidGenerator := uuid.UUIDv7Generator{}
input := &cloudfront.CreateInvalidationInput{
DistributionId: aws.String(DISTRIBUTION_ID),
InvalidationBatch: &types.InvalidationBatch{
CallerReference: aws.String(uuidGenerator.Next().ToString()),
Paths: &types.Paths{
Quantity: aws.Int32(1),
Items: aws.ToStringSlice([]*string{aws.String(encoded)}),
},
},
}
_, err := cf.CreateInvalidation(context.TODO(), input)
if err != nil {
return err
}
return nil
}