From ecd51bd017171a1988742ca3fee8ce744a3eed8f Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Tue, 27 Apr 2021 19:59:10 +0200 Subject: [PATCH] add optional cors header --- api/rest-api.go | 9 +++++++-- config/config.go | 8 ++++++++ go.mod | 1 + go.sum | 4 ++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/api/rest-api.go b/api/rest-api.go index 4f8be63..b9278ef 100644 --- a/api/rest-api.go +++ b/api/rest-api.go @@ -4,10 +4,12 @@ import ( "encoding/json" "fmt" "net/http" + "weather-data/config" "weather-data/storage" "weather-data/weathersource" "github.com/google/uuid" + "github.com/gorilla/handlers" "github.com/gorilla/mux" ) @@ -29,8 +31,11 @@ func NewRestAPI(connection string, weatherStorage storage.WeatherStorage, sensor //Start a new Rest-API instance func (api *weatherRestApi) Start() error { - handler := api.handleRequests() - return http.ListenAndServe(api.connection, handler) + router := api.handleRequests() + + originsOk := handlers.AllowedOrigins([]string{config.RestConfiguration.AccessControlAllowOriginHeader}) + + return http.ListenAndServe(api.connection, handlers.CORS(originsOk)(router)) } //Close the rest api diff --git a/config/config.go b/config/config.go index 71c9f8d..a4b6186 100644 --- a/config/config.go +++ b/config/config.go @@ -31,6 +31,10 @@ type MqttConfig struct { AllowAnonymousAuthentication bool } +type RestConfig struct { + AccessControlAllowOriginHeader string +} + var MongoConfiguration = MongoConfig{ Host: getEnv("MONGO_HOST", "localhost:27017"), Database: getEnv("MONGO_DB", "weathersensors"), @@ -56,6 +60,10 @@ var MqttConfiguration = MqttConfig{ AllowAnonymousAuthentication: getEnvBool("MQTT_ANONYMOUS", false), } +var RestConfiguration = RestConfig{ + AccessControlAllowOriginHeader: getEnv("ACCESS_CONTROL_ALLOW_ORIGIN_HEADER", "*"), +} + var AllowUnregisteredSensors = getEnvBool("ALLOW_UNREGISTERED_SENSORS", false) //helper diff --git a/go.mod b/go.mod index 6d46038..523284b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/eclipse/paho.mqtt.golang v1.3.2 github.com/google/uuid v1.2.0 + github.com/gorilla/handlers v1.5.1 // indirect github.com/gorilla/mux v1.8.0 github.com/influxdata/influxdb-client-go/v2 v2.2.2 go.mongodb.org/mongo-driver v1.5.1 // indirect diff --git a/go.sum b/go.sum index a7dadec..d3cf5f8 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/deepmap/oapi-codegen v1.3.13/go.mod h1:WAmG5dWY8/PYHt4vKxlt90NsbHMAOC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/eclipse/paho.mqtt.golang v1.3.2 h1:ICzfxSyrR8bOsh9l8JBBOwO1tc2C26oEyody0ml0L6E= github.com/eclipse/paho.mqtt.golang v1.3.2/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc= +github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= +github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= @@ -46,6 +48,8 @@ github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGS github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= +github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=