This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·118 lines (96 loc) · 2.94 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
'use strict';
var express = require('express'),
// _ = require('underscore'),
swig = require('swig'),
flash = require('connect-flash'),
session = require('express-session'),
logger = require('morgan'),
moment = require('moment'),
bodyParser = require('body-parser');
// Load conf
var conf = require('./conf');
console.log('Running app.js in', conf.env, 'environment');
var app = express();
// Connects with db and load models
var db = require('./lib/db');
db.loadModels(['user', 'order']);
// Static assets
app.use(express.static('./public'));
// Template engine
var swigHelpers = require('./views/helpers');
swigHelpers(swig);
/// Add filters
swig.setFilter('time_since', function (input, idx) {
return moment(input).fromNow();
});
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
// Swig cache for production
if (conf.env === 'production') {
console.log('Adding cache to templates', conf.env);
swig.setDefaults({ cache: 'memory' });
} else {
swig.setDefaults({ cache: false });
}
// Add session to the app
var RedisStore = require('connect-redis')(session);
app.use(bodyParser.json({limit: '2mb'}));
app.use(bodyParser.urlencoded({ limit: '2mb', extended: false }));
app.use(logger(':status :req[x-real-ip] :method :response-time ms :url'));
app.use(session({
store: new RedisStore(conf.redis.options),
secret: 'keyboard cat',
saveUninitialized : true,
resave : true
}));
app.use(flash());
app.get('/', function (req, res) {
res.render('home/index');
});
// Controllers
var loginController = require('./controllers/login');
var appController = require('./controllers/app');
var checkoutController = require('./controllers/checkout');
var brandController = require('./controllers/brand');
checkoutController(app);
loginController(app);
appController(app);
brandController(app);
// Admin controllers
var adminDashboard = require('./controllers/admin/dashboard.js');
var adminUsers = require('./controllers/admin/users.js');
var adminOrders = require('./controllers/admin/orders.js');
adminDashboard(app);
adminUsers(app);
adminOrders(app);
//////////// PARAMS
var User = require('./models/user');
var Order = require('./models/order');
app.param('userId', function(req,res, next, id){
User.findOne({_id:id}, function (e, user){
if (e) return res.send(500, e);
if (!user) return res.send(404, e);
req.requestUser = user;
next();
});
});
app.param('orderId', function(req,res, next, id){
var query = Order.findOne({_id:id})
query.populate("user")
query.exec(function (e, order){
if (e) return res.send(500, e);
if (!order) return res.send(404, e);
req.requestOrder = order;
next();
});
});
//////////////
/// LOCALS ///
//////////////
app.locals.mixpanel = conf.mixpanel.id;
app.locals.googleAnalytics = conf.googleAnalytics.id;
app.locals.baseUrl = conf.baseUrl;
app.locals.conektaPublishableKey = conf.conekta.publicKey;
app.listen(conf.port);