This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
88 lines (81 loc) · 3.05 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
import { clientErrorHandler, errorHandler, logErrors } from './middlewares/errorHandlers.js';
import { cookieKey, mongoURI } from './config/keys.js';
import WebSocket from 'ws';
import express from 'express';
import httpServer from 'http';
import connectRabbitmq from './services/rabbitmq/index.js';
import connectStomp from './services/rxstomp/index.js';
import connectSocketio from './services/socketio.js';
import cookieParser from 'cookie-parser';
import cookieSession from 'cookie-session';
import mongoose from 'mongoose';
import passport from 'passport';
import './models/Artist.js';
import './models/CreditPayment.js';
import './models/Favourite.js';
import './models/PaymentSession.js';
import './models/Release.js';
import './models/Sale.js';
import './models/Play.js';
import './models/StreamSession.js';
import './models/User.js';
import './models/Wish.js';
import './services/passport.js';
import artists from './routes/artistRoutes.js';
import artwork from './routes/artworkRoutes.js';
import auth from './routes/authRoutes.js';
import catalogue from './routes/catalogueRoutes.js';
import download from './routes/downloadRoutes.js';
import email from './routes/emailRoutes.js';
import nem from './routes/nemRoutes.js';
import release from './routes/releaseRoutes.js';
import track from './routes/trackRoutes.js';
import user from './routes/userRoutes.js';
const app = express();
const server = httpServer.createServer(app);
global.WebSocket = WebSocket;
const rxStomp = await connectStomp();
const io = connectSocketio(server, rxStomp);
app.set('socketio', io);
await connectRabbitmq(io).catch(error => console.error(error));
const mongooseConfig = {
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
};
const connectMongoose = async () => {
try {
await mongoose.connect(mongoURI, mongooseConfig);
} catch ({ message }) {
console.error('Mongoose connection error: %s', message);
}
};
mongoose.set('debug', process.env.NODE_ENV === 'development');
const db = mongoose.connection;
db.once('open', async () => console.log('Mongoose connected.'));
db.on('error', () => io.emit('error', { message: 'Database connection error.' }));
db.on('disconnected', () => {
console.error('Mongoose disconnected. Attempting to reconnect in 5 seconds…');
setTimeout(mongoose.connect, 5000, mongoURI, mongooseConfig);
});
await connectMongoose();
app.use(express.json());
app.use(cookieParser(cookieKey));
app.use(cookieSession({ name: 'nemp3 session', keys: [cookieKey], maxAge: 28 * 24 * 60 * 60 * 1000 }));
app.use(clientErrorHandler);
app.use(errorHandler);
app.use(logErrors);
app.use(passport.initialize());
app.use(passport.session());
app.use('/api/artists', artists);
app.use('/api/artwork', artwork);
app.use('/api/auth', auth);
app.use('/api/catalogue', catalogue);
app.use('/api/download', download);
app.use('/api/email', email);
app.use('/api/nem', nem);
app.use('/api/release', release);
app.use('/api/track', track);
app.use('/api/user', user);
server.listen(process.env.PORT || 8083, () => console.log('Express server running.'));