-
Notifications
You must be signed in to change notification settings - Fork 75
/
main.go
188 lines (166 loc) · 4.69 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"errors"
"flag"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/gorilla/mux"
"google.golang.org/api/option"
)
var (
bind = flag.String("b", "127.0.0.1:8080", "Bind address")
verbose = flag.Bool("v", false, "Show access log")
credentials = flag.String("c", "", "The path to the keyfile. If not present, client will use your default application credentials.")
defaultIndex = flag.String("i", "", "The default index file to serve.")
)
var client *storage.Client
func handleError(w http.ResponseWriter, err error) {
if errors.Is(err, storage.ErrObjectNotExist) {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func header(r *http.Request, key string) (string, bool) {
if r.Header == nil {
return "", false
}
if candidate := r.Header[key]; len(candidate) > 0 {
return candidate[0], true
}
return "", false
}
func setStrHeader(w http.ResponseWriter, key string, value string) {
if value != "" {
w.Header().Add(key, value)
}
}
func setIntHeader(w http.ResponseWriter, key string, value int64) {
if value > 0 {
w.Header().Add(key, strconv.FormatInt(value, 10))
}
}
func setTimeHeader(w http.ResponseWriter, key string, value time.Time) {
if !value.IsZero() {
w.Header().Add(key, value.UTC().Format(http.TimeFormat))
}
}
type wrapResponseWriter struct {
http.ResponseWriter
status int
}
func (w *wrapResponseWriter) WriteHeader(status int) {
w.ResponseWriter.WriteHeader(status)
w.status = status
}
func wrapper(fn func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
proc := time.Now()
writer := &wrapResponseWriter{
ResponseWriter: w,
status: http.StatusOK,
}
fn(writer, r)
addr := r.RemoteAddr
if ip, found := header(r, "X-Forwarded-For"); found {
addr = ip
}
if *verbose {
log.Printf("[%s] %.3f %d %s %s",
addr,
time.Now().Sub(proc).Seconds(),
writer.status,
r.Method,
r.URL,
)
}
}
}
func fetchObjectAttrs(ctx context.Context, bucket, object string) (*storage.ObjectAttrs, error) {
var err error
var indexAppended bool
if object == "" && *defaultIndex != "" {
object, err = url.JoinPath(object, *defaultIndex)
if err != nil {
return nil, err
}
indexAppended = true
}
attrs, err := client.Bucket(bucket).Object(strings.TrimSuffix(object, "/")).Attrs(ctx)
if err != nil {
if errors.Is(err, storage.ErrObjectNotExist) {
if *defaultIndex == "" || indexAppended {
return nil, err
}
object, err = url.JoinPath(object, *defaultIndex)
if err != nil {
return nil, err
}
return client.Bucket(bucket).Object(object).Attrs(ctx)
}
return nil, err
}
return attrs, nil
}
func proxy(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
attrs, err := fetchObjectAttrs(r.Context(), params["bucket"], params["object"])
if err != nil {
handleError(w, err)
return
}
if lastStrs, ok := r.Header["If-Modified-Since"]; ok && len(lastStrs) > 0 {
last, err := http.ParseTime(lastStrs[0])
if *verbose && err != nil {
log.Printf("could not parse If-Modified-Since: %v", err)
}
if !attrs.Updated.Truncate(time.Second).After(last) {
w.WriteHeader(304)
return
}
}
gzipAcceptable := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
objr, err := client.Bucket(attrs.Bucket).Object(attrs.Name).ReadCompressed(gzipAcceptable).NewReader(r.Context())
if err != nil {
handleError(w, err)
return
}
setTimeHeader(w, "Last-Modified", attrs.Updated)
setStrHeader(w, "Content-Type", attrs.ContentType)
setStrHeader(w, "Content-Language", attrs.ContentLanguage)
setStrHeader(w, "Cache-Control", attrs.CacheControl)
setStrHeader(w, "Content-Encoding", objr.Attrs.ContentEncoding)
setStrHeader(w, "Content-Disposition", attrs.ContentDisposition)
setIntHeader(w, "Content-Length", objr.Attrs.Size)
io.Copy(w, objr)
}
func healthCheck(w http.ResponseWriter, r *http.Request) {
setStrHeader(w, "Content-Type", "text/plain")
io.WriteString(w, "OK\n")
}
func main() {
flag.Parse()
var err error
if *credentials != "" {
client, err = storage.NewClient(context.Background(), option.WithCredentialsFile(*credentials))
} else {
client, err = storage.NewClient(context.Background())
}
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
r := mux.NewRouter()
r.HandleFunc("/_health", wrapper(healthCheck)).Methods("GET", "HEAD")
r.HandleFunc("/{bucket:[0-9a-zA-Z-_.]+}/{object:.*}", wrapper(proxy)).Methods("GET", "HEAD")
log.Printf("[service] listening on %s", *bind)
if err := http.ListenAndServe(*bind, r); err != nil {
log.Fatal(err)
}
}