-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
172 lines (145 loc) · 10.8 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/** TechLead is so cool!!! He knows da wae!!! https://www.youtube.com/c/TechLead ***************************************
...........-....--...`````..............................................................................................
........................................................................................................................
..........................................................----..........................................................
..............................`````...................-/oyhhhhhy+-......``````...................................```````
....................................................:ohddmmmmddddho...`````````````..................................```
...................................................:hmmmdhmmhhdmdmds..................```...`.`..`......................
....-.............................................-hmmmdssmhossdhdmdo...........................```.....................
..------........................................../mNmdhyyhyoooyhymmh...................................................
-.-------........................................./mmhyyhhhsyhhyssdmd...................................................
----------.-.......................................hdsooooo/+oo+++shy.....................................`.............
-----------........................`.............../yooooso++++++++s+.....................................`.`..`........
-----------....-----...............`................+ossssyysooo++//-...................................................
------------....-----------..........................:oosssooso+++-.....................................................
.......--.................----......................../soooo+++oo:......................................................
....................-.....---..........................+yyssssss+-.........................``````.......................
...........`...........................................:oyhhhhso/:......................................................
.......................................................-osyysso++/ooo+:--------.......................----..............
...............................----------------.......:+ossooooshdddddhyo/--------......................-...............
...........................----------------------:/oyhho++++sddmmmmmmdddddh+-...-----------.............................
.....................-----------------------:+syhdmdddy//+sdmmmmmmmmmmdddddho.............-.--..........................
....--..........---------------------------+hmmmmmdddddosdmdddddmmmmmdddddddh-.................................````````.
-----------------------------------------:ydmmmmmddddmmmmddddmmmNmmmmmmmmdddh/................................``````````
----------------------------------------:ymmmNmmdddmmmmmmmmmmmmNNmmdddmmmmddh/---------------...----............````````
--::::::-----------------------------...sdmmNNmdmmmmmNmmmmmmmmNNmmmhyhhhhdddh+----------------....----------..........``
:::::::::::::::------------------------odmmmNNmmmmmmNNmmmmmmmNNmddhddddddhhdd/------------...----....-------............
::::::::::::::::::::::::-----------.../+oshdNNmmmmmNNmmmmmNNNNho+///+oshdddds-------------......-.....----------.-......
:::::::::::::::::---:-----------.....:::::/+sdmmmNNNNmmNmNNNNy/::::::::/sddy:-------------.............-----------------
:::::::::::::------.-......-----...-::://++++symNNNNmmNNNNNNy/:::://///+sNNo....----------..............----------------
:::::::::::-----------------------:/:////+o+oosdmNmmdddhddmh///::////++ymNd-...................---------------------::::
////////////////////::::::::///////////::::::://///::::::///////////+odNNmo-.................--------------------------:
++++++++++++++++++++++++++++++++++///////////++++++oos+/++++//////++ohmNmd:--------------:::::::::::::::--::::::::::::::
oooooooooo++++++++++++++++++++++++++++ooooossssssyyso++++++////++++oydmmmd/:://:::///::::::::///////////////////////////
oooooooooooooooooooooooooooo+++++osssyyssssssyhhhsooooooooooooo+ooshmNNmdy+///+//////////////////+++++++/////////////+++
oooooooooooooooooooooooooooooooooosyyssyyyyydhysoosssssyyyhhyyyhdmNNNNmmms+++++++++//////+++++++++++++++++++++++++++++++
ooooooooooooooooooooooooossssssoooooooooooooosdhhhyyhhhhdddmmNNMNNNNNNNmdy+++o++++++++++++++++++++++++++++++++++++++++++
oossoooosoooooooooooosssssssssssssssssssooooodmNNNNNNNNNNNMNNNNNNNNmNNNddyoooooooooooooooooooosssooooooooooooooooooooooo
sssssssssosssssssssssssssssssssssssssssssssssmmmmNNNNNNNNNNNNNNNmNNmmmNmdsosssssssssssssssssssssssoooooosssssssssooossss
ssssssssssssssssssssssssssssssssssssssssssssymmNNNNNNNNNNNNNNNNNmmNmmmNmhsssssssssssssssssssssssssssssssssssssssssssssss
sssssssssssssssssssssssssssssssssssssssssssshmNNNNNNNNNNNNNNNNNNmmmNmmmmhssssssssssssssssssssyyysyyysssssssyyyyyyyyyyyyy
ssssssssssssssssssssssssyssssssssssssssssssshmNNNNNNNNNNNNNNNNNmmmmmmmmmysssssssssssssssssssssssyyyyyyyssysyyyyyyyyyyyyy
ssoosssssssssssssssssssssssssssssssssssssssshNNNNNNNNNNNNNNNNNNmmmmmmmmdysssssssssyyysssssyysyyyyyyysyyysssyyyyyyyyyyyyy
ssosssssssssssssssssssssssssssssssyyyyssssshNNNNNmmNNNNNNNNmmNNNNmmmmdddyyyysyyysssssssyyyyyyyyyyyyyyyyyyyyyyyyyssyyyyyy
ysssssssysssssssssyysyyyyysssssssssssssyyshNmmNmmmNNNNNmmmmNNNmmmNNNmmmhyysyyysyssyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyssyyyyy
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyysssssssymmNNmmNNNNNNmmmmmmNNNNmmmmmhyysssysyyyssssyyyyyyyyyyyyyyyyyyyyyyssyyyyyysssyy
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyhmmmNNNNNNNNNNNNNNNNmmmmmmNdyyyyyyyyyyysysssssyyyyyyyyyyyyyyyyyyyyyssyyyyyysss
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyhhhyyyyyddmmmmmNNNNNNNNNNmmmddhhdddhyyyyyyyyyyyyyyyssssssyyyyyyyyyyyyyyyyyyyysssssyyyy
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyhhyhyhddmmmmmNmNmmddddddhhhhhhhhyyyyyyyyyyyyyyyyyyysyyysyyyyyyyyyyyyhhyyyyyyyysssssyy
yyyyyyyyyyyssyyyyyyyyyyyyyhyyyyyyyyyyhyyhddmmmmmmmmmmdddhhhhhhhhhhhhhsyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
yyyyyyyyy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hddhdhhhddddhdhdddhddyyhddmmmmmmNNNNdhhhhhyyhhhddmmdhhdddyyyhhyyyyyyyyyyyyyyyyyyyyyhhhhyyyyyyyyyyyhyyyhhhhhh
hhhhhhhhhhhhhhhhhhddhhhhdddhhdddhyhdddmmmmmmNNNmddhdhhhhhyyhdmmmmdhhhyyhyhhyyhyyyyyyhyyyyyyyyyhhhyyyyyyhhyyyyyyyyyyhhyyy
hhhhhhhhhhhhhhddddhhhhhhhhhddddhhhdddmmmmmmmNmdddddddhhhhhhhhddmmddddhhhhhhhhhhhyyyhyyyhyyhyyyhhhhhhhhhhyyyhhyyyyyyyyyyy
hhhhhhhhhyydddhhdhhhhhhhhhhhhhhhhhhddmmmmmmmmmhddddddddhhhddhhhdmmddhhhhhhhhhhyyyyyyyhhyyyhhhyyhhhyyhyhhyyyhhhhhhyyhyysy
dddhhhhhhhhhdddhhhhhhhhhhhhhhhhddhhhddmmmmmmmmddddddddddyyhdddhhdddmmdhhhhhyyyyyhhhhhhhyyyyyyhhhhhhhhhyhyhyyyhhhhhhyyyyy
***********************************************************************************************************************/
const { v4: uuidv4 } = require('uuid');
const express = require('express');
const expressStaticGzip = require("express-static-gzip");
const app = express();
const http = require('http');
const server = http.createServer(app);
const _debug = require('debug');
const compression = require('compression');
const requireDir = require("requiredir");
const controllers = requireDir('./controllers');
let Controllers = {};
debug = _debug("liongov");
debug('Debugging liongov');
const ethers = require('ethers');
const c = require('./public/constants');
const blkchain = require('./libs/blockchain');
const Moralis = require('moralis/node');
const IPFS = require('ipfs-core')
const AWS = require("aws-sdk");
dynamoDB = new AWS.DynamoDB(); // Global scope
ipfs = null; // Global scope
IPFS.create().then( e => {ipfs = e});
Moralis.initialize(process.env.MORALIS_APP_ID);
Moralis.serverURL = process.env.MORALIS_SERVER_URL;
Blkchain = new blkchain(Moralis); // Global scope
if(process.env.NODE_ENV == 'development'){
// Allows CROS to development, it's necessary to debug frontend with backend.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
}
// Sends precompressed files whenever possible
app.use("/", expressStaticGzip("dist/", {
enableBrotli: true,
orderPreference: ['br', 'gz']
}));
// Compress on the fly all the rest.
app.use(compression());
app.use(express.static('public'))
// body-parser is deprecated, using below instead: https://stackoverflow.com/questions/66525078/bodyparser-is-deprecated
app.use(express.urlencoded({ extended: true }));
app.use(express.json()) // To parse the incoming requests with JSON payloads
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/healthcheck.html', (req, res) => {
res.send('Health check!');
});
let controller_this = {Blkchain, dynamoDB, _debug}; //Obj that will be accessible in controller 'this'
// Load routes and controllers
Object.keys(controllers).forEach((Contr_name) => {
if(Contr_name == "length" || Contr_name == "toArray"){return;}
controller_this.debug = debug.extend(Contr_name);
Controller = Controllers[Contr_name] = new controllers[Contr_name](controller_this);
let namel = Contr_name.toLowerCase();
if(Controller.get) {
app.get('/' + namel, Controller.get);
}
if(Controller.post) {
app.post('/' + namel, Controller.post);
}
});
// Some prerender to get Proposal title in social links
app.get('/proposal/:uid', Controllers.proposal.get);
// TODO: add filtering/validation on all the APIS. In particular, want to prevent database injection attacks
// TOOD: add API throttling (e.g. by IP address), there are various libraries for that
// TODO: add DDOS protection (e.g. AWS provides some services for this). We can do this after we add a load balancer.
// TODO: show error to client when voting twice
//Redirect all left routes to React
app.get('*', function(req, res){
res.sendFile(__dirname + '/dist/index.html');
});
server.listen(3000, () => {
console.log('listening on *:3000');
Controllers.privacyVoteQuery._privacyVoteQuery({
voteClass: 'matrix'
}, () => {
});
});