-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
182 lines (153 loc) · 5.48 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const express = require('express');
const path = require('path');
const createError = require('http-errors');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const handlebars = require('hbs');
const fs = require('fs');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const pgSession = require('connect-pg-simple')(session);
const favicon = require('serve-favicon');
const { ensureAuthenticated } = require('./config/auth');
const { ensureAdministrator } = require('./config/auth');
const pgClient = require('./db/PGController');
// Favicon handler
// Routers
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const recipesRouter = require('./routes/recipes');
const adminsRouter = require('./routes/admins');
const apiRecipesRouter = require('./routes/api/recipes');
const apiVotesRouter = require('./routes/api/votes');
const apiUsersRouter = require('./routes/api/users');
const apiCategoriesRouter = require('./routes/api/categories');
const apiUnitsRouter = require('./routes/api/units');
const apiFavouritesRouter = require('./routes/api/favourites');
const apiIngredientsRouter = require('./routes/api/ingredients');
const apiShoppingListRouter = require('./routes/api/shoppingList');
const apiReportsRouter = require('./routes/api/reports');
const apiGraphsRouter = require('./routes/api/graphs');
var app = express();
require('./config/passport')(passport);
handlebars.registerHelper('isRecipeMine', (author, email, options) => {
if(author === email) {
return options.fn(this);
}
return options.inverse(this);
});
handlebars.registerHelper('listItem', function (from, to, context, options) {
var item = "";
for (var i = from, j = to; i < j; i++) {
item = item + options.fn(context[i]);
}
return item;
});
handlebars.registerHelper('isBanned', (state, options) => {
if (state === 'Zbanowany'){
return options.inverse(this);
} else {
return options.fn(this);
}
});
handlebars.registerHelper('isDeleted', (state, options) => {
if (state === 'Usunięty'){
return options.inverse(this);
} else {
return options.fn(this);
}
});
handlebars.registerHelper('isRecipeAccepted', (state, options) => {
if (state === 'Zweryfikowany'){
return options.inverse(this);
} else {
return options.fn(this);
}
});
handlebars.registerHelper('isRecipeNotAccepted', (state, options) => {
if (state === 'Niezaakceptowany'){
return options.inverse(this);
} else {
return options.fn(this);
}
});
handlebars.registerHelper('toLowerCase', function (str) {
return str.toLowerCase();
});
handlebars.registerHelper('toUpperCase', function (str) {
return str.toUpperCase();
});
handlebars.registerPartials(__dirname.concat('/views/partials'));
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// Morgan configuration - log in specific format and ignore urls starting with stuff from public dir
app.use(logger(':date[iso] - [:method] - :url :status - :response-time ms', {
skip: (req, res) => req.url.match(/^\/(images)|(javascripts)|(stylesheets)/g)
}));
// Allow express to use json in requests and responses
// Extended true - we might want to use more complex forms
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
store: new pgSession({
pool: pgClient, // Connection pool
tableName: 'user_session', // Use another table-name than the default "session" one
schemaName: 'public',
}),
secret: process.env.FOO_COOKIE_SECRET,
resave: true,
secret: 'secret',
saveUninitialized: true,
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
if (req.user){
res.locals.userId = req.user["id_user"];
res.locals.userEmail = req.user["email_address"];
res.locals.userNickname = req.user["nickname"];
res.locals.userName = req.user["name"];
res.locals.userSurname = req.user["surname"];
res.locals.userIsAdmin = req.user["is_admin"];
}
next();
});
app.use(cookieParser());
// Path to serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.use('/', indexRouter);
app.use('/users', ensureAuthenticated, usersRouter);
app.use('/recipes', ensureAuthenticated, recipesRouter);
app.use('/administrator_panel', ensureAdministrator, adminsRouter);
app.use('/api/recipes', apiRecipesRouter);
app.use('/api/users', apiUsersRouter);
app.use('/api/categories', apiCategoriesRouter);
app.use('/api/units', apiUnitsRouter);
app.use('/api/favourites', apiFavouritesRouter);
app.use('/api/shoppingList',apiShoppingListRouter);
app.use('/api/votes', apiVotesRouter);
app.use('/api/ingredients', apiIngredientsRouter);
app.use('/api/reports', apiReportsRouter);
app.use('/api/graphs', apiGraphsRouter);
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// Error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;