Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
210 lines (180 loc) · 6.52 KB
/
Copy pathserver.js
File metadata and controls
210 lines (180 loc) · 6.52 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
/**
* @fileOverview Server.js defines the external modules used server-side for
* Ledger, defines server routes, and initializes Express and MongoDB.
*
* @version 0.0.1
*
* @author Zhecan "James" Wang
* @author Ziyi "Jason" Lan
* @author Ian Hill
*
* @requires NPM:dotenv
* @requires NPM:path
* @requires NPM:morgan
* @requires NPM:cookie-parser
* @requires NPM:q
* @requires NPM:express
* @requires NPM:body-parser
* @requires NPM:mongoose
* @requires NPM:express-session
* @requires NPM:connect-mongo
* @requires NPM:less-middleware
* @requires routes/routes
* @requires ultilities/startup
* @requires authentication
*/
// MODULE IMPORTS ==============================================================
// environment variable modules
var dotenv = require('dotenv');
dotenv.config();
// utility modules
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var q = require('q');
// express modules
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// database modules
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
// route modules
var routes = require('./routes/routes');
// authentication modules
var authentication = require('./authentication');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
// startup modules
var startup = require('./utilities/startup');
// less transpiler
var lessMiddleware = require('less-middleware');
// CONFIGURATION ===============================================================
// configure middleware
var pathRoot = path.join(__dirname, 'public');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(lessMiddleware('/stylesheets', {
compress: true,
pathRoot: pathRoot
}));
app.use(express.static(pathRoot));
// CONNECT TO DATABASE =========================================================
mongoose.connect(process.env.MONGODB);
// SECURITY CONFIGURATION ======================================================
var passport = authentication.configure();
app.use(session({
store: new MongoStore({
mongooseConnection: mongoose.connection,
ttl: 24 * 60 * 60
}),
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
// ROUTES ======================================================================
// GET requests
// authentication GET requests
app.get('/logout', authentication.logout);
app.get('/api/getUserPermissions', authentication.getUserPermissions)
// user GET requests
app.get('/api/getUserList',
authentication.authenticateUser, routes.getUserList);
app.get('/auth/getPendingUsers',
authentication.authenticateAdmin, authentication.getPendingUsers);
app.get('/auth/getPendingFundRequests',
authentication.authenticateUser, routes.getPendingFundRequests);
app.get('/api/getUserOrgs',
authentication.authenticateUser, routes.getUserOrgs);
// org GET requests
app.get('/api/getListedOrgs',
authentication.authenticateUser, routes.getListedOrgs);
//ToD0: To be implemented
app.get('/api/getOrgByUrl/:url',
authentication.authenticateUser, routes.getOrgByUrl);
//ToD0: To be implemented
app.get('/api/getOrgFinances',
authentication.authenticateOwner, routes.getOrgFinances);
// request GET requests
app.get('/api/getRequests/:user', authentication.authenticateUser,
routes.getRequests);
app.get('/api/getOrgRequests/:id',
authentication.authenticateUser, routes.getOrgRequests);
//ToD0: To be implemented
app.get('/api/getUserRequests',
authentication.authenticateUser, routes.getUserRequests);
// record GET requests
//ToD0: To be implemented
app.get('/api/getOrgRecords/:id',
authentication.authenticateUser, routes.getOrgRecords);
app.get('/api/getUserRecords',
authentication.authenticateUser, routes.getUserRecords);
// emails
app.get('/api/RegEmail/:email',
authentication.authenticateUser, routes.sendRegEmail);
// POST requests
// authentication POST requests
app.post('/login', authentication.login);
app.post('/checkUniqueUsername', authentication.checkUniqueUsername);
app.post('/register', authentication.register);
app.post('/changeEmail',
authentication.authenticateUser, authentication.changeEmail);
app.post('/changeName',
authentication.authenticateUser, authentication.changeName);
app.post('/changePassword',
authentication.authenticateUser,
authentication.confirmPassword,
authentication.changePassword);
// user POST requests
//ToD0: To be implemented
app.post('/api/changeOwnership',
authentication.authenticateAdmin, routes.changeOwnership);
app.post('/auth/resolveUser',
authentication.authenticateAdmin, authentication.resolveUser);
// org POST requests
app.post('/api/createOrg',
authentication.authenticateOwner, routes.createOrg);
app.post('/api/changeOwner',
authentication.authenticateUser, routes.changeOwner);
//ToD0: To be implemented
app.post('/api/editOrg',
authentication.authenticateOwner, routes.editOrg);
//ToD0: To be implemented
app.post('/api/deleteOrg',
authentication.authenticateOwner, routes.deleteOrg);
// requests POST requests
app.post('/api/createRequest',
authentication.authenticateUser, routes.createRequest);
app.post('/api/editRequest', authentication.authenticateUser, routes.editRequest);
// record POST requests
app.post('/api/createRecord',
authentication.authenticateUser, routes.createRecord);
app.post('/api/editRecord',
authentication.authenticateOwner, routes.editRecord);
app.post('/api/voidRecord',
authentication.authenticateOwner, routes.voidRecord);
// transfer POST requests
app.post('/api/createTransfer',
authentication.authenticateOwner, routes.createTransfer);
app.post('/api/approveTransfer',
authentication.authenticateOwner, routes.approveTransfer);
// AngularJS requests
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// INITIALIZE DATABASE AND DEFAULT ADMIN USER ==================================
var initialize = startup.initialize();
// START SERVER ================================================================
initialize.then(function(err) {
if (!err) {
app.listen(process.env.PORT, function() {
console.log('Ledger running on port:', process.env.PORT);
});
} else {
console.log('STARTUP ERROR: ' + err);
}
});