forked from cliftonc/calipso
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
168 lines (137 loc) · 4.42 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
/**
* Calipso, a NodeJS CMS
*
* This file is the core application launcher. See app-cluster for visibility
* of how the application should be run in production mode
*
* Usage: node app, or NODE_ENV=production node app
*
*/
var rootpath = process.cwd() + '/',
path = require('path'),
fs = require('fs'),
express = require('express'),
mongoose = require('mongoose'),
sys = require('sys'),
nodepath = require('path'),
form = require('connect-form'),
stylus = require('stylus'),
colors = require('colors'),
calipso = require(path.join(rootpath, 'lib/calipso')),
translate = require(path.join(rootpath, 'i18n/translate')),
logo = require(path.join(rootpath, 'logo')),
mongoStore = require(path.join(rootpath, 'support/connect-mongodb'));
// Local App Variables
var path = rootpath,
theme = 'default',
port = process.env.PORT || 3000,
version = "0.2.3";
/**
* Catch All exception handler
*/
process.on('uncaughtException', function (err) {
console.log('Uncaught exception: ' + err + err.stack);
});
/**
* Placeholder for application
*/
var app, exports;
/**
* App settings and middleware
* Any of these can be added into the by environment configuration files to
* enable modification by env.
*/
function bootApplication(next) {
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.responseTime());
app.use(express.session({ secret: 'calipso', store: mongoStore({ url: app.set('db-uri') }) }));
// Default Theme
calipso.defaultTheme = require(path + '/conf/configuration.js').getDefaultTheme();
// Create holders for theme dependent middleware
// These are here because they need to be in the connect stack before the calipso router
// THese helpers are re-used when theme switching.
app.mwHelpers = {};
// Stylus
app.mwHelpers.stylusMiddleware = function (themePath) {
var mw = stylus.middleware({
src: themePath + '/stylus', // .styl files are located in `views/stylesheets`
dest: themePath + '/public', // .styl resources are compiled `/stylesheets/*.css`
debug: false,
compile: function (str, path) { // optional, but recommended
return stylus(str)
.set('filename', path)
.set('warn', true)
.set('compress', true);
}
});
mw.tag = 'theme.stylus';
return mw;
};
// Load placeholder, replaced later
app.use(app.mwHelpers.stylusMiddleware(''));
// Static
app.mwHelpers.staticMiddleware = function (themePath) {
var mw = express["static"](themePath + '/public', {maxAge: 86400000});
mw.tag = 'theme.static';
return mw;
};
// Load placeholder, replaced later
app.use(app.mwHelpers.staticMiddleware(''));
// Media paths
app.use(express["static"](path + '/media', {maxAge: 86400000}));
// connect-form
app.use(form({
keepExtensions: true
}));
// Translation - after static, set to add mode if appropriate
app.use(translate.translate(app.set('config').language, app.set('language-add')));
// Core calipso router
app.use(calipso.calipsoRouter(next));
}
/**
* Initial bootstrapping
*/
exports.boot = function (next) {
//Create our express instance, export for later reference
app = exports.app = express.createServer();
app.path = path;
app.version = version;
// Import configuration
require(path + '/conf/configuration.js')(app, function (err) {
if (err) {
// Add additional detail to know errors
switch (err.code) {
case "ECONNREFUSED":
console.log("Unable to connect to the specified database: ".red + app.set('db-uri'));
break;
default:
console.log("Fatal unknown error: ".magenta + err);
}
next();
} else {
// Load application configuration
theme = app.set('config').theme;
// Bootstrap application
bootApplication(function () {
next(app);
});
}
});
};
// allow normal node loading if appropriate
// e.g. not called from app-cluster or bin/calipso
if (!module.parent) {
logo.print();
exports.boot(function (app) {
if (app) {
app.listen(port);
console.log("Calipso version: ".green + app.version);
console.log("Calipso configured for: ".green + (global.process.env.NODE_ENV || 'development') + " environment.".green);
console.log("Calipso server listening on port: ".green + app.address().port);
} else {
console.log("\r\nCalipso terminated ...\r\n".grey);
process.exit();
}
});
}