From 09f1d0c4aef3e3220d4b7f94c64d33ae5566e6e5 Mon Sep 17 00:00:00 2001 From: Manish R Jain <manishrjain@gmail.com> Date: Thu, 26 Nov 2015 06:37:49 +0000 Subject: [PATCH] Add cors headers, so the endpoint can be called by other domains. --- server/main.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/server/main.go b/server/main.go index 1a258b76..bc526b3f 100644 --- a/server/main.go +++ b/server/main.go @@ -39,14 +39,27 @@ var postingDir = flag.String("postings", "", "Directory to store posting lists") var mutationDir = flag.String("mutations", "", "Directory to store mutations") var port = flag.String("port", "8080", "Port to run server on.") +func addCorsHeaders(w http.ResponseWriter) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", + "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, X-Auth-Token, Cache-Control, X-Requested-With") + w.Header().Set("Access-Control-Allow-Credentials", "true") + w.Header().Set("Connection", "close") +} + func queryHandler(w http.ResponseWriter, r *http.Request) { - var l query.Latency - l.Start = time.Now() + addCorsHeaders(w) + if r.Method == "OPTIONS" { + return + } if r.Method != "POST" { x.SetStatus(w, x.E_INVALID_METHOD, "Invalid method") return } + var l query.Latency + l.Start = time.Now() defer r.Body.Close() q, err := ioutil.ReadAll(r.Body) if err != nil || len(q) == 0 { -- GitLab