-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
131 lines (106 loc) · 3.02 KB
/
handler.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
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/golang/groupcache"
"github.com/gorilla/mux"
"io"
"math/rand"
"net/http"
"os"
"time"
"vip/fetch"
)
type UploadResponse struct {
Url string `json:"url"`
}
type verifyAuth func(http.ResponseWriter, *http.Request)
func (h verifyAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Enable cross-origin requests
if domain := os.Getenv("ALLOWED_ORIGIN"); domain != "" {
if origin := r.Header.Get("Origin"); origin == domain {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-Vip-Token, Authorization")
}
} else {
auth := r.Header.Get("X-Vip-Token")
if auth != authToken {
w.WriteHeader(http.StatusUnauthorized)
return
}
}
if r.Method == "OPTIONS" {
return
}
h(w, r)
}
func fileKey(bucket string) string {
seed := rand.New(rand.NewSource(time.Now().UnixNano()))
key := fmt.Sprintf("%d-%s-%d", seed.Int63(), bucket, time.Now().UnixNano())
hash := md5.New()
io.WriteString(hash, key)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func handleImageRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
w.Header().Set("Cache-Control", "public, max-age=31536000")
// Client is checking for a cached URI, assume it is valid
// and return a 304
if r.Header.Get("If-Modified-Since") != "" {
w.WriteHeader(http.StatusNotModified)
return
}
gc := fetch.RequestContext(r)
var data []byte
err := cache.Get(gc, gc.CacheKey(), groupcache.AllocatingByteSliceSink(&data))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", http.DetectContentType(data))
http.ServeContent(w, r, gc.ImageId, time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), bytes.NewReader(data))
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
vars := mux.Vars(r)
bucket := vars["bucket_id"]
// Set a hard 5mb limit on files
if r.ContentLength > 5<<20 {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
}
key := fileKey(bucket)
err := storage.PutReader(bucket, key, r.Body,
r.ContentLength, r.Header.Get("Content-Type"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer r.Body.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uri := r.URL
if r.URL.Host == "" {
uri.Host = os.Getenv("URI_HOSTNAME")
uri.Scheme = "http"
}
uri.Path = fmt.Sprintf("%s/%s", bucket, key)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(UploadResponse{
Url: uri.String(),
})
}
func handlePing(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "pong")
}