Skip to content

Commit

Permalink
feat: add cors configuration list to express
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 committed Aug 19, 2024
1 parent 0ca11af commit a7b1b8e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# server
PORT=8888
LOG_LEVEL=info
CORS=http://0.0.0.0:4000

# redis
REDIS=redis://localhost:6379
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ copy [.env.example](./.env.example) to `.env` and fill in the following variable
|:--|:--|
| PORT | Port number of the server |
| LOG_LEVEL | Log level of the server |
| CORS | Allow origin url for CORS |
| REDIS | Redis connection string |
| GOOGLE_APPLICATION_CREDENTIALS | Path to the Google Cloud credential file |
| ID | Google Analytics View ID |
Expand Down
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/cors": "^2.8.17",
"@types/eslint__js": "^8.42.3",
"@types/express": "^4.17.21",
"@types/node": "^22.4.0",
Expand All @@ -30,6 +31,7 @@
},
"dependencies": {
"@google-analytics/data": "^4.8.0",
"cors": "^2.8.5",
"express": "^4.19.2",
"ioredis": "^5.4.1"
}
Expand Down
5 changes: 5 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dotenv.config();
type ServerConfig = {
port: number;
logLevel: string;
cors: string[];
redis: string;
credentialPath: string;
reportId: string;
Expand All @@ -15,9 +16,13 @@ type ServerConfig = {
const config = ReadConfig();

function ReadConfig(): ServerConfig {
const corsList = process.env.CORS || "";
const cors = corsList.split(",").map((item) => item.trim());

return {
port: Number(process.env.PORT) || 8888,
logLevel: process.env.LOG_LEVEL || "info",
cors: cors,
redis: process.env.REDIS || "",
credentialPath: process.env.GOOGLE_APPLICATION_CREDENTIALS || "",
reportId: process.env.ID || "",
Expand Down
6 changes: 6 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import dotenv from "dotenv";
import cors from "cors";

import ViewRoutes from "./routes/views";
import { interceptor } from "./middleware/interceptor";
Expand All @@ -10,6 +11,11 @@ dotenv.config();

const app = express();

app.use(
cors({
origin: config.cors
})
);
app.use(interceptor.LogRequest.bind(interceptor));
app.use(interceptor.ErrorHandle.bind(interceptor));
app.use("/views", ViewRoutes);
Expand Down

0 comments on commit a7b1b8e

Please # to comment.