Merge pull request #130 from OktopUSP/cors_by_env
Get cors allowed origins from env file [ close #116 ]
This commit is contained in:
commit
a68b3c0b72
|
|
@ -9,4 +9,5 @@ BROKER_USERNAME=""
|
||||||
BROKER_PASSWORD=""
|
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"
|
||||||
|
|
@ -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, ",")
|
||||||
|
}
|
||||||
2
backend/services/socketio/.env
Normal file
2
backend/services/socketio/.env
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
CORS_ALLOWED_ORIGINS=""
|
||||||
|
# addresses must be separated by commas example: "http://localhost:3000,http://myapp.com"
|
||||||
3
backend/services/socketio/.gitignore
vendored
3
backend/services/socketio/.gitignore
vendored
|
|
@ -1 +1,2 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
|
.env.local
|
||||||
1089
backend/services/socketio/package-lock.json
generated
1089
backend/services/socketio/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
|
@ -13,6 +13,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@koa/cors": "^4.0.0",
|
"@koa/cors": "^4.0.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^16.3.1",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"koa": "^2.14.2",
|
"koa": "^2.14.2",
|
||||||
"socket.io": "^4.6.2"
|
"socket.io": "^4.6.2"
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,24 @@
|
||||||
|
const dotenv = require('dotenv')
|
||||||
|
dotenv.config();
|
||||||
|
dotenv.config({ path: `.env.local`, override: true });
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 5000;
|
const PORT = 5000;
|
||||||
|
|
||||||
const http = require('http').Server(app);
|
const http = require('http').Server(app);
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
var allowedOrigins;
|
||||||
|
let allowedOriginsFromEnv = process.env.CORS_ALLOWED_ORIGINS.split(',')
|
||||||
|
if (allowedOriginsFromEnv.length > 1) {
|
||||||
|
allowedOrigins = allowedOriginsFromEnv
|
||||||
|
}else{
|
||||||
|
allowedOrigins = "*"
|
||||||
|
}
|
||||||
|
console.log("allowedOrigins:",allowedOrigins)
|
||||||
|
|
||||||
const io = require('socket.io')(http, {
|
const io = require('socket.io')(http, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: "http://localhost:3000"
|
origin: allowedOrigins
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user