-
Notifications
You must be signed in to change notification settings - Fork 2
/
groove.js
115 lines (90 loc) · 2.94 KB
/
groove.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
// Store base folder path
global.__base = __dirname + '/';
var fs = require('fs'),
compression = require('compression'),
express = require('express'),
expressSession = require('express-session'),
mongoStore = require('connect-mongo')(expressSession),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
helmet = require('helmet'),
hbs = require('hbs'),
app = express(),
mongoose = require('mongoose'),
passport = require('passport'),
socket = require('./lib/socket.js'),
flash = require('connect-flash'),
channels = require('./lib/channels.js'),
library = require('./lib/library.js');
// library.add('Sorry you\'re sick', 'Ted Hawkins');
// channels.create('Friday!');
// channels.addSong('metal-mash', 'Rose for epona', 'Eluveitie').then(function() {
// console.log('Woo we did it');
// }, function() {
// console.log('Boo');
// });
// Check and load config
var config = require('./config/default.js');
// Create folder structure
if (!fs.existsSync('./data')){
fs.mkdirSync('./data');
}
// Connect to MongoDB
mongoose.connect(config.storage.mongo.url);
// Listen to port
var server = app.listen(config.site.port, function() {
var host = server.address().address;
var port = server.address().port;
console.log('%s listening at http://%s:%s', config.site.title, host, port);
});
// Setup socket
socket = socket(server);
hbs.registerPartials(__dirname + '/client/views/partials');
app.set('view engine', 'hbs');
app.set('views', __dirname + '/client/views');
// GZIP
app.use(compression());
// Serve static files
app.use('/images/channels', express.static('data/images'));
app.use('/music', express.static('data/music'));
app.use('/views', express.static('client/views'));
app.use(express.static('client/static'));
// Helmet
// app.use(helmet({dnsPrefetchControl: false}));
// app.use(helmet.contentSecurityPolicy({
// directives: config.site.csp,
// browserSniff: false
// }));
// Passport setup
app.use(cookieParser());
app.use(bodyParser());
app.use(expressSession({
secret: 'u45lkhsfKJAS',
store: new mongoStore({ mongooseConnection: mongoose.connection }),
resave: true,
saveUninitialized: true,
httpOnly: true,
name: 'groove'
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./lib/passport')(passport);
// Setup default variables
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
res.locals = {}
// Include user details if logged in
if (req.user) {
res.locals.user = req.user;
}
res.locals.title = config.site.title;
// Set correct rendering layout based on request
var layout = req.xhr ? false : 'layout';
res.locals.layout = layout;
next();
});
// Routes
require('./routes/auth')(app, passport);
require('./routes/site')(app);