This repository has been archived by the owner on Apr 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
162 lines (133 loc) · 5.96 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
var express = require('express');
var mysql = require('mysql');
var mysqlconfig = require('./config/mysqlconfig');
var path = require('path');
var connectAssets = require('connect-assets');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var cookies = require( "cookies" )
var session = require('express-session');
var crypto = require('crypto');
/**
* Create MySQL Server with config data
*/
global.db = mysql.createConnection(mysqlconfig.config);
/**
* Create Express server
*/
var app = express();
/**
* Export the app for testability
*/
module.exports = app;
/**
* Express configuration
*/
app.set('port', process.env.PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.use(connectAssets({
paths: ['public/css', 'public/js', 'bower_components'],
helperContext: app.locals
}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cookies.express());
app.use(session({
secret: 'foobar', // <- this should be a random number or something, but for testing this will be enough
saveUninitialized: true,
resave: true
}));
// This is needed to pass information to the jade templates, if the user is currently logged in
// or not, so that they can show the correct menu options
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();
});
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.locals.pretty = true;
}
// This is used for url parsing in the middle of a jade template
app.locals.url = require('url');
/**
* Multi Language Support
*/
// create the language module and store a reference in the global
// namespace using the identifier 'lang', so it can be accessed
// from everywhere within the application
global.lang = require('./controllers/lang')();
app.locals.supportedLanguages = lang.getSupportedLanguages();
app.use(lang.getDictionary);
/**
* Controllers
*/
var accountController = require('./controllers/account')();
var homeController = require('./controllers/home')();
var personsController = require('./controllers/persons')(db);
var singleController = require('./controllers/person')(db);
var companiesController = require('./controllers/companies')(db);
var singleCompanyController = require('./controllers/company')(db);
var importController = require('./controllers/import')(db);
var errorController = require('./controllers/error')();
/**
* Routes
*/
app.get('/', homeController.index);
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);
app.get('/createadmin', accountController.createAdmin); // <= Dev method, should be removed at release
app.get('/persons', accountController.isAuthenticated, personsController.index);
app.get('/persons/new', accountController.isAuthenticated, personsController.newIndex);
app.post('/persons/new', accountController.isAuthenticated, personsController.addPerson);
app.post('/persons/find', accountController.isAuthenticated, personsController.findID);
app.post('/persons/delete', accountController.isAuthenticated, personsController.delete);
app.post('/searchPerson', accountController.isAuthenticated, personsController.searchPerson);
app.post('/sortColumns', accountController.isAuthenticated, personsController.sortColumns);
app.post('/editMemo', accountController.isAuthenticated, personsController.editMemo);
app.get('/persons/:id', accountController.isAuthenticated, singleController.index);
app.get('/persons/:id/edit', accountController.isAuthenticated, singleController.editIndex);
app.post('/persons/:id/edit', accountController.isAuthenticated, singleController.edit);
app.post('/persons/:id/editMemo', accountController.isAuthenticated, singleController.editMemo);
app.get('/companies', accountController.isAuthenticated, companiesController.index);
app.get('/companies/new', accountController.isAuthenticated, companiesController.newIndex);
app.post('/companies/new', accountController.isAuthenticated, companiesController.addCompany);
app.post('/companies/find', accountController.isAuthenticated, companiesController.findID);
app.get('/companies/:id', accountController.isAuthenticated, singleCompanyController.index);
app.get('/lang/:tag', lang.switchLanguage);
app.get('/import', accountController.isAuthenticated, importController.index);
app.post('/import', accountController.isAuthenticated, importController.handleUpload);
app.get('/about', homeController.about);
app.get('/makecoffee', homeController.coffee);
/**
* Error Handling
*/
// Registers all error handlers for the application
// NOTE: if you want the error handler debug routes, set the second parameter to true
//errorController.registerErrorHandlers(app, false);
/**
* Run server
*/
db.connect(function(err){
if (err) {
console.error('✗ MySQL Connection Error. Please make sure MySQL server is running.');
} else {
console.log("✔ Successfully connected to MySQL database.");
}
});
app.listen(app.get('port'), function() {
console.log("✔ Express server listening on http://localhost:%d", app.get('port'));
});