-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (127 loc) · 4.41 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/TeamTrumpet/waltz/waltz"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/codegangsta/negroni"
"github.com/facebookgo/grace/gracehttp"
)
const imageExpiry time.Duration = 1 * time.Hour * 24 * 30
type resizeOpts struct {
region, bucket, prefix string
path string
client *s3.S3
}
func newResizeHandler(ro resizeOpts) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Starting resizeHandler")
imageKey := r.URL.Path[len(ro.path):]
// fetch the query param
resizeQuery := r.URL.Query().Get("resize")
key := fmt.Sprintf("%s/%s", ro.prefix, imageKey)
if resizeQuery == "" {
log.Println("Completed resizeHandler: Redirected, no resize parameter")
urlStr := fmt.Sprintf("https://s3-%s.amazonaws.com/%s/%s", ro.region, ro.bucket, key)
http.Redirect(w, r, urlStr, http.StatusTemporaryRedirect)
return
}
resizeX, resizeY, err := waltz.ParseResize(resizeQuery)
if err != nil {
log.Println("Completed resizeHandler: ERROR: Resize parameters invalid:", err.Error())
http.Error(w, "", http.StatusBadRequest)
return
}
params := &s3.GetObjectInput{
Bucket: aws.String(ro.bucket),
Key: aws.String(key),
}
log.Println("Getting", *params.Key, "from bucket", *params.Bucket)
ifNoneMatch := r.Header.Get("If-None-Match")
if ifNoneMatch != "" {
params.IfNoneMatch = aws.String(ifNoneMatch)
}
resp, err := ro.client.GetObject(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if reqErr, ok := err.(awserr.RequestFailure); ok {
// if this is a not modified error, then cut quick
if reqErr.StatusCode() == http.StatusNotModified {
log.Println("Completed resizeHandler: Object not modified")
w.WriteHeader(http.StatusNotModified)
return
}
if reqErr.StatusCode() == http.StatusNotFound {
log.Println("Completed resizeHandler: Key not found")
w.WriteHeader(http.StatusNotFound)
return
}
// A service error occurred
log.Println("Completed resizeHandler: ERROR: AWS Service error:", reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
http.Error(w, "", http.StatusInternalServerError)
return
}
// Generic AWS error with Code, Message, and original error (if any)
log.Println("Completed resizeHandler: ERROR: Generic AWS error:", awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
http.Error(w, "", http.StatusInternalServerError)
return
}
// This case should never be hit, the SDK should always return an
// error which satisfies the awserr.Error interface.
log.Println("Completed resizeHandler: ERROR: Impossible error:", err.Error())
http.Error(w, "", http.StatusInternalServerError)
return
}
// write out the content type
w.Header().Set("Cache-control", fmt.Sprintf("public, max-age=%d", int64(imageExpiry.Seconds())))
w.Header().Set("Content-Type", *resp.ContentType)
w.Header().Set("Etag", *resp.ETag)
if err := waltz.Do(resp.Body, w, nil, resizeX, resizeY); err != nil {
log.Println("Completed resizeHandler: ERROR: Resize Error:", err.Error())
http.Error(w, "", http.StatusInternalServerError)
return
}
}
}
func robotsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "User-Agent: *\nDisallow: /")
}
func main() {
// fetch config from environment
awsRegion := os.Getenv("AWS_REGION")
awsBucketName := os.Getenv("AWS_BUCKET")
awsBucketPrefix := os.Getenv("AWS_BUCKET_PREFIX")
// create the s3 client
client := s3.New(nil)
// create middleware
n := negroni.Classic()
// create mux
mux := http.NewServeMux()
resizePath := fmt.Sprintf("/%s/", awsBucketPrefix)
ro := resizeOpts{
bucket: awsBucketName,
prefix: awsBucketPrefix,
path: resizePath,
region: awsRegion,
client: client,
}
resizeHandler := newResizeHandler(ro)
log.Println("Serving images from", resizePath)
mux.HandleFunc(resizePath, resizeHandler)
mux.HandleFunc("/robots.txt", robotsHandler)
// add mux to middleware stack
n.UseHandler(mux)
server := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")),
Handler: n,
}
log.Printf("Starting server on 0.0.0.0:%s\n", os.Getenv("PORT"))
// run using gracehttp
gracehttp.Serve(server)
}