-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
162 lines (150 loc) · 6.65 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
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
/**
* Dependencies
*/
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var cookies = require( "cookies" )
var session = require('express-session');
/**
* Configuration file
*/
var fs = require('fs');
//try {
//fs.accessSync(__dirname + '/config.json', fs.F_OK | fs.R_OK);
//} catch(err) {
if (fs.existsSync(__dirname + '/config.json') == false) {
console.log('> ERROR: Config file doesn\'t exist or doesn\'t have read permissions.');
console.log(' Please execute "createExpampleData.js" to create all neccessary data objects.');
process.exit();
}
try {
var raw = fs.readFileSync(__dirname + '/config.json', 'utf8');
global.config = JSON.parse(raw);
} catch(err) {
console.log('> ERROR: Config file can\'t be parsed correctly.');
console.log(' Please make sure that "config.json" is a valid json-file.');
process.exit();
}
/**
* Create Express setup and configuration
*/
global.app = express();
app.set('port', process.env.PORT || config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookies.express());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: config.sessionSeed,
saveUninitialized: true,
resave: true
}));
// set the environment to development
var env = process.env.NODE_ENV || 'development';
app.use(function(req,res,next) {
if (req.session !== undefined && req.session.loggedIn !== undefined) {
res.locals.loggedIn = req.session.loggedIn;
res.locals.username = req.session.username;
} else {
res.locals.loggedIn = false;
}
next();
});
// This is used for url parsing in the middle of a jade template
app.locals.url = require('url');
/**
* Data-Controller
*/
global.utils = require('./controllers/utils')();
global.accountData = require('./controllers/accountData')(path.join(__dirname, config.dataPath));
var cashPoolData = require('./controllers/cashPoolsData')(path.join(__dirname, config.dataPath, 'cashPools'));
var billingData = require('./controllers/billingData')(path.join(__dirname, config.dataPath), cashPoolData)
/**
* View-Models
*/
var accountController = require('./controllers/accountPresenter')();
var cashPoolsController = require('./controllers/cashPoolsPresenter')(cashPoolData);
var billingController = require('./controllers/billingPresenter')(billingData);
var calendarController = require('./controllers/calendar')(path.join(__dirname, config.dataPath));
var shoppingListController = require('./controllers/shoppingList')(path.join(__dirname, config.dataPath));
var errorController = require('./controllers/error')();
/**
* Routes
*/
app.get('/', function(res, req, next) { req.render('index', { title: 'WG Title Page'}); });
// Accounts
app.get('/login', accountController.loginGet);
app.post('/login', accountController.loginPost);
app.get('/logout', accountController.logoutGet);
app.get('/account/changePassword', accountController.isAuthenticated, accountController.changePasswordGet);
app.post('/account/changePassword', accountController.isAuthenticated, accountController.changePasswordPost);
app.get('/account/create', accountController.isAuthenticated, accountController.createAccountGet);
app.post('/account/create', accountController.isAuthenticated, accountController.createAccountPost);
app.get('/account/delete', accountController.isAuthenticated, accountController.deleteAccountGet);
app.post('/account/delete', accountController.isAuthenticated, accountController.deleteAccountPost);
// CashPools
app.get('/cashPools', accountController.isAuthenticated, cashPoolsController.showCashPoolsIndex);
app.post('/cashPools', accountController.isAuthenticated, cashPoolsController.addNewPool);
app.get('/cashPools/:id', accountController.isAuthenticated, cashPoolsController.showCashPool);
app.post('/cashPools/:id', accountController.isAuthenticated, cashPoolsController.addNewEntryToPool);
app.post('/cashPools/:id/removeEntry', accountController.isAuthenticated, cashPoolsController.removeEntryFromPool);
app.post('/cashPools/:id/setState', accountController.isAuthenticated, cashPoolsController.setState);
app.post('/cashPools/:id/toggleUserState', accountController.isAuthenticated, cashPoolsController.toggleUserState);
app.post('/cashPools/:id/setFactor', accountController.isAuthenticated, cashPoolsController.setFactor);
app.get('/cashPools/:id/entry/:entryIndex', accountController.isAuthenticated, cashPoolsController.showEntry);
app.post('/cashPools/:id/entry/:entryIndex', accountController.isAuthenticated, cashPoolsController.updatePoolEntry);
// Billing
app.get('/billing', accountController.isAuthenticated, billingController.showBillingIndex)
app.post('/billing', accountController.isAuthenticated, billingController.addNewBill);
app.get('/billing/:id', accountController.isAuthenticated, billingController.showBill);
app.post('/billing/:id/toggleUserState', accountController.isAuthenticated, billingController.toggleBillUserState);
// Shopping List
app.get('/shoppingList', accountController.isAuthenticated, shoppingListController.showShoppingListPage);
app.get('/shoppingList/GetAll', accountController.isAuthenticated, shoppingListController.getAll);
app.post('/shoppingList/Create', accountController.isAuthenticated, shoppingListController.addNewEntry);
app.post('/shoppingList/Update', accountController.isAuthenticated, shoppingListController.updateEntry);
app.delete('/shoppingList/Delete', accountController.isAuthenticated, shoppingListController.deleteEntry);
// Calendar
app.get('/calendar', accountController.isAuthenticated, calendarController.showCalendar);
app.post('/calendar/Save', accountController.isAuthenticated, calendarController.saveEvents);
/**
* Error Handling
*/
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/**
* Run Server
*/
app.listen(app.get('port'), function() {
console.log("> Express server listening on http://localhost/WGTool:%d", app.get('port'));
});