-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
139 lines (117 loc) · 3.73 KB
/
server.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
/* Import NPM Modules */
var fs = require("fs");
var express = require("express");
var device = require("express-device");
var slashes = require("connect-slashes");
var subdomains = require('express-subdomains');
var app = express();
var ejs = require("ejs");
var RedisStore = require("connect-redis")(express);
/* IMPORTANT - Global Variables */
GLOBAL.$ = require("jquery");
GLOBAL.async = require("async");
GLOBAL.config = require("./config");
GLOBAL.lib = require("./lib");
/* Create Server */
if(config.general.ssl) {
var srv = require('https').createServer({
key: fs.readFileSync('./credentials/imprint.key'),
cert: fs.readFileSync('./credentials/imprint.crt'),
ca: fs.readFileSync('./credentials/gandi.pem')
}, app);
} else {
var srv = require('http').createServer(app);
}
/* Initialize Lib */
lib.init(ejs);
/* Install Crontab */
require("./cron")(__dirname);
/* Express: Configuration */
app.configure(function() {
//HTML Engine
app.engine("html", ejs.renderFile);
//Global Config
app.set("views", __dirname + "/views");
app.set("view engine", "html");
app.set("view options", { layout: true });
app.set("view cache", true);
app.set("x-powered-by", false);
//Piler Assests
require("./public").init(app, srv);
app.use(require("./public").express);
//Direct Assests
app.use("/favicon", express.static(__dirname + "/public/favicons"));
app.use("/fonts", express.static(__dirname + "/public/fonts"));
app.use("/img", express.static(__dirname + "/public/images"));
//External Addons
app.use(slashes(true));
app.use(device.capture());
//Setup Subdomains
async.each(config.general.subdomains, function(subdomain, next) {
if(subdomain != "") {
subdomains.use(subdomain);
}
next();
}, function() {
app.use(subdomains.middleware);
});
//Logger & Cookie
app.use(express.logger("dev"));
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(config.cookies.session.secret));
app.use(function(req, res, next) {
if(/^\/(?!track)/.test(req.url)) {
express.session({
key: config.cookies.session.key,
secret: config.cookies.session.secret,
store: new RedisStore({
client: lib.redis
})
})(req, res, function() {
express.csrf()(req, res, next);
});
} else {
next();
}
});
//Initialize Models
app.use(lib.models);
//Setup Express Error Handling
app.use(require("./routes/error").express);
//Setup Globals
app.use(require("./routes/globals"));
});
/* Development Only */
app.configure('development', function() {
//Debug Toolbar
require('express-debug')(app, {
theme: __dirname + config.development.debugger.theme,
panels: config.development.debugger.panels
});
});
/* Production Only */
app.configure('production', function() {
//Last Resort Error Handling
process.on('uncaughtException', function(exception) {
lib.error.capture(exception);
return false;
});
});
/* Activate Routes */
require("./routes")(app);
/* Start Router */
app.use(app.router);
//Setup Global Error Handling
app.use(require("./routes/error").global);
/* Lister To Server */
if(config.general.ssl) {
srv.listen(config.general.ports.https);
/* HTTP -> HTTPS Redirect */
express().use(function(req, res) {
res.redirect("https://" + req.host + req.url);
}).listen(config.general.ports.http);
} else {
srv.listen(config.general.ports.http);
}