-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
180 lines (152 loc) · 5.52 KB
/
index.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
173
174
175
176
177
178
179
180
// Import and configure dotenv to enable use of environmental variable
const dotenv = require('dotenv');
dotenv.config();
// Import express
const express = require('express');
const http = require('http');
// Import mongoose to connect to Database
const mongoose = require('mongoose');
// Import Models from Wallfair Commons
const wallfair = require('@wallfair.io/wallfair-commons');
const { handleError } = require('./util/error-handler');
const { initDb } = require('@wallfair.io/trading-engine');
// const { initDatabase } = require('@wallfair.io/wallfair-casino');
const { requestLogHandler } = require('./services/request-log-service');
let mongoURL = process.env.DB_CONNECTION;
/**
* CORS options
* @type import('cors').CorsOptions
*/
const corsOptions = {
origin: ["wallfair.io",
/\.wallfair\.io$/,
"alpacasino.io",
"https://alpacasino.io",
/\.alpacasino\.io$/,
/\.ngrok\.io$/,
/\.netlify\.app$/,
/localhost:?.*$/m,
],
credentials: true,
allowedMethods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE'],
allowedHeaders: [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'X-Access-Token',
'Authorization',
],
exposedHeaders: ['Content-Length'],
preflightContinue: false,
};
// Connection to Database
async function connectMongoDB() {
await mongoose.connect(mongoURL, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
readPreference: 'primary',
retryWrites: true,
});
console.log('Connection to Mongo-DB successful');
wallfair.initModels(mongoose);
console.log('Mongoose models initialized');
return mongoose;
}
async function main() {
const mongoDBConnection = await connectMongoDB();
// Initialize the postgres database (trading-engine)
await initDb();
// await initDatabase();
const amqp = require('./services/amqp-service');
await amqp.init();
await amqp.subscribe();
//init redis connection
const { createClient } = require('redis');
const redisClient = createClient({
url: process.env.REDIS_CONNECTION,
no_ready_check: false
});
redisClient.on('connect', () => console.log('::> Redis Client Connected'));
redisClient.on('error', (err) => console.error('<:: Redis Client Error', err));
//init agenda
const { agenda } = require('./util/agenda');
await agenda.start();
//init api-info-channel
const wsInfoChannelService = require('./services/ws-info-channel-service');
await wsInfoChannelService.init(redisClient);
// Import cors
const cors = require('cors');
// Initialise server using express
const server = express();
const httpServer = http.createServer(server);
server.use(cors(corsOptions));
const awsS3Service = require('./services/aws-s3-service');
awsS3Service.init();
//(auto migration) convert roomId to string, when ObjectID
const isObjectIdStillExist = await mongoDBConnection.models.ChatMessage.find(
{ roomId : { $type: "objectId" } }
);
if(isObjectIdStillExist && isObjectIdStillExist.length) {
await mongoDBConnection.models.ChatMessage.updateMany(
{ roomId : { $type: "objectId" } },
[{ $set: { roomId: { $toString: "$$CURRENT.roomId" } } }]
)
}
// Jwt verification
const passport = require('passport');
const auth = require('./util/auth');
auth.setPassportStrategies();
server.use(passport.initialize());
server.use(passport.session());
server.use(auth.evaluateIsAdmin);
server.use(express.json({ limit: '5mb' }));
server.use(express.urlencoded({ limit: '5mb', extended: true }));
// request log handler
server.use(requestLogHandler);
// Home Route
server.get('/', (req, res) => {
res.status(200).send({
message: 'Blockchain meets Prediction Markets made Simple. - Wallfair.',
});
});
// Import Routes
const userRoute = require('./routes/users/users-routes');
const secureRewardsRoutes = require('./routes/users/secure-rewards-routes');
const secureUserRoute = require('./routes/users/secure-users-routes');
const twitchWebhook = require('./routes/webhooks/twitch-webhook');
const chatRoutes = require('./routes/users/chat-routes');
const notificationEventsRoutes = require('./routes/users/notification-events-routes');
const authRoutes = require('./routes/auth/auth-routes');
const userMessagesRoutes = require('./routes/users/user-messages-routes');
const quoteRoutes = require('./routes/users/quote-routes');
const adminRoutes = require('./routes/users/admin-routes');
// Using Routes
server.use('/api/user', userRoute);
server.use('/api/user', passport.authenticate('jwt', { session: false }), secureUserRoute);
server.use('/api/rewards', passport.authenticate('jwt', { session: false }), secureRewardsRoutes);
server.use('/webhooks/twitch/', twitchWebhook);
server.use('/api/chat', chatRoutes);
server.use('/api/notification-events', notificationEventsRoutes);
server.use('/api/auth', authRoutes);
server.use(
'/api/user-messages',
passport.authenticate('jwt', { session: false }),
userMessagesRoutes
);
server.use('/api/quote', passport.authenticate('jwt', { session: false }), quoteRoutes);
server.use('/api/admin', passport.authenticate('jwt_admin', { session: false }), adminRoutes);
// Error handler middleware
// eslint-disable-next-line no-unused-vars
server.use((err, req, res, next) => {
handleError(err, res);
});
// Let server run and listen
const appServer = httpServer.listen(process.env.PORT || 8000, () => {
const { port } = appServer.address();
console.log(`API runs on port: ${port}`);
});
}
main();