From 5a522b79264e5ef12a15e0d4bc1d7292dd6a6b61 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 1 Oct 2021 13:26:18 +0000 Subject: [PATCH] Cors settings for status servers --- crawlers/server/main.go | 14 ++++++++++++++ crawlers/server/sample.env | 1 + db/server/main.go | 14 ++++++++++++++ db/server/sample.env | 1 + 4 files changed, 30 insertions(+) diff --git a/crawlers/server/main.go b/crawlers/server/main.go index 923d1574..413c3c88 100644 --- a/crawlers/server/main.go +++ b/crawlers/server/main.go @@ -13,6 +13,7 @@ import ( ) var MOONSTREAM_IPC_PATH = os.Getenv("MOONSTREAM_IPC_PATH") +var MOONSTREAM_CORS_ALLOWED_ORIGINS = os.Getenv("MOONSTREAM_CORS_ALLOWED_ORIGINS") type GethResponse struct { Result string `json:"result"` @@ -26,8 +27,17 @@ type PingResponse struct { Status string `json:"status"` } +func setupCorsResponse(w *http.ResponseWriter, req *http.Request) { + (*w).Header().Set("Access-Control-Allow-Origin", MOONSTREAM_CORS_ALLOWED_ORIGINS) + (*w).Header().Set("Access-Control-Allow-Methods", "GET") +} + func ping(w http.ResponseWriter, req *http.Request) { + setupCorsResponse(&w, req) log.Printf("%s, %s, %q", req.RemoteAddr, req.Method, req.URL.String()) + if (*req).Method == "OPTIONS" { + return + } w.Header().Set("Content-Type", "application/json") response := PingResponse{Status: "ok"} @@ -35,7 +45,11 @@ func ping(w http.ResponseWriter, req *http.Request) { } func pingGeth(w http.ResponseWriter, req *http.Request) { + setupCorsResponse(&w, req) log.Printf("%s, %s, %q", req.RemoteAddr, req.Method, req.URL.String()) + if (*req).Method == "OPTIONS" { + return + } postBody, err := json.Marshal(map[string]interface{}{ "jsonrpc": "2.0", diff --git a/crawlers/server/sample.env b/crawlers/server/sample.env index fc9b5a4a..053b87b4 100644 --- a/crawlers/server/sample.env +++ b/crawlers/server/sample.env @@ -1,2 +1,3 @@ export MOONSTREAM_CRAWLERS_SERVER_PORT="8080" export MOONSTREAM_IPC_PATH=null +export MOONSTREAM_CORS_ALLOWED_ORIGINS="http://localhost:3000,https://moonstream.to,https://www.moonstream.to,https://alpha.moonstream.to" diff --git a/db/server/main.go b/db/server/main.go index fd5f4899..c05dab03 100644 --- a/db/server/main.go +++ b/db/server/main.go @@ -12,6 +12,7 @@ import ( ) var MOONSTREAM_DB_URI = os.Getenv("MOONSTREAM_DB_URI") +var MOONSTREAM_CORS_ALLOWED_ORIGINS = os.Getenv("MOONSTREAM_CORS_ALLOWED_ORIGINS") type PingResponse struct { Status string `json:"status"` @@ -21,8 +22,17 @@ type BlockResponse struct { BlockNumber uint64 `json:"block_number"` } +func setupCorsResponse(w *http.ResponseWriter, req *http.Request) { + (*w).Header().Set("Access-Control-Allow-Origin", MOONSTREAM_CORS_ALLOWED_ORIGINS) + (*w).Header().Set("Access-Control-Allow-Methods", "GET") +} + func ping(w http.ResponseWriter, req *http.Request) { + setupCorsResponse(&w, req) log.Printf("%s, %s, %q", req.RemoteAddr, req.Method, req.URL.String()) + if (*req).Method == "OPTIONS" { + return + } w.Header().Set("Content-Type", "application/json") response := PingResponse{Status: "ok"} @@ -30,7 +40,11 @@ func ping(w http.ResponseWriter, req *http.Request) { } func blockLatest(w http.ResponseWriter, req *http.Request) { + setupCorsResponse(&w, req) log.Printf("%s, %s, %q", req.RemoteAddr, req.Method, req.URL.String()) + if (*req).Method == "OPTIONS" { + return + } w.Header().Set("Content-Type", "application/json") diff --git a/db/server/sample.env b/db/server/sample.env index 412c015d..ef5a4be3 100644 --- a/db/server/sample.env +++ b/db/server/sample.env @@ -1,2 +1,3 @@ export MOONSTREAM_DB_SERVER_PORT="8080" export MOONSTREAM_DB_URI="postgresql://:@:/" +export MOONSTREAM_CORS_ALLOWED_ORIGINS="http://localhost:3000,https://moonstream.to,https://www.moonstream.to,https://alpha.moonstream.to"