@@ -3,10 +3,10 @@ package handlers
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "fmt"
6
7
"graphql_cache/config"
7
8
"graphql_cache/graphcache"
8
9
"io"
9
- "log"
10
10
"net/http"
11
11
"net/url"
12
12
"time"
@@ -19,13 +19,13 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
19
19
// Create a new HTTP request with the same method, URL, and body as the original request
20
20
targetURL , err := url .Parse (cfg .Origin )
21
21
if err != nil {
22
- log .Println (err )
22
+ fmt .Println (err )
23
23
http .Error (w , "Error parsing target URL" , http .StatusInternalServerError )
24
24
}
25
25
26
26
proxyReq , err := http .NewRequest (r .Method , targetURL .String (), r .Body )
27
27
if err != nil {
28
- log .Println (err )
28
+ fmt .Println (err )
29
29
http .Error (w , "Error creating proxy request" , http .StatusInternalServerError )
30
30
}
31
31
@@ -38,7 +38,7 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
38
38
39
39
requestBody , err := io .ReadAll (proxyReq .Body )
40
40
if err != nil {
41
- log .Println (err )
41
+ fmt .Println (err )
42
42
http .Error (w , "error reading request body" , http .StatusInternalServerError )
43
43
}
44
44
@@ -49,17 +49,17 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
49
49
50
50
astQuery , err := graphcache .GetASTFromQuery (request .Query )
51
51
if err != nil {
52
- log .Println (err )
52
+ fmt .Println (err )
53
53
http .Error (w , "error parsing query" , http .StatusInternalServerError )
54
54
}
55
55
56
56
transformedBody , err := graphcache .AddTypenameToQuery (request .Query )
57
57
if err != nil {
58
- log .Println (err )
58
+ fmt .Println (err )
59
59
http .Error (w , "error transforming body" , http .StatusInternalServerError )
60
60
}
61
61
62
- log .Println ("time taken to transform body " , time .Since (start ))
62
+ fmt .Println ("time taken to transform body " , time .Since (start ))
63
63
64
64
transformedRequest := request
65
65
transformedRequest .Query = transformedBody
@@ -89,30 +89,28 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
89
89
// Set the status code of the original response to the status code of the proxy response
90
90
w .WriteHeader (resp .StatusCode )
91
91
92
- // Copy the body of the proxy response to the original response
93
- io .Copy (w , resp .Body )
94
-
95
92
responseBody := new (bytes.Buffer )
96
93
io .Copy (responseBody , resp .Body )
97
94
98
95
responseMap := make (map [string ]interface {})
99
96
err = json .Unmarshal (responseBody .Bytes (), & responseMap )
100
97
if err != nil {
101
- log .Println ("Error unmarshalling response:" , err , string (responseBody .Bytes ()))
98
+ fmt .Println ("Error unmarshalling response:" , string (responseBody .Bytes ()))
102
99
}
103
100
104
101
cache .InvalidateCache ("data" , responseMap , nil )
102
+ w .Write (responseBody .Bytes ())
105
103
return
106
104
}
107
105
108
106
cachedResponse , err := cache .ParseASTBuildResponse (astQuery , request )
109
107
if err == nil && cachedResponse != nil {
110
- log .Println ("serving response from cache..." )
108
+ fmt .Println ("serving response from cache..." )
111
109
br , err := json .Marshal (cachedResponse )
112
110
if err != nil {
113
111
http .Error (w , "error marshalling response" , http .StatusInternalServerError )
114
112
}
115
- log .Println ("time taken to serve response from cache " , time .Since (start ))
113
+ fmt .Println ("time taken to serve response from cache " , time .Since (start ))
116
114
graphqlresponse := graphcache.GraphQLResponse {Data : json .RawMessage (br )}
117
115
res , err := cache .RemoveTypenameFromResponse (& graphqlresponse )
118
116
if err != nil {
@@ -130,15 +128,17 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
130
128
// Send the proxy request using the custom transport
131
129
resp , err := client .Do (proxyReq )
132
130
if err != nil {
133
- log .Println (err )
131
+ fmt .Println (err )
134
132
http .Error (w , "error sending proxy request" , http .StatusInternalServerError )
135
133
}
136
134
defer resp .Body .Close ()
137
135
138
- // Copy the headers from the proxy response to the original response
136
+ // copy the headers from the proxy response to the original response
139
137
for name , values := range resp .Header {
140
- for _ , value := range values {
141
- w .Header ().Add (name , value )
138
+ if name != "Content-Length" { // copy all headers except Content-Length
139
+ for _ , value := range values {
140
+ w .Header ().Add (name , value )
141
+ }
142
142
}
143
143
}
144
144
@@ -148,18 +148,18 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
148
148
responseMap := make (map [string ]interface {})
149
149
err = json .Unmarshal (responseBody .Bytes (), & responseMap )
150
150
if err != nil {
151
- log .Println ("Error unmarshalling response:" , err , string (responseBody .Bytes ()))
151
+ fmt .Println ("Error unmarshalling response:" , string (responseBody .Bytes ()))
152
152
}
153
153
154
- log .Println ("time taken to get response from API " , time .Since (start ))
154
+ fmt .Println ("time taken to get response from API " , time .Since (start ))
155
155
156
156
astWithTypes , err := graphcache .GetASTFromQuery (transformedRequest .Query )
157
157
if err != nil {
158
- log .Println (err )
158
+ fmt .Println (err )
159
159
http .Error (w , "error parsing query" , http .StatusInternalServerError )
160
160
}
161
161
162
- log .Println ("time taken to generate AST with types " , time .Since (start ))
162
+ fmt .Println ("time taken to generate AST with types " , time .Since (start ))
163
163
164
164
reqVariables := transformedRequest .Variables
165
165
variables := make (map [string ]interface {})
@@ -177,7 +177,7 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
177
177
}
178
178
}
179
179
180
- log .Println ("time taken to build response key " , time .Since (start ))
180
+ fmt .Println ("time taken to build response key " , time .Since (start ))
181
181
182
182
// go through the response. Every object that has a __typename field, and an id field cache it in the format of typename:id
183
183
// for example, if the response has an object with __typename: "Organisation" and id: "1234", cache it as Organisation:1234
@@ -186,23 +186,15 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
186
186
187
187
cache .CacheResponse ("data" , responseMap , nil )
188
188
189
- log .Println ("time taken to cache response " , time .Since (start ))
190
-
191
- // Copy the body of the proxy response to the original response
192
- // io.Copy(w, resp.Body)
193
-
194
- resBody , err := io .ReadAll (resp .Body )
195
- if err != nil {
196
- http .Error (w , "error reading response body" , http .StatusInternalServerError )
197
- }
189
+ fmt .Println ("time taken to cache response " , time .Since (start ), responseMap )
198
190
199
191
newResponse := & graphcache.GraphQLResponse {}
200
- newResponse .FromBytes (resBody )
192
+ newResponse .FromBytes (responseBody . Bytes () )
201
193
res , err := cache .RemoveTypenameFromResponse (newResponse )
202
194
if err != nil {
203
195
http .Error (w , "error removing __typename" , http .StatusInternalServerError )
204
196
}
205
197
w .Write (res .Bytes ())
206
- // w.WriteHeader(http.StatusOK )
198
+ w . Header (). Add ( "graphql_cache" , "miss" )
207
199
})
208
200
}
0 commit comments