From 1efc7c48be81b8df075cb84d68300bbbea1a0186 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Tue, 5 Oct 2021 03:21:40 +0200 Subject: [PATCH] added cors header --- config.yaml | 5 ++++- main.go | 4 ++++ utils/utils.go | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index 81ae077..1c4a7c5 100644 --- a/config.yaml +++ b/config.yaml @@ -7,4 +7,7 @@ parser: steam: username: steamusername api_key: apikey - rate_per_sec: 1 \ No newline at end of file + rate_per_sec: 1 + +httpd: + cors_allow_domains: '*' \ No newline at end of file diff --git a/main.go b/main.go index c4c6e66..12344d1 100644 --- a/main.go +++ b/main.go @@ -127,6 +127,7 @@ func housekeeping() { } func getPlayer(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains) id := mux.Vars(r)["id"] tPlayer, err := utils.GetPlayer(db, id, conf.Steam.APIKey, rL) if err != nil { @@ -204,6 +205,7 @@ func getPlayer(w http.ResponseWriter, r *http.Request) { } func postPlayerTrackMe(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains) err := r.ParseForm() if err != nil { log.Errorf("[postPlayerTrackMe] %+v", err) @@ -252,6 +254,7 @@ func postPlayerTrackMe(w http.ResponseWriter, r *http.Request) { } func getMatchParse(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains) shareCode := mux.Vars(r)["sharecode"] if shareCode == "" || !utils.ShareCodeRegEx.MatchString(shareCode) { @@ -268,6 +271,7 @@ func getMatchParse(w http.ResponseWriter, r *http.Request) { } func getMatch(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains) id := mux.Vars(r)["id"] if id == "" { diff --git a/utils/utils.go b/utils/utils.go index 724686c..27baf91 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -34,6 +34,9 @@ type Conf struct { APIKey string `yaml:"api_key"` RatePerSecond int `yaml:"rate_per_sec"` } + Httpd struct { + CORSAllowDomains string `yaml:"cors_allow_domains"` + } } type DBWithLock struct {