forked from janrotter/bloblo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
120 lines (97 loc) · 2.93 KB
/
hello.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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
listenAddress string
s3BucketName string
s3Svc *s3.S3
upstreamUrl *url.URL
cachedBlobDigests map[string]bool
)
func init() {
listenAddress = ":7777"
s3BucketName = "sample-bucket"
upstreamUrl, _ = url.Parse("http://localhost:6666")
cachedBlobDigests = make(map[string]bool)
cachedBlobDigests["sha256:dfcff6d93b39097b3e4f343e505e1af69ccc98d4122439edc882f1ab908f48cb"] = true
localstackResolver := func(service, region string, opts ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
localstackUrl := "http://localhost:4566"
return endpoints.ResolvedEndpoint{
URL: localstackUrl,
SigningRegion: "us-east-1",
}, nil
}
s3ForcePathStyle := true
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("us-east-1"),
EndpointResolver: endpoints.ResolverFunc(localstackResolver),
S3ForcePathStyle: &s3ForcePathStyle,
},
SharedConfigState: session.SharedConfigEnable,
}))
s3Svc = s3.New(sess)
}
func presignBlob(blobDigest string) string {
req, _ := s3Svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(s3BucketName),
Key: aws.String(blobDigest),
})
urlStr, err := req.Presign(15 * time.Minute)
if err != nil {
log.Println("Failed to sign request", err)
}
return urlStr
}
func blobInCache(blobDigest string) bool {
return cachedBlobDigests[blobDigest]
}
type RequestLogger struct {
proxy *httputil.ReverseProxy
}
func (rl *RequestLogger) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Println(req.RequestURI, req.Method)
pathElements := strings.Split(req.RequestURI, "/")
if len(pathElements) > 2 && pathElements[len(pathElements)-2] == "blobs" {
blobDigest := pathElements[len(pathElements)-1]
headReq := req.Clone(req.Context())
headReq.RequestURI = ""
headReq.Host = upstreamUrl.Host
headReq.URL.Host = upstreamUrl.Host
headReq.URL.Scheme = upstreamUrl.Scheme
headReq.Method = http.MethodHead
response, err := http.DefaultClient.Do(headReq)
if err != nil {
log.Println("Failed to reach the upstream", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if response.StatusCode == http.StatusOK {
if blobInCache(blobDigest) {
user, _, _ := headReq.BasicAuth()
log.Println("Serving the blob from cache for user", user)
http.Redirect(w, req, presignBlob(blobDigest), http.StatusFound)
return
}
}
}
rl.proxy.ServeHTTP(w, req)
}
func main() {
fmt.Println("Hello, World! I will use", upstreamUrl, "as my upstream and listen on", listenAddress)
proxy := httputil.NewSingleHostReverseProxy(upstreamUrl)
r := new(RequestLogger)
r.proxy = proxy
http.ListenAndServe(listenAddress, r)
}