GitHub

@@ -2,27 +2,23 @@ package main

2233

import (

44

"fmt"

5+

"github.com/aws/aws-lambda-go/lambda"

56

"github.com/aws/aws-sdk-go/aws"

67

"github.com/aws/aws-sdk-go/aws/session"

7-

"github.com/aws/aws-sdk-go/service/cloudfront"

88

"github.com/aws/aws-sdk-go/service/s3/s3manager"

99

"gopkg.in/src-d/go-git.v4"

1010

"log"

1111

"mime"

1212

"os"

1313

"os/exec"

1414

"path/filepath"

15-

"strconv"

1615

"strings"

17-

"time"

18-

"github.com/aws/aws-lambda-go/lambda"

1916

)

20172118

func hugobuild() (string, error) {

2219

gitRepo := os.Getenv("GIT_REPO")

2320

s3Bucket := os.Getenv("S3_BUCKET")

2421

s3Region := os.Getenv("S3_REGION")

25-

cfDistro := os.Getenv("CLOUDFRONT_DISTRO")

26222723

blogTmpPath := "/tmp/blog"

2824

blogOutputPath := blogTmpPath + "/public"

@@ -63,39 +59,10 @@ func hugobuild() (string, error) {

6359

return "", s3Err

6460

}

656166-

// Invalidate Cloudfront

67-

fmt.Printf("Submitting Cloudfront invalidation for distribution %s\n", cfDistro)

68-

if cfErr := cloudFrontInvalidate(cfDistro); cfErr != nil {

69-

log.Fatalf("Cloudfront invalidate failed with %s\n", cfErr)

70-

return "", cfErr

71-

}

72-7362

return "SUCCESS", nil

7463

}

756476-

func cloudFrontInvalidate(distribution string) (error) {

77-

// If a cloudfront distribution is defined, submit an

78-

// invalidation request

79-

if distribution == "" {

80-

return nil

81-

}

82-

invalidateBatch := &cloudfront.InvalidationBatch{}

83-

invalidateBatch.SetCallerReference(strconv.Itoa(int(time.Now().Unix())))

84-

paths := &cloudfront.Paths{}

85-

pathStrings := []*string{aws.String("/*")}

86-

paths.SetItems(pathStrings)

87-

paths.SetQuantity(int64(len(pathStrings)))

88-

invalidateBatch.SetPaths(paths)

89-

invalidationInput := &cloudfront.CreateInvalidationInput{}

90-

invalidationInput.SetDistributionId(distribution)

91-

invalidationInput.SetInvalidationBatch(invalidateBatch)

92-93-

cfSvc := cloudfront.New(session.New())

94-

_, cfErr := cfSvc.CreateInvalidation(invalidationInput)

95-

return cfErr

96-

}

97-98-

func s3Sync(s3Region string, blogOutputPath string, s3Bucket string) (error) {

65+

func s3Sync(s3Region string, blogOutputPath string, s3Bucket string) error {

9966

// Take the built artifacts from Hugo and sync them to the S3 bucket.

10067

// This code was taken from this example:

10168

// https://github.com/aws/aws-sdk-go/tree/master/example/service/s3/sync

@@ -178,11 +145,20 @@ func (iter *SyncFolderIterator) UploadObject() s3manager.BatchUploadObject {

178145

mimeType = "binary/octet-stream"

179146

}

180147148+

cacheControl := "no-cache"

149+150+

if strings.Contains(mimeType, "image/") || strings.Contains(mimeType, "binary/octet-stream") {

151+

cacheControl = "max-age=86400"

152+

} else if strings.Contains(mimeType, "text/css") || strings.Contains(mimeType, "application/x-javascript") {

153+

cacheControl = "max-age=31536000"

154+

}

155+181156

input := s3manager.UploadInput{

182-

Bucket: &iter.bucket,

183-

Key: &fi.key,

184-

Body: body,

185-

ContentType: &mimeType,

157+

Bucket: &iter.bucket,

158+

Key: &fi.key,

159+

Body: body,

160+

ContentType: &mimeType,

161+

CacheControl: &cacheControl,

186162

}

187163188164

return s3manager.BatchUploadObject{

Read the original on github.com ↗