forked from seyhani/UT-DMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·125 lines (116 loc) · 4.54 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
'use strict';
const express = require("express"),
router = express.Router(),
app = express(),
path = require('path'),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
cookieParser = require("cookie-parser"),
LocalStrategy = require("passport-local"),
flash = require("connect-flash"),
session = require("express-session"),
methodOverride = require("method-override"),
middleware = require("./middleware/index"),
User = require("./models/user"),
Rule = require("./models/rule"),
Problem = require('./models/problem'),
Puzzle = require('./models/puzzle'),
Clar = require('./models/clar'),
groupRoutes = require("./routes/group"),
dashboardRoutes = require("./routes/dashboard"),
adminRoutes = require("./routes/admin"),
problemRoutes = require("./routes/problems"),
tagRoutes = require("./routes/tag"),
indexRoutes = require("./routes/index"),
competitionRoutes = require("./routes/competition"),
userRoutes = require("./routes/user"),
clarRoutes = require("./routes/clar");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/DMC");
//public/Files/Problems/
///Files/Problems/sadegh/Sources/Homework 15 F95.pdf
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(methodOverride('_method'));
app.use(cookieParser('secret'));
// PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "Devil May Cry",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./config/passport')(passport);
app.use(function(req, res, next){
//app.locals.currentUser = req.user;
res.locals.baseURL = baseURL;
// console.log("PRE:" + req.url);
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
});
app.use( (req, res, next) => {
let probsPath = `${baseURL}/Files/Problems/`;
if(new RegExp(`^` + probsPath).test(req.url)) {
if(!req.user)
return middleware.dmcRedirect(res, '/');
if(req.user.isAdmin)
return next();
if(!req.user.group)
return middleware.dmcRedirect(res, '/');
let probName = [].concat(req.url.replace(probsPath, ``).split(`/`));
if(probName.length > 1 && !req.user.isAdmin && probName[1] != `Sources`)
return middleware.dmcRedirect(res, '/');
console.log(probName[0]);
probName[0] = probName[0].split(`%20`).join(` `);
console.log(probName[0]);
Problem.findOne({name: probName[0]}, (err, prob) => {
if(err || !prob)
return middleware.dmcRedirect(res, '/');
Puzzle.findOne({problem: prob, group: req.user.group}, (err, puzzle) => {
if(err || !puzzle || puzzle.new)
return middleware.dmcRedirect(res, '/');
next();
})
});
}
else
next();
});
app.use(express.static(__dirname + "/public"));
////
var baseUrlLocal = "";
app.use(baseUrlLocal+"/", indexRoutes);
// app.use(baseUrlLocal+"/forum", forumRoutes);
app.use(baseUrlLocal+"/clar/", clarRoutes);
app.use(baseUrlLocal+"/dashboard/", dashboardRoutes);
app.use(baseUrlLocal+"/admin/", groupRoutes);
app.use(baseUrlLocal+"/admin/", competitionRoutes);
app.use(baseUrlLocal+"/admin/", adminRoutes);
app.use(baseUrlLocal+"/admin/users", userRoutes);
app.use(baseUrlLocal+"/admin/problems", problemRoutes);
app.use(baseUrlLocal+"/admin/tags", tagRoutes);
// Rule.findOne({name:"DMC"}).exec(function (err,rule) {
// console.log(rule);
// if(rule)
// rule.remove();
// Rule.create({name:"DMC",startDate: new Date(2017,0,18,14),duration:3600*8000});
// });
Rule.findOne({name:"DMC"}).exec(function (err,rule) {
if(!rule)
Rule.create({name:"DMC",startDate: new Date(2017,0,18,14),duration:3600*8000});
else
console.log(rule);
});
app.use((req, res, next) => {
req.flash(`error`, `Sorry cant find that: `+req.url);
return middleware.dmcRedirect(res, '/');
});
let server = app.listen(3042, function () {
let host = server.address().address;
let port = server.address().port;
console.log(`Survey listening at http://${host}:${port}`);
});