feat(controller): custom api cors trough env
This commit is contained in:
parent
588e56ec27
commit
bc3633e20b
|
|
@ -10,3 +10,4 @@ BROKER_PASSWORD=""
|
||||||
BROKER_CLIENTID=""
|
BROKER_CLIENTID=""
|
||||||
BROKER_QOS=""
|
BROKER_QOS=""
|
||||||
REST_API_PORT=""
|
REST_API_PORT=""
|
||||||
|
REST_API_CORS="" #addresses must be separated by commas example: "http://localhost:3000,http://myapp.com,app2.com"
|
||||||
|
|
@ -3,13 +3,16 @@ package cors
|
||||||
import (
|
import (
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetCorsConfig() cors.Cors {
|
func GetCorsConfig() cors.Cors {
|
||||||
|
allowedOrigins := getCorsEnvConfig()
|
||||||
|
fmt.Println(allowedOrigins)
|
||||||
return *cors.New(cors.Options{
|
return *cors.New(cors.Options{
|
||||||
AllowedOrigins: []string{
|
AllowedOrigins: allowedOrigins,
|
||||||
"http://localhost:3000",
|
|
||||||
},
|
|
||||||
AllowedMethods: []string{
|
AllowedMethods: []string{
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
|
|
@ -25,3 +28,11 @@ func GetCorsConfig() cors.Cors {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCorsEnvConfig() []string {
|
||||||
|
val, _ := os.LookupEnv("REST_API_CORS")
|
||||||
|
if val == "" {
|
||||||
|
return []string{"*"}
|
||||||
|
}
|
||||||
|
return strings.Split(val, ",")
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user