-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
354 lines (298 loc) · 14.9 KB
/
app.js
File metadata and controls
354 lines (298 loc) · 14.9 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var os = require('os');
if (os.hostname() === 'srv01appPPL') {
require('dotenv').load()
}
const express = require('express');
path = require('path');
favicon = require('serve-favicon');
morgan = require('morgan');
cookieParser = require('cookie-parser');
cors = require('cors');
bodyParser = require('body-parser'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
CASAuthentication = require('cas-authentication'),
MongoClient = require('mongodb').MongoClient,
URL = require('./app_api/utils/change_database').local();
// base de datos mongoose
require('./app_api/models/db')
// sync db y ws
require('./app_api/ws').update()
var app = express();
var server = require('http').Server(app);
var debug = require('debug')('espol-ppl:server');
var port = normalizePort('8000')
// CAS URLS
if (process.env.MODO == 'debug') {
process.env.NODE_ENV = 'debug'
}
console.log(process.env.NODE_ENV)
if (process.env.NODE_ENV == 'development') {
var URL_CAS_LOCALHOST = 'http://localhost:8000'
} else if (process.env.NODE_ENV == 'debug') {
var URL_CAS_LOCALHOST = 'http://localhost:7000'
port = normalizePort('7000')
} else {
port = normalizePort(process.env.PORT || '8000');
}
var URL_ESPOL_SERVER = 'http://ppl-assessment.espol.edu.ec'
var URL_HEROKU = 'https://ppl-realtime.herokuapp.com/'
var SERVICE_URL = ''
app.set('port', port);
server.listen(port);
// var io = require("socket.io").listen(server)
var io = require('socket.io')(server, {'pingInterval': 3000, 'pingTimeout': 12000});
app.use(function(req, res, next){
res.io = io;
next();
});
// variables de entorno
if (process.env.NODE_ENV == 'development' || process.env.NODE_ENV == 'production-test' ||process.env.NODE_ENV == 'debug') {
app.use(morgan('dev'));
SERVICE_URL = URL_CAS_LOCALHOST
/*
const webpack = require('webpack')
const path = require('path')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const WebpackConfig = require('./webpack.config')
const compiler = webpack(WebpackConfig)
app.use(webpackDevMiddleware(compiler, {
publicPath: '/__build__/',
stats: {
colors: true,
chunks: false
}
}))
app.use(webpackHotMiddleware(compiler))
*/
} else if (os.hostname() === 'srv01appPPL' || process.env.NODE_ENV == 'production') {
SERVICE_URL = URL_ESPOL_SERVER
app.use(morgan('tiny'))
} else if (process.env.NODE_ENV == 'testing') {
SERVICE_URL = URL_HEROKU
app.use(morgan('tiny'))
}
app.use(favicon(path.join(__dirname, 'public', 'img/favicon.ico')))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: require('./app_api/config/main').secret, // <= en un env
resave: true,
expire: 1 * 24 * 60 * 60 ,
saveUninitialized: true,
store: new MongoStore({
url: require('./app_api/utils/change_database').local(),
ttl: 12 * 60 * 60 // = 14 days. Default
})
}));
var cas = new CASAuthentication({
cas_url : 'https://auth.espol.edu.ec',
service_url : SERVICE_URL,
cas_version : '2.0',
dev_mode_user: 'joeedrod',
is_dev_mode : false,
session_name : 'cas_user',
session_info : 'cas_userinfo',
destroy_session: true
});
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/scripts', express.static(__dirname + '/node_modules/'));
// El problema de las imagenes es por el cors, esta puede ser la solucion
app.all('/*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
function redirecion(req, res , next) {
if (!req.session.cas_user) {
res.redirect('/')
} else {
next();
}
}
// Control de rutas(para que estudiantes no puedan ingresar a profesor ruta)
var procesarSession = require('./app_api/config/auth.cas.config').session
var procesarSessionEstudiante = require('./app_api/config/auth.cas.config').sessionEstudiante
var middleEstudianteControl = require('./app_api/config/auth.cas.config').middlewareControlEstudiante
var middleProfesorControl = require('./app_api/config/auth.cas.config').middlewareControlProfesor
// variables de entorno de middlewares
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV == 'debug') {
app.use('/', express.static(path.join(__dirname, 'app_client/login')));
app.use('/api/session', require('./app_api/routes/login.router'));
var { authProfesor, authEstudiante, authApiProfesor, authApiEstudiante } = require('./app_api/config/auth.config')
var procesarSession = function(req, res, next) {
next()
}
var redirecion = function(req, res, next) {
next()
}
} else if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'production-test' || os.hostname() === 'srv01appPPL' || process.env.NODE_ENV == 'testing') {
app.get( '/', cas.bounce, function(req, res, next) {
res.redirect('/profesores') // asi sea estudiante o profesor, despues se redirigira solo
});
var authProfesor = cas.block;
var authApiProfesor = cas.block;
var authEstudiante = cas.block;
var authApiEstudiante = cas.block;
app.get('/api/session/usuario_conectado', require('./app_api/controllers/login.controller').obtenerUsuarioLoggeado)
app.get( '/api/session/logout', cas.logout)
}
require('./app_api/realtime/realtime')(io)
//vistas
app.use('/profesores',redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores')));
app.use('/profesores/grupos', redirecion,authProfesor , procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/grupos')));
app.use('/profesores/preguntas/estimacion', redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/estimacion')));
app.use('/profesores/preguntas/estimacion/:id', redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/ver-pregunta')));
app.use('/profesores/preguntas/tutorial',redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/tutorial')));
app.use('/profesores/preguntas/tutorial/:id', redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/ver-pregunta')));
app.use('/profesores/preguntas/laboratorio', redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/laboratorio')));
app.use('/profesores/preguntas/laboratorio/:id', redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/ver-pregunta')));
app.use('/profesores/preguntas/nueva-pregunta',redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/preguntas/nueva-pregunta')));
app.use('/profesores/leccion/crear',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/crear')));
app.use('/profesores/leccion/',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/')))
app.use('/profesores/leccion/calificar/grupos/:id_leccion',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/calificar/grupos')))
app.use('/profesores/leccion/calificar/:id_leccion/:id_estudiante/:id_grupo',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/calificar')))
app.use('/profesores/lecciones',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/lecciones')))
app.use('/profesores/leccion/modificar/:id',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/modificar')))
app.use('/profesores/leccion-panel/:id_leccion/paralelo/:id_paralelo' ,redirecion,authProfesor, procesarSession, middleProfesorControl,express.static(path.join(__dirname, 'app_client/profesores/leccion-panel')));
app.use('/profesores/leccion/calificar',redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/calificar')));
app.use('/profesores/leccion/:id',redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/ver/')));
app.use('/profesores/leccion/recalificar/grupos/:id',redirecion, authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/recalificar/grupos')));
app.use('/profesores/leccion/recalificar/:id_leccion/:id_estudiante/:id_grupo',redirecion,authProfesor, procesarSession, middleProfesorControl, express.static(path.join(__dirname, 'app_client/profesores/leccion/recalificar')));
/*
Estudiantes
*/
const { estudianteDandoLeccion, estudiantePuedeDarLeccion } = require('./app_api/middlewares/estudiante.middlewares')
app.use('/estudiantes/single_page',redirecion, authEstudiante, procesarSession, middleEstudianteControl, express.static(path.join(__dirname, 'app_client/estudiantes/dist')));
app.use('/estudiantes/',redirecion, authEstudiante, procesarSession, middleEstudianteControl, express.static(path.join(__dirname, 'app_client/estudiantes/perfil')));
app.use('/estudiantes/ver-leccion/:id',redirecion, authEstudiante, procesarSession, middleEstudianteControl, express.static(path.join(__dirname, 'app_client/estudiantes/ver-leccion')));
app.use('/estudiantes/tomar-leccion',redirecion, authEstudiante, procesarSession, middleEstudianteControl, function(req, res, next) {
var EstudianteModel = require('./app_api/models/estudiante.model')
var ParaleloModel = require('./app_api/models/paralelo.model')
EstudianteModel.obtenerEstudiante(req.session._id, (err, estudiante) => {
ParaleloModel.obtenerParaleloDeEstudiante(req.session._id, (err, paralelo) => {
if (estudiante.codigoIngresado && paralelo.leccionYaComenzo) {
res.redirect('/estudiantes/leccion')
} else {
next()
}
})
})
} , express.static(path.join(__dirname, 'app_client/estudiantes/tomar-leccion')));
app.use('/estudiantes/leccion',redirecion, authEstudiante, procesarSession, middleEstudianteControl, function(req, res, next) {
var EstudianteModel = require('./app_api/models/estudiante.model')
var ParaleloModel = require('./app_api/models/paralelo.model')
EstudianteModel.obtenerEstudiante(req.session._id, (err, estudiante) => {
ParaleloModel.obtenerParaleloDeEstudiante(req.session._id, (err, paralelo) => {
if (estudiante.codigoIngresado && paralelo.leccionYaComenzo) {
next()
} else {
res.redirect('/estudiantes/tomar-leccion')
}
})
})
},express.static(path.join(__dirname, 'app_client/estudiantes/leccion'))); // otro middleware que no pueda ingresar si no esta dando leccion
// app.use('/estudiantes/tomar-leccion' , procesarSession, middleEstudianteControl, estudiantePuedeDarLeccion,express.static(path.join(__dirname, 'app_client/estudiantes/tomar-leccion')));
//
// app.use('/estudiantes/leccion', authEstudiante, procesarSession, estudianteDandoLeccion, middleEstudianteControl, express.static(path.join(__dirname, 'app_client/estudiantes/leccion'))); // otro middleware que no pueda ingresar si no esta dando leccion
// otros
app.use('/otros', function(req, res, next) {
if (req.sessionID) {
if (res) {
MongoClient.connect(URL, function(err, db) {
var collection = db.collection('sessions');
collection.remove({_id: req.sessionID}, function(err, docs) {
req.session = null
db.close();
return next()
})
});
} else {
req.session = null
return next()
}
} else {
req.session = null
next()
}
}, express.static(path.join(__dirname, 'app_client/otros')), cas.logout)
// navbars
app.use('/navbar/profesores' ,express.static(path.join(__dirname, 'app_client/profesores/partials/navbar.html')))
// documentation
app.use('/docs/api' ,express.static(path.join(__dirname, 'docs/api')))
app.use('/docs/code' ,express.static(path.join(__dirname, 'docs/code/_book')))
var authApi = require('./app_api/config/auth.api')
// app_api OJO aqui esta expuesta
app.use('/api/profesores', require('./app_api/routes/profesores.router'));
app.use('/api/estudiantes', require('./app_api/routes/estudiantes.router'));
app.use('/api/grupos', require('./app_api/routes/grupos.router'));
app.use('/api/paralelos', require('./app_api/routes/paralelos.router'));
app.use('/api/lecciones', require('./app_api/routes/lecciones.router'));
app.use('/api/preguntas', require('./app_api/routes/preguntas.router'));
app.use('/api/respuestas', authApi.estudiante, require('./app_api/routes/respuestas.router'));
app.use('/api/capitulos', authApi.profesor, require('./app_api/routes/capitulo.router'));
app.use('/api/calificaciones', authApi.estudiante, require('./app_api/routes/calificacion.router'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Url o metodo no valido');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
var mensaje = err.message;
var error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.json({"errorMessage": mensaje, "errorCodigo": error.status, "estado": false});
});
// module.exports = {app: app, server: server};
// var app = require('../app').app;
var debug = require('debug')('espol-ppl:server');
// var http = require('http');
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}