Merge pull request #133 from OktopUSP/dev

Api Cors Allowed  Origins
This commit is contained in:
Leandro Antônio Farias Machado 2023-10-18 18:39:08 -03:00 committed by GitHub
commit eb9a996ddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1140 additions and 8 deletions

20
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,20 @@
## New contributor guide
To get an overview of the project, read the [README](README.md) file. Here are some resources to help you get started with open source contributions:
- [Finding ways to contribute to open source on GitHub](https://docs.github.com/en/get-started/exploring-projects-on-github/finding-ways-to-contribute-to-open-source-on-github)
- [Set up Git](https://docs.github.com/en/get-started/quickstart/set-up-git)
- [GitHub flow](https://docs.github.com/en/get-started/quickstart/github-flow)
- [Collaborating with pull requests](https://docs.github.com/en/github/collaborating-with-pull-requests)
### Pull Request
When you're finished with the changes, create a pull request, also known as a PR.
- Set your PR to go into the <b>dev branch</b>.
- Don't forget to [link PR to issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) if you are solving one.
- Enable the checkbox to [allow maintainer edits](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork) so the branch can be updated for a merge.
Once you submit your PR, a team member will review your proposal. We may ask questions or request additional information.
- We may ask for changes to be made before a PR can be merged, either using [suggested changes](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/incorporating-feedback-in-your-pull-request) or pull request comments. You can apply suggested changes directly through the UI. You can make any other changes in your fork, then commit them to your branch.
- As you update your PR and apply changes, mark each conversation as [resolved](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request#resolving-conversations).
- If you run into any merge issues, checkout this [git tutorial](https://github.com/skills/resolve-merge-conflicts) to help you resolve merge conflicts and other issues.

View File

@ -9,4 +9,5 @@ BROKER_USERNAME=""
BROKER_PASSWORD=""
BROKER_CLIENTID=""
BROKER_QOS=""
REST_API_PORT=""
REST_API_PORT=""
REST_API_CORS="" # addresses must be separated by commas example: "http://localhost:3000,http://myapp.com"

View File

@ -3,13 +3,16 @@ package cors
import (
"github.com/rs/cors"
"net/http"
"os"
"strings"
"fmt"
)
func GetCorsConfig() cors.Cors {
allowedOrigins := getCorsEnvConfig()
fmt.Println(allowedOrigins)
return *cors.New(cors.Options{
AllowedOrigins: []string{
"http://localhost:3000",
},
AllowedOrigins: allowedOrigins,
AllowedMethods: []string{
http.MethodGet,
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, ",")
}

View File

@ -0,0 +1,2 @@
CORS_ALLOWED_ORIGINS=""
# addresses must be separated by commas example: "http://localhost:3000,http://myapp.com"

View File

@ -1 +1,2 @@
node_modules/
node_modules/
.env.local

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@
"dependencies": {
"@koa/cors": "^4.0.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"koa": "^2.14.2",
"socket.io": "^4.6.2"

View File

@ -1,13 +1,24 @@
const dotenv = require('dotenv')
dotenv.config();
dotenv.config({ path: `.env.local`, override: true });
const express = require('express');
const app = express();
const PORT = 5000;
const http = require('http').Server(app);
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, {
cors: {
origin: "http://localhost:3000"
origin: allowedOrigins
}
});