-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
219 lines (181 loc) · 6.18 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
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
210
211
212
213
214
215
216
217
218
/**
* UNICORN BACK-END GLOBAL GUIDELINE
* -------------------
*
* GENERAL SPECIFICATIONS :
* ---------
* The whole system is built on the socket system.
* Every calls from the front-end after the user loaded the page will be done
* From socket calls which have access to all the tools pre-defined by the system.
* You will mainly write code in the `/sockets/` and `/processes/` folders.
*
* APP INITIALIZATION :
* ---------
* The system will load the basic services provided by Express.
* First of all the `helpers` ($h) are loaded followed
* By the `starters` which are initializers of database and socket service (will open the connection).
* It's quickly followed by the `after_starters` which is
* A simple library processing some code depending on the `starters` (database update, etc.)
*
* PAGE LOAD :
* ---------
* When an user shows up, the whole system initialize again the `helpers`
* Followed by the `services` and the `workers` which are dependents of each others.
* Afterwards, you can access those libraries from every `*_socket.js` and `*_process.js` file.
* The user start to handle the sockets calls afterwards.
*
* STRUCTURE EXPLANATION :
* ---------
* controllers /
* app_controller.js -> the only controller we user will be redirected on via HTTP
*
* helpers /
* any(s).js -> the basic helpers of the project, the name must be plural
*
* models /
* Any.js -> the MongoDB schemas which will be loaded by Mongoose (maj + singular)
*
* processes /
* any_process.js -> the complex processes called from the socket controllers when it's to heavy
*
* services /
* any(s).js -> the libraries that need req, res to work (such as sessions or cookies, must be plural)
* sockets.js -> there is also `sockets.js` which will be basically the link between front-end / back-end
*
* sockets /
* any_socket.js -> the socket controllers containing the methods that will be called from the front-end socket system
* validations.js -> the back-end socket validation system, it's a big object which controls all the front-end calls, if there's no validation for a specific call, it output a WARNING and accept it automatically
*
* starters /
* any.js -> the services that must be initialized on server start (not page load)
* after_starters.js -> the module that will be called after the `starters` are initialized, will use them to make some short processes (such as empty the users from the servers)
*
* workers /
* any(s).js -> the libraries that need the `services` to work (such as socket control, socket sending, etc.)
*
* LIBRARIES :
* ---------
* $h -> `helpers` available from anywhere in the application
* they are small helpful methods not depending on any context, they can be dependants from each others
*
* $s -> `services` available in every socket controllers and their children (`/processes/`)
* they are context dependants libraries which play with sessions, sockets and database calls, etc.
*
* $w -> `workers` also vaiable in every socket controller and their children
* they are `services` dependants and got all the other libraries available to process
*
* SOCKET CONTROLLERS :
* ---------
* Every socket controller (`/sockets/*_socket.js`) got a `process` to stay lightweight
* Each time the socket controller method get too big, you MUST put the business logic within its `process`
*
*
* -------------------
*/
/**
* Constants we will use through the app
*/
__ROOT__ = process.cwd();
__START__ = new Date();
__STORAGE__ = {};
/**
* Basics services express need
*/
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser');
var engine = require('ejs-locals');
var routes = require('./routes/index');
var app = express();
/**
* External handy libraries we will use everytime (global scope exception, BE CAREFUL about it, antipattern.)
*/
_ = require('underscore');
/**
* We load all the helpers
*/
var $h = require(__ROOT__ + '/api/helpers');
/**
* We load the starters (database and sockets)
*/
var starters = {
database: require(__ROOT__+'/api/starters/db')(),
sockets: require(__ROOT__+ '/api/starters/sockets')()
}
/**
* We load the after starters
*/
require(__ROOT__ + '/api/starters/post_starters')(starters);
/**
* View engine
*/
app.engine('ejs', engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser('53e323db2e35a688407fc416'));
app.use(session({
secret: '53e323db2e35a688407fc416',
maxAge : new Date(Date.now() + 3600000 * $h.configs.system.session_expiration),
expires : new Date(Date.now() + 3600000 * $h.configs.system.session_expiration),
//cookie: { maxAge: 60000 },
saveUninitialized: true,
resave: true
}));
app.use(express.static(path.join(__dirname, 'public')));
/**
* Database / Sockets injection within req
*/
app.use(function(req, res, next) {
$h.logs.info('We inject the database & sockets for the user ...');
req.sockets = starters.sockets;
req.database = starters.database;
next();
});
app.use('/', routes);
/**
* HTTP Error handling
*/
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/**
* Development mode
*/
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 mode
*/
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/**
* Welcome display
*/
$h.logs.info('--------------------');
$h.logs.info('Unicorn (%s)', $h.configs.about.version);
$h.logs.info('-> Welcome aboard !');
$h.logs.info('-> Your app is ready to use.');
$h.logs.info('--------------------');
module.exports = app;