Skip to content

Commit dea01d0

Browse files
author
Alok Nerurkar
committed
fix: preconf RPC CORS headers
1 parent b0c7e5c commit dea01d0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

tools/preconf-rpc/rpcserver/rpcserver.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func (s *JSONRPCServer) writeResponse(w http.ResponseWriter, id any, result *jso
159159
Result: result,
160160
Error: nil,
161161
}
162+
setCorsHeaders(w)
162163
w.Header().Set("Content-Type", "application/json")
163164
if err := json.NewEncoder(w).Encode(response); err != nil {
164165
http.Error(w, "Failed to write response", http.StatusInternalServerError)
@@ -178,6 +179,7 @@ func (s *JSONRPCServer) writeError(w http.ResponseWriter, id any, code int, mess
178179
Data: nil,
179180
},
180181
}
182+
setCorsHeaders(w)
181183
w.Header().Set("Content-Type", "application/json")
182184
if err := json.NewEncoder(w).Encode(response); err != nil {
183185
http.Error(w, "Failed to write error response", http.StatusInternalServerError)
@@ -206,6 +208,7 @@ func (s *JSONRPCServer) proxyRequest(w http.ResponseWriter, body []byte) {
206208
_ = resp.Body.Close()
207209
}()
208210

211+
setCorsHeaders(w)
209212
w.Header().Set("Content-Type", "application/json")
210213
w.WriteHeader(resp.StatusCode)
211214
rdr := io.LimitReader(resp.Body, defaultMaxBodySize)
@@ -219,3 +222,9 @@ func (s *JSONRPCServer) proxyRequest(w http.ResponseWriter, body []byte) {
219222
return
220223
}
221224
}
225+
226+
func setCorsHeaders(w http.ResponseWriter) {
227+
w.Header().Set("Access-Control-Allow-Origin", "*")
228+
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
229+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
230+
}

0 commit comments

Comments
 (0)