forked from cliftonc/calipso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
160 lines (125 loc) · 4.45 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
/**
* 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
*
*/
require.paths.unshift(__dirname); //make local paths accessible
var fs = require('fs'),
express = require('express'),
mongoose = require('mongoose'),
sys = require('sys'),
nodepath = require('path'),
form = require('connect-form'),
stylus = require('stylus'),
translate = require('i8n/translate'),
calipso = require('lib/calipso'),
mongoStore = require('support/connect-mongodb');
// Local App Variables
var path = __dirname;
var theme = 'default';
var port = 3000;
var app;
var version = "0.2.0";
/**
* Test the db connection. db.open is async, so we get the CALIPSO ascii art
* before we get the error message, but I left it this way to reduce overhead.
*/
(function(){
var mongodb = require('mongodb'),
Db = mongodb.Db,
Connection = mongodb.Connection,
Server = mongodb.Server;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
//sys.puts(">> Connecting to " + host + ":" + port);
var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
db.open(function(err, db) {
if(err){
console.log("Error connecting to mongodb - maybe it's not running?");
} else {
//console.log("Connected to mongodb.");
db.close();
}
});
})();
/**
* Initial bootstrapping
*/
exports.boot = function(next) {
//Create our express instance
app = express.createServer();
app.path = path;
app.version = version;
// Import configuration
require(path + '/conf/configuration.js')(app, express, function(err){
if(err) {
console.log("There was a fatal error attempting to load the configuration, application will terminate.");
}
// Load application configuration
theme = app.set('config').theme;
// Bootstrap application
bootApplication(app, function() {
next(app);
});
});
};
/**
* App settings and middleware
* Any of these can be added into the by environment configuration files to
* enable modification by env.
*/
function bootApplication(app, 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') }) }));
// Stylus
var stylusMiddleware = stylus.middleware({
src: __dirname + '/themes/' + theme + '/stylus', // .styl files are located in `views/stylesheets`
dest: __dirname + '/themes/' + theme + '/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);
}
});
app.use(stylusMiddleware);
// connect-form
app.use(form({
keepExtensions: true
}));
// Static - tag it so we can replace later
var themeStatic = express.static(path + '/themes/' + theme + '/public');
themeStatic.tag = 'themeStatic';
app.use(themeStatic);
// Media paths
app.use(express.static(path + '/media'));
// 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(app, app.set('config'), function() {
next();
}));
}
// allow normal node loading if appropriate
if (!module.parent) {
console.log("");
console.log("\x1b[36m _ _ \x1b[0m");
console.log("\x1b[36m ___ __ _| (_)_ __ ___ ___ \x1b[0m");
console.log("\x1b[36m / __|/ _` | | | '_ \\/ __|/ _ \\ \x1b[0m");
console.log("\x1b[36m| (__| (_| | | | |_) \\__ \\ (_) | \x1b[0m");
console.log("\x1b[36m \\___|\\__,_|_|_| .__/|___/\\___/ \x1b[0m");
console.log("\x1b[36m |_| \x1b[0m");
console.log("");
exports.boot(function(app) {
app.listen(port);
console.log("\x1b[36mCalipso server listening on port: \x1b[0m %d", app.address().port);
console.log("\x1b[36mCalipso configured for\x1b[0m %s \x1b[36menvironment\x1b[0m\r\n", global.process.env.NODE_ENV || 'development');
});
}