-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (116 loc) · 3.89 KB
/
app.js
File metadata and controls
148 lines (116 loc) · 3.89 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
const http = require('http');
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
const cron = require('node-cron');
const rateLimit = require('express-rate-limit');
const {
timeIsRecent,
} = require('@digix/helpers/lib/helpers');
const mongoUtil = require('./dbWrapper/mongoUtil');
const dijixUtil = require('./dijixWrapper/dijixUtil');
const web3Util = require('./web3Wrapper/web3Util');
const cacheUtil = require('./cacheWrapper/cacheUtil');
const {
initContracts,
} = require('./helpers/contracts');
const server = require('./graphql');
const routes = require('./routes');
const scripts = require('./scripts');
const {
setLastSeenBlock,
} = require('./dbWrapper/counters');
const app = express();
let waitingCron;
web3Util.initWeb3(process.env.WEB3_HTTP_PROVIDER);
app.use(cors());
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('json spaces', 4);
const initDB = async () => {
await mongoUtil.connectToServer(process.env.DB_URL, process.env.DIGIXDAO_DB_NAME);
if (process.env.FORCE_REFRESH_DB === 'true') {
await mongoUtil.initFreshDb();
} else if (process.env.RESYNC === 'true') {
await mongoUtil.initToResyncDb();
await mongoUtil.checkAndInitFreshDb();
} else if (process.env.REPROCESS_ONLY === 'true') {
await mongoUtil.initToProcessOnlyDb();
} else {
await mongoUtil.checkAndInitFreshDb();
}
};
const initIpfs = async () => {
const ipfsTimeout = parseInt(process.env.IPFS_TIMEOUT, 10);
await dijixUtil.init(process.env.IPFS_ENDPOINT, process.env.HTTP_ENDPOINT, ipfsTimeout);
};
const addProcessKycCron = async () => {
// kill the cron that was waiting for it to start
waitingCron.stop();
// refresh DAO, now that the DigixDAO has started
const startOfFirstQuarter = await scripts.getStartOfFirstQuarter();
if (timeIsRecent(startOfFirstQuarter)) {
await scripts.refreshDaoTemp();
} else {
await scripts.initDao();
}
scripts.refreshDaoConfigs();
const cronFrequency = process.env.CRON_PROCESS_KYC_FREQUENCY;
cron.schedule(`*/${cronFrequency} * * * *`, async () => {
console.log('INFOLOG: [processKycCron]');
// refresh DAO info and process pending KYC applications
scripts.refreshDao();
scripts.processPendingKycs();
});
};
const addWatchBlocksCron = async () => {
const watchBlocksFrequency = process.env.CRON_WATCH_BLOCKS_FREQUENCY;
cron.schedule(`*/${watchBlocksFrequency} * * * * *`, async () => {
console.log('INFOLOG: [watchBlocksCron]');
// watch for new blocks
scripts.watchNewBlocks();
});
};
const waitForDaoToStart = async () => {
waitingCron = cron.schedule('*/2 * * * * *', async () => {
// schedule a script to run every min
console.log('INFOLOG: waiting for digixdao to start');
if (await scripts.isDaoStarted()) {
addProcessKycCron();
}
});
};
const init = async () => {
console.log('INFOLOG: init');
await initDB();
await initIpfs();
cacheUtil.init();
app.use('/', routes);
const defaultLimiter = rateLimit({
windowMs: process.env.RATE_LIMIT_WINDOW_MS, // 1 minute
max: process.env.RATE_LIMIT_PER_WINDOW, // limit each IP to 10 requests per minute
});
// apply to all requests
app.use(defaultLimiter);
const web3 = web3Util.getWeb3();
const networkId = await web3.version.network;
await initContracts(web3, networkId);
await scripts.syncAndProcessToLatestBlock();
// set the last seen block (at start)
await setLastSeenBlock(web3.eth.blockNumber);
// cron to watch for new blocks
addWatchBlocksCron();
waitForDaoToStart();
};
init();
server.applyMiddleware({
app,
path: '/api',
});
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen(process.env.PORT, () => {
console.log('Info server running on port.', process.env.PORT);
});