This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
105 lines (90 loc) · 2.65 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
// Node.js Modules
var fs = require('fs');
// NPM Modules
var colors = require('colors');
var path = require('path');
var express = require('express');
var passport = require('passport');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// DB
var mongoose = require('mongoose');
// Custom modules
var env = require('./server/lib/env');
var markdown = require('./server/lib/markdown');
// Validate environment variables
try {
env.validate([
'COOKIE_SECRET',
'GIMLI_REDIRECT_URL',
'GITHUB_CLIENT_ID',
'GITHUB_CLIENT_SECRET',
'NODE_ENV',
'REDIS_URL',
'MONGO_URL'
]);
} catch(err) {
console.error(err.message.underline.red);
console.error("Shutting down due to invalid env configuration".red);
process.exit(1);
}
// Connect to the DB
mongoose.connect(process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL);
var mongo = mongoose.connection;
mongo.on('error', function(err) {
console.error('Error connecting to mongodb'.red + ': ' + err.message);
process.exit(1);
});
mongo.once('open', function callback () {
console.log('Connected to '.yellow + process.env.MONGO_URL.magenta);
});
// Express yoself
var app = express();
// Config
app.set('views', path.join(__dirname, 'server', 'views'));
app.set('view engine', 'jade');
// Static Content
app.use('/static', express.static(path.join(__dirname, 'dist', 'static')));
app.use('/static', function(req, res) {
res.status(404);
res.render('404', { url: req.originalUrl });
});
// Parse cookies and sessions
app.use(cookieParser());
app.use(session({
store: new RedisStore({
url: process.env.REDIS_URL
}),
secret: process.env.COOKIE_SECRET
}));
// Body Parsing
app.use(markdown.rawBodyParser);
app.use(bodyParser());
// Authentication
app.use(passport.initialize());
app.use(passport.session());
// Serve
var socket = process.env.GIMLI_SOCKET;
var port = process.env.PORT || process.env.GIMLI_PORT || 3000;
var server = null;
if (socket) {
// Make sure the socket is gone before trying to create another
fs.unlink(socket, function (err) {
server = app.listen(socket, function() {
console.log('Listening on socket %s', socket);
});
fs.chmod(socket, '0660');
});
} else {
server = app.listen(port, function() {
console.log('Listening on port '.yellow + port.toString().magenta);
console.log('You have my sword, my shield ... and my '.blue + 'axe'.red + '!'.blue);
});
}
var bayeux = require('./server/lib/faye');
bayeux.attach(server);
// Normal Routes
var router = require('./server/router')(app);
module.exports = app;