-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (65 loc) · 2.22 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
import express from 'express';
import get from './src/configs/env.config.js';
import { connectDB } from './src/configs/mongo.config.js';
import { handleNotFoundPage, handleError } from './src/middlewares/error.middleware.js';
import morgan from 'morgan';
import userRoute from './src/routes/user.route.js';
import videoRoutes from './src/routes/video.route.js';
import { getPlayingVideo, getTracksInQueue, getSenior, getJunior, getOther } from './src/services/video.service.js';
import oidc from 'express-openid-connect';
const auth = oidc.auth;
import { createServer } from 'http';
import { Server } from "socket.io";
import * as fs from 'fs';
import moment from 'moment';
const config = {
authRequired: false,
auth0Logout: true,
baseURL: get('base_url'),
clientID: get('client_id'),
issuerBaseURL: get('auth0_domain'),
secret: get('secret'),
}
// config middleware
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer);
io.on('connection', (client) => {
const tracks = getTracksInQueue();
client.emit('update-tracks', tracks)
const { playingVideo, playedTime } = getPlayingVideo();
client.emit('playingVideo', {
playingVideo,
playedTime
});
io.emit('senior-tracks-update', getSenior());
io.emit('junior-tracks-update', getJunior());
io.emit('other-tracks-update', getOther());
var clientIpAddress = client.request.headers['x-forwarded-for'] || client.request.connection.remoteAddress;
fs.appendFile('address.txt', `New connection from ${clientIpAddress} at ${moment().format()} \n`, function(err) {
if (err) console.log(err.message);
});
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.use(auth(config));
app.use(express.static('public'))
// connect DB
connectDB();
// config routes
app.use('/video', videoRoutes());
app.use('/user', userRoute());
app.get("/login", (req, res) => {
res.oidc.login();
});
app.get("/logout", (req, res) => {
req.oidc.logout();
});
// handler error
app.use(handleNotFoundPage);
app.use(handleError);
// start app
const port = get('port');
httpServer.listen(port, () => console.log(`Server start at port ${port}`));
export default io;