Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (73 loc) · 2.36 KB
/
Copy pathapp.js
File metadata and controls
86 lines (73 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require("express");
const path = require("path");
const logger = require("morgan");
const fs = require("fs");
const cors = require("cors");
const graphql = require("./data-management/init-graphql");
const interoperationRouter = require("./routes/interoperation");
const LOG_FOLDER = "logs";
if (!fs.existsSync(LOG_FOLDER)) {
fs.mkdirSync(LOG_FOLDER);
}
// create a write stream (in append mode)
const accessLogStream = fs.createWriteStream(
path.join(__dirname, LOG_FOLDER, "access.log"),
{ flags: "a" }
);
const app = express();
const allowedOrigins = [
"http://localhost:3000",
"http://localhost:7000",
"https://caninecommons-dev.cancer.gov",
"https://caninecommons-qa.cancer.gov",
"https://caninecommons-stage.cancer.gov",
"https://caninecommons.cancer.gov",
"https://caninecommons-test.cancer.gov",
];
// Match any internal CRDC ALB domain
const awsRegions = [
"us-east-1", "us-east-2", "us-west-1", "us-west-2",
];
const awsRegionPattern = awsRegions.join("|");
const internalCrdcAlbRegex = new RegExp(
`^https://internal-crdc-i-[a-z0-9-]+-[a-z0-9]+-[0-9]+\\.(${awsRegionPattern})\\.elb\\.amazonaws\\.com$`
);
function isAllowedOrigin(origin) {
if (!origin) return true;
if (allowedOrigins.includes(origin)) return true;
if (internalCrdcAlbRegex.test(origin)) return true;
return false;
}
app.use(
cors({
origin: function (origin, callback) {
if (isAllowedOrigin(origin)) {
callback(null, true);
} else {
console.warn(`Origin blocked by CORS policy: ${origin}`);
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
allowedHeaders: ["Content-Type"],
})
);
// setup the logger
app.use(logger("combined", { stream: accessLogStream }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api/interoperation", interoperationRouter);
app.use("/api/interoperation/graphql", graphql);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next({ status: 404, message: `Path: '${req.path}' is not supported!` });
});
// error handler
app.use(function (err, req, res, next) {
const message = req.app.get("env") === "development" ? err.message : "error";
// render the error page
res.status(err.status || 500);
res.json(message);
});
module.exports = app;