forked from sourcejs/Source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
176 lines (124 loc) · 3.83 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
/*!
* SourceJS - IME for front-end components documentation and maintenance
* @copyright 2014 Sourcejs.com
* @license MIT license: http://github.com/sourcejs/source/wiki/MIT-License
* */
var express = require('express');
var colors = require('colors');
var fs = require('fs');
var deepExtend = require('deep-extend');
var headerFooter = require('./core/headerFooter.js');
var loadOptions = require('./core/loadOptions');
var commander = require('commander');
/* Globals */
global.app = express();
global.opts = loadOptions();
global.app.set('views', __dirname + '/core/views');
global.app.set('user', __dirname + '/' + global.opts.core.common.pathToUser);
global.MODE = process.env.NODE_ENV || 'development';
/* /Globals */
/* App config */
// Args
commander
.option('-p, --port [number]', 'Server port (default: '+global.opts.core.common.port+')', global.opts.core.common.port)
.parse(process.argv);
// Optimization
global.app.use(express.compress());
// Cookies
global.app.use(express.cookieParser());
app.use(function (req, res, next) {
res.cookie('source-mode', global.MODE, { maxAge: 3600000, httpOnly: false });
// keep executing the router middleware
next();
});
/* /App config */
/* Middlewares */
// LESS processing
if (global.MODE === 'development') {
var less = require('less-middleware');
var lessOpts = {
src: global.app.get('user')
};
deepExtend(lessOpts, global.opts.core.less);
global.app.use(less(lessOpts));
}
// Clarify module
var clarify = require('./core/clarify');
global.app.use(clarify);
global.app.use(express.static(__dirname + '/core/clarify')); // static for module css
// Api first steps
global.app.use('/api/options', function(req, res){
res.jsonp(loadOptions().assets);
});
// File tree module
fileTree = require('./core/file-tree');
global.app.use(function(req, res, next){
if(req.url === "/") {
fileTree.scan();
}
next();
});
// *.src content
var src = require("./core/middleware/src");
global.app.use(src.handleIndex);
global.app.use(src.process);
/* /Middlewares */
/* Includes */
// Routes
require('./core/routes');
// User extenstions
try {
/* User plugins */
require("./core/plugins.js");
/* User additional functionality */
require(global.app.get('user') + "/core/app.js");
} catch(e){}
/* /Includes */
/* Serving content */
// Static content
global.app.use(express.static(global.app.get('user')));
// Page 404
global.app.use(function(req, res, next){
if (req.accepts('html')) {
var headerFooterHTML = headerFooter.getHeaderAndFooter();
res.status(404).render(__dirname + '/core/views/404.ejs', {
header: headerFooterHTML.header,
footer: headerFooterHTML.footer
});
}
});
/* /Serving content */
/* Error handling */
var logErrors = function(err, req, res, next) {
console.error(("Error: " + err.stack).red);
next(err);
};
var clientErrorHandler = function(err, req, res, next) {
if (req.xhr) {
res.send(500, { error: 'Something blew up!' });
} else {
next(err);
}
};
var errorHandler = function(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
};
global.app.use(logErrors);
global.app.use(clientErrorHandler);
global.app.use(errorHandler);
/* /Error handling */
// Server start
if (!module.parent) {
var port = global.opts.core.common.port;
if (commander.port) {
port = parseInt(commander.port);
}
global.app.listen(port);
var portString = port.toString();
var d = new Date(),
dateArr = [d.getHours(), d.getMinutes(), d.getSeconds()],
dateArr = dateArr.map(function (el) { return (el > 9)? el : '0'+ el; }),
dateString = dateArr.join(':').red;
console.log(dateString + ' [SOURCE] lauched on http://localhost:'.blue + portString.red + ' in '.blue + MODE.blue + ' mode...'.blue);
}