forked from kom-1dv611/kom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (70 loc) · 2.65 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
'use strict';
require('dotenv').config();
let express = require('express');
let bodyParser = require('body-parser');
let path = require('path');
let socket = require('socket.io');
let port = process.env.PORT || 2000;
let app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let mongoose = require('mongoose');
let BookingModel = require('./src/models/Booking').model('Booking');
let RoomModel = require('./src/models/Room').model('Room');
let RoomHandler = require('./src/handlers/roomHandler');
let Room = new RoomHandler(RoomModel, BookingModel);
let ngrok = require('ngrok');
async function getPublicUrl() {
console.log("Public url: " + await ngrok.connect(port));
}
getPublicUrl();
require('./src/config/database').initialize();
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();
});
//Setup grouprooms with LNUscraper
app.get('/setup-rooms', (req, res) => {
let LNUscraper = require('./src/libs/setupLNUscraper');
LNUscraper(RoomModel);
})
//Setup grouproom information
app.get('/setup-room-info', async function (req, res) {
let scrape = require('./src/libs/infoScraper');
let rooms = await Room.getRoomsFromDB();
let promises = rooms.map((room, index) => {
return new Promise(async (resolve, reject) => {
let roomName = await scrape(room.name);
resolve(roomName);
})
})
Promise.all(promises)
.then((groupRooms) => {
for (let i = 0; i < groupRooms.length; i++) {
if (groupRooms[i] !== undefined) {
RoomModel.findOne({ name: groupRooms[i].name }, function (err, result) {
result.equipment = groupRooms[i].equipment;
result.size = groupRooms[i].size;
result.save(function (err, data) {
if (err) {
console.log(err);
}
})
})
}
}
}).catch((err) => {
console.log(err);
})
})
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, '/client/build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
let routes = require('./src/routes/routes')(RoomModel, BookingModel);
app.use('/', routes);
http.listen(port, function () {
console.log("Express started on http://localhost:" + port);
console.log("Press Ctrl-C to terminate...");
});