This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (102 loc) · 3.22 KB
/
app.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
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const session = require('express-session');
const rateLimit = require("express-rate-limit");
const helmet = require('helmet')
require('dotenv').config()
//API PATH
const Istumapaikka = require('./api/routes/istumapaikka');
const Kayttajacontrol = require('./api/routes/kayttajacontrol');
const Kayttajat = require('./api/routes/kayttajat');
const LoginControl = require("./api/login/logincontrol");
const LoginAuth = require("./api/login/loginauth");
const Article = require("./api/routes/article");
const Timetable = require("./api/routes/timetable");
const YoutubeReq = require("./api/routes/youtubeurl");
const YoutubeList = require("./api/routes/youtubelist");
const app = express();
//Express session / Cookie
app.use(session({
secret: process.env.COOKIE_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: JSON.parse(process.env.COOKIE_HTTPS), httpOnly:true},
}));
//Express middleware & stuff
app.use(helmet());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use("/login", express.static("index"));
app.use(express.static(path.join(__dirname, 'public')));
app.enable("trust proxy");
app.disable('x-powered-by');
//API LIMITER
const apiLimiter = rateLimit({
windowMs: 2 * 60 * 1000,
max: 1
});
const apiLimiterSong = rateLimit({
windowMs: 10 * 60 * 1000,
max: 5
});
app.use("/api/istumapaikka", apiLimiter);
app.use("/api/youtubereq", apiLimiterSong);
//API request
app.use("/dashboard", LoginAuth);
app.use("/api/istumapaikka", Istumapaikka);
app.use("/api/kayttajacontrol", Kayttajacontrol);
app.use("/api/kayttajat", Kayttajat);
app.use("/api/login", LoginControl);
app.use("/api/article", Article);
app.use("/api/timetable", Timetable);
app.use("/api/youtubereq", YoutubeReq);
app.use("/api/youtubelist", YoutubeList);
//MongoDB connection
mongoose.connect(process.env.DB, { useNewUrlParser: true })
const db = mongoose.connection;
//PORT
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () => {
console.log('We have a server running on PORT: ' + PORT);
});
//Socket IO
const io = require('socket.io').listen(server);
io.of("/musicplayer-socket").on("connection", function (socket) {
console.log("New socket trigger in music-player "+ socket.handshake.address)
});
//Socket IO API requests
app.post('/api/youtubelist/play', (req, res) =>{
if(req.session.login)
{
io.of("/musicplayer-socket").emit("play");
res.send("success");
}
else{ res.sendStatus(403);}
});
app.post('/api/youtubelist/pause', (req, res) =>{
if(req.session.login)
{
io.of("/musicplayer-socket").emit("pause");
res.send("success");
}
else{ res.sendStatus(403);}
});
app.post('/api/youtubelist/refresh', (req, res) =>{
if(req.session.login)
{
io.of("/musicplayer-socket").emit("refresh");
res.send("success");
}
else{ res.sendStatus(403);}
});
app.post('/api/youtubelist/skip', (req, res) =>{
if(req.session.login)
{
io.of("/musicplayer-socket").emit("skip");
res.send("success");
}
else{ res.sendStatus(403);}
});