/
eidetic.go
120 lines (96 loc) · 2.78 KB
/
eidetic.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 (
"os"
"log"
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"encoding/json"
"crypto/sha1"
"menteslibres.net/gosexy/redis"
"encoding/base64"
)
type SerializableResponse struct {
Header map[string][]string // the HTTP response headers
Body []byte // the body string
}
func main() {
// The remote URL has to be provided as the first command line argument
remote_host := os.Args[1]
log.Println("Initializing proxy for: " + remote_host)
remote, err := url.Parse(remote_host)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
redisClient := redis.New()
err = redisClient.Connect("127.0.0.1", uint(6379))
if err != nil {
panic(err)
}
http.HandleFunc("/", handler(proxy, redisClient))
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
func hashKey(r *http.Request) string {
url := []byte(r.URL.String())
// log.Println("\n\n\nRequest URL: " + r.URL.String())
// Read the body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
// log.Println("\nRequest Body:\n" + string(body))
// Hack to pretend the reader has ben resetted
reader := bytes.NewReader(body)
r.Body = ioutil.NopCloser(reader)
hash := sha1.Sum(append(url,body...))
return "eidetic#" + base64.URLEncoding.EncodeToString(hash[:])
}
func handler(p *httputil.ReverseProxy, redisClient *redis.Client) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
hash := hashKey(r)
serializableResponse := SerializableResponse{make(map[string][]string), nil}
s, err := redisClient.Get(hash)
if err != nil { // The request is not cached
rec := httptest.NewRecorder()
log.Println("Non cached: " + hash)
// Perform the real request and cache it
p.ServeHTTP(rec, r)
for k, v := range rec.Header() {
serializableResponse.Header[k] = v
}
serializableResponse.Body = rec.Body.Bytes()
jsonResponse, err := json.Marshal(serializableResponse)
if err != nil {
panic(err)
}
redisClient.Set(hash, jsonResponse)
w.Header().Set("X-Eidetic", "Live request")
} else { // The request is cached
log.Println("Cached!: " + hash)
// Load the cached request
err = json.Unmarshal([]byte(s), &serializableResponse)
if err != nil {
panic(err)
}
w.Header().Set("X-Eidetic", "Cached request")
}
//Copy the data to the actual ResponseWriter
// log.Println("\n\n\nResponse Headers:")
for k, v := range serializableResponse.Header {
w.Header()[k] = v
// log.Println(k + ": ")
// for _, str := range v {
// log.Println(" " + str)
// }
}
w.Write([]byte(serializableResponse.Body))
// log.Println("\nResponse body:\n" + string(serializableResponse.Body))
}
}