-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
150 lines (131 loc) · 4.78 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const OS = require('os');
const cluster = require('cluster');
let numCPUs = OS.cpus().length*2
const dotenv = require('dotenv').config();
const express = require("express");
// var bb = require('express-busboy');
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const session = require('express-session')
const Routes = require("./apps/routes/index");
const cronJob = require('cron').CronJob;
const {cronTask} = require('./crontask.js');
const responseTime = require('response-time');
const app = express();
app.use(responseTime())
/* Cron Task */
var job = new cronJob({
cronTime: '00 05 00 * * *',
onTick: function() {
cronTask()
},
start: false,
timeZone: 'Asia/Kolkata'
});
job.start();
// var corsOptions = {
// origin: ['https://*.acadelearn.com/','http://admin-acadelearn.herokuapp.com/'],
// optionsSuccessStatus: 200 // For legacy browser support
// }
app.use(cors());
app.use(express.json({limit: '500mb'}));
app.use(express.urlencoded({ extended: true, limit: '500mb' }));
const PORT = process.env.PORT || 8080;
const flash = require('connect-flash')
let ALLOWED_ORIGINS = ['https://acade-demo-school.acadelearn.com/','http://admin-acadelearn.herokuapp.com/'];
app.use((req, res, next) => {
let origin = req.headers.origin;
let theOrigin = (ALLOWED_ORIGINS.indexOf(origin) >= 0) ? origin : ALLOWED_ORIGINS[0];
res.header("Access-Control-Allow-Origin", "*");
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
next();
});
// express session
app.use(session({
secret: 'shivam-secerate',
resave: true,
saveUninitialized: true
}));
// connect flash session
app.use(flash());
// global vars session
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg')
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
});
// DB Cofiguration
const options = {
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
};
// connecction MOngo DB
(async() => {
const MONGO_URI = process.env.MONGO_URI;
await mongoose.connect(MONGO_URI, options)
.then(() => console.log(`Mongo DB Connected`))
.catch(err => console.log(err));
mongoose.Promise = global.Promise;
if(cluster.isMaster) {
console.log('Master cluster setting up ' + numCPUs + ' workers...');
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died.');
console.log('Starting a new worker with new pid ' + worker.process.pid);
cluster.fork();
});
}else{
process.env.UV_THREADPOOL_SIZE = numCPUs
app.listen(PORT, () => {
console.log(`App is running pid ${process.pid} on PORT ${PORT}`);
})
}
})()
// login
app.get(`/api/v1/test`, (req, res) => {
res.send(`Test Api running on Dev Environment`);
})
app.use("/api/v1/admin", Routes.adminAuth);
app.use("/api/v1/role", Routes.roleRoutes);
app.use("/api/v1/module", Routes.moduleRoutes);
app.use("/api/v1/permission", Routes.permissionRoutes);
app.use("/api/v1/class", Routes.classRoutes);
app.use("/api/v1/school", Routes.schoolRoutes);
app.use("/api/v1/subject", Routes.subjectRoutes);
app.use("/api/v1/unit", Routes.unitRoutes);
app.use("/api/v1/unit-test", Routes.unitTestRoutes);
app.use("/api/v1/assign-test", Routes.assignTestRoutes);
app.use("/api/v1/chapter", Routes.chapterRoutes);
app.use("/api/v1/student", Routes.studentRoutes);
app.use("/api/v1/teacher", Routes.teacherRoutes);
app.use("/api/v1/question-bank", Routes.questionRoutes);
app.use("/api/v1/principal", Routes.principalRoutes);
app.use("/api/v1/teacher-subject-mapping", Routes.teacherSubjectMappingRoutes);
app.use("/api/v1/teacher-class-mapping", Routes.teacherClassMappingRoutes);
app.use("/api/v1/class-subject-mapping", Routes.classSubjectMappingRoutes);
app.use("/api/v1/mock-test", Routes.mockTestRoutes);
app.use("/api/v1/test-report", Routes.testReportRoutes);
app.use("/api/v1/web", Routes.webRoutes);
const directory = path.join(__dirname, 'apps/uploads');
app.use('/uploads', express.static(directory));
if (process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
app.get('/*', (req, res) => {
const index = path.join(__dirname, 'build', 'index.html');
res.sendFile(index);
});
}