-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (115 loc) · 3.44 KB
/
app.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Load .env files
require("dotenv").config();
var Util = require("./common/Util");
// Loading from admin page
if (process.env.ENVIRONMENT === "HEROKU" && !Util.hasAllRequiredKeys()) {
const express = require("express");
const app = express();
app.use(express.static(__dirname + "/public-admin"));
const server = app.listen(process.env.PORT || 5000, function () {
console.log("Listening on port " + server.address().port);
});
return;
}
const MongoDbClient = require("./database/mongodb/MongoDbClient");
const express = require("express");
const bodyParser = require("body-parser");
const rateLimit = require("express-rate-limit");
const cors = require("cors");
const helmet = require("helmet");
const router = require("./routes");
const common = require("./common/common");
const { errors } = require("celebrate");
const fileUpload = require("express-fileupload");
require("./routes/middleware/passport");
const app = express();
const fs = require("fs");
const https = require("https");
// Set Up Clients.
const dbClient = new MongoDbClient();
if (process.env.ETH_FUNDING_PRIVATE_KEY !== undefined) {
const UportClient = require("./services/blockchain/UportClient");
const blockchainClient = new UportClient();
common.blockchainClient = blockchainClient;
const RskBlockchainClient = require("./services/blockchain/RskBlockchainClient");
const rsk = new RskBlockchainClient();
common.rskClient = rsk;
} else {
const SimpleBlockchainClient = require("./services/blockchain/SimpleBlockchainClient");
const blockchainClient = new SimpleBlockchainClient();
common.blockchainClient = blockchainClient;
}
common.dbClient = dbClient;
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 200, // limit each IP to 100 requests per windowMs
});
// apply to all requests
app.use(limiter);
app.use(express.static(__dirname + "/public"));
app.use(
bodyParser.json({
limit: "50mb",
})
);
app.use(
fileUpload({
limits: {
fileSize: 5000000000, // 50mb
},
abortOnLimit: true,
// this was incorrectly empty when attempting to load
// profile images so using in-memory buffer as a workaround.
useTempFiles: false,
})
);
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
// Using NGIX cors config if production
if (
process.env.ENVIRONMENT === "DEVELOPMENT" ||
process.env.ENVIRONMENT === "HEROKU"
) {
app.use(helmet());
app.use(cors());
}
app.use(errors());
app.use(router);
// error handler
app.use(function (err, req, res, next) {
console.log(err.stack);
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: err,
},
});
});
const port = 5000;
let key;
let cert;
try {
key = fs.readFileSync("/home/ubuntu/STAGING/CERTS/server-key.pem");
cert = fs.readFileSync("/home/ubuntu/STAGING/CERTS/server-cert.pem");
} catch (err) {
console.log("key or cert not available. Continuing... ");
}
if (key !== undefined && cert !== undefined) {
https
.createServer(
{
key: fs.readFileSync("/home/ubuntu/STAGING/CERTS/server-key.pem"),
cert: fs.readFileSync("/home/ubuntu/STAGING/CERTS/server-cert.pem"),
},
app
)
.listen(port, function () {
console.log(
"Mypass listening on port 5000! Go to https://localhost:5000/"
);
});
} else {
const server = app.listen(process.env.PORT || port, function () {
console.log("Mypass Listening on port " + server.address().port);
});
}