forked from bigyunicorn/security_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
163 lines (116 loc) · 4.45 KB
/
server.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
151
152
153
154
155
156
157
158
159
160
161
162
163
/********************************************************************************/
/* */
/* Main Server for Security Lab */
/* */
/********************************************************************************/
/********************************************************************************/
/* */
/* Imports */
/* */
/********************************************************************************/
var express = require("express");
var favicon = require("serve-favicon");
var bodyParser = require("body-parser");
var session = require("express-session");
var consolidate = require("consolidate"); // Templating library adapter for Express
var swig = require("swig");
var http = require("http");
var marked = require("marked");
var logger = require("morgan");
var config = require("./config.js");
var sessionmanager = require("./session.js");
var profile = require("./profile.js");
var contributions = require("./contributions.js");
var benefits = require("./benefits.js");
var allocations = require("./allocations.js");
var memos = require("./memos.js");
/********************************************************************************/
/* */
/* Setup routing using express */
/* */
/********************************************************************************/
function setup()
{
var app = express();
app.disable("x-powered-by");
app.use(favicon(__dirname + "/app/assets/favicon.ico"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: config.COOKIE_SECRET,
saveUninitialized: true,
resave: true }));
// Register templating engine
app.engine(".html", consolidate.swig);
app.set("view engine", "html");
app.set("views", __dirname + "/app/views");
app.use(express.static(__dirname + "/app/assets"));
app.use(logger('combined'));
// intialize marked library
marked.setOptions({ });
app.locals.marked = marked;
var isAdmin = sessionmanager.isAdminUserMiddleware;
app.get("/", sessionmanager.displayWelcomePage);
app.get("/login", sessionmanager.displayLoginPage);
app.post("/login", sessionmanager.handleLoginRequest);
app.get("/signup", sessionmanager.displaySignupPage);
app.post("/signup", sessionmanager.handleSignup);
// Logout page
app.get("/logout", sessionmanager.displayLogoutPage);
// Handle redirect for learning resources link
app.get("/tutorial", function(req, res, next) {
return res.render("tutorial/a1");
});
app.get("/tutorial/:page", function(req, res, next) {
return res.render("tutorial/" + req.params.page);
});
// anything below here requires login
app.use(sessionmanager.isLoggedInMiddleware);
// The main page of the app
app.get("/dashboard", sessionmanager.displayWelcomePage);
// Profile page
app.get("/profile", profile.displayProfile);
app.post("/profile", profile.handleProfileUpdate);
// Contributions Page
app.get("/contributions", contributions.displayContributions);
app.post("/contributions", contributions.handleContributionsUpdate);
// Benefits Page
app.get("/benefits", benefits.displayBenefits);
app.post("/benefits", benefits.updateBenefits);
// Allocations Page
app.get("/allocations/:userId", allocations.displayAllocations);
// Memos Page
app.get("/memos", memos.displayMemos);
app.post("/memos", memos.addMemos);
// Handle redirect for learning resources link
app.get("/learn", function(req, res, next) {
return res.redirect(req.query.url);
});
// Error handling middleware
app.use(errorHandler);
// Template system setup
swig.setDefaults({
autoescape: false
});
var port = config.PORT;
var server = app.listen(port);
console.log("Listening on " + port);
}
/********************************************************************************/
/* */
/* Handle errors */
/* */
/********************************************************************************/
function errorHandler(err,req,res,next)
{
console.log(err.message);
console.log(err.stack);
res.status(500);
res.render("error-template", { error : err } );
}
/********************************************************************************/
/* */
/* Main program */
/* */
/********************************************************************************/
setup();
/* end of server.js */