-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·111 lines (98 loc) · 3.17 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
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const help = require('./helper_functions');
var creds = require('./creds');
const session = require('express-session');
const fileupload = require('express-fileupload');
const fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
// Importing Controllers
var Home = require('./Controllers/Home');
var Topic = require('./Controllers/Topic');
var Quiz = require('./Controllers/Quiz');
var Auth = require('./Controllers/Authorization');
var Mobile = require('./Controllers/Mobile');
var Answers = require('./Controllers/Answers');
var Audio = require('./Controllers/Audio');
var Youtube = require('./Controllers/Youtube');
var UserResults = require('./Controllers/UserResults');
var OTA = require('./Controllers/UpdateWebApp');
// Importing Serivces
var AuthService = require('./Services/Authentication');
global.AuthService = AuthService;
var TopicService = require('./Services/TopicService');
global.TopicService = TopicService;
var QuizService = require('./Services/QuizService');
global.QuizService = QuizService;
var UserResultService = require('./Services/UserResultService');
global.UserResultService = UserResultService;
// importing common
var DB = require('./Common/DB');
global.DB = DB;
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'w'});
var log_stdout = process.stdout;
console.log = function(d) {
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
// global declarations
global.help = help;
global.fs = fs;
global.io = io;
// initializing handlebar template engine
app.engine('handlebars', exphbs({defaultLayout: 'main',
helpers: { json: (context) => JSON.stringify(context) }}
));
app.set('view engine', 'handlebars');
// MIDDLEWARES
// logger
const logger = (req,res,next) => {
console.log(`log : ${req.protocol}://${req.get('host')}${req.originalUrl}`)
// console.log(JSON.stringify(req.body))
next()
}
// fileupload
app.use(fileupload())
// public
app.use(express.static(path.join(__dirname, '/public')));
// initializing the logger middleware
app.use(logger)
// initializing body parser
app.use(express.json())
// for handling url encoded data. ie html forms
app.use(express.urlencoded({ extended:false }))
// for session
app.use(session({secret:creds.secret,resave:false,saveUninitialized:true}))
// using controllers
app.use(Home);
app.use(Answers);
app.use(Audio);
app.use(Auth);
app.use(Youtube);
app.use(Mobile);
app.use(Quiz);
app.use(OTA);
app.use(UserResults);
app.use(Topic);
const PORT = process.env.PORT || 5000
// initializing mongo client
const conString = creds.connectionString;
MongoClient.connect(conString, { useUnifiedTopology: true },function(err, db) {
if (err) throw err;
console.log("MONGO CONNECTED")
global.db = db;
http.listen(PORT, () => console.log(`Server up on port ${PORT}`))
});
// const creds = {
// host:'host.docker.internal',
// user: 'root',
// password: 'password',
// database: 'db_name',
// secret: "secret",
// port: 3306
// }