-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (114 loc) · 4.28 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
140
141
142
143
144
145
146
147
148
// For dev, false otherwise
var autologin = true;
var username = 'AuthMasterKrobbs';
var password = 'password';
var User = require('./models/user');
var bodyParser = require('body-parser');
var express = require('express');
var mongoose = require('mongoose');
var getRawBody = require('raw-body')
var typer = require('media-typer')
var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');
var wav = require('wav');
// Session and auth
var passport = require('passport');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var flash = require('connect-flash');
var auth = require('./controllers/auth');
app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// moongoose stuff
mongoose.connect('mongodb://localhost/bandify');
app.use(cookieParser()); // read cookies (needed for auth)
// required for passport
require('./passportConfig')(passport);
app.use(session({ secret: 'krobbelovesburgers' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// Always pass user session object to (jade) templates if available (equals to logged in)
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
// routes/controllers
var example = require('./controllers/example');
var jam = require('./controllers/jam');
var user = require('./controllers/user');
var track = require('./controllers/track');
var musixmatch = require('./controllers/musixmatch');
var band = require('./controllers/band');
app.get('/', function(req, res) {
res.render('index');
});
app.get('/signup', user.signup);
app.get('/login', user.login);
app.get('/logout', user.logout);
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/user', // redirect to the secure user section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/jam', // redirect to the secure user section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/user', auth.isLoggedIn, user.show);
app.get('/user/username/:id', user.searchUsername);
app.get('/user/:id', user.search);
app.get('/jam', jam.index);
app.get('/jam/create', auth.isLoggedIn, jam.create);
app.post('/jam/save', auth.isLoggedIn, jam.save);
app.get('/jam/search', jam.search);
app.get('/jam/getall', jam.getAll);
app.get('/jam/:id', jam.show);
app.get('/jam/delete/:id', jam.delete);
app.get('/jam/search/:id', jam.searchSingleJam);
app.get('/jam/search/jams/:id', jam.searchMultipleJams);
app.post('/jam/addTrack', jam.addTrack);
app.post('/jam/removeTrack', jam.removeTrack);
app.get('/tracks', track.index)
app.get('/tracks/create', auth.isLoggedIn, track.create);
app.post('/tracks/save', auth.isLoggedIn, track.save);
app.get('/tracks/:id', track.show)
app.get('/example', example.index);
app.get('/musixmatch/:artist/:title', musixmatch.findSongs);
app.get('/band', band.index);
app.get('/band/create', auth.isLoggedIn, band.create);
app.post('/band/save', auth.isLoggedIn, band.save);
app.get('/band/user/:id', band.getUserBand);
app.get('/band/delete/:id', band.delete);
app.put('/band/:id/addmember/:name', band.addOneMember);
app.get('/band/:id', band.show);
app.listen(3000);
// Audio streaming
binaryServer = BinaryServer({port: 9001});
binaryServer.on('connection', function(client) {
var r_id = Math.floor(Math.random()*36000),
path = 'public/sound/demo' + r_id + '.wav';
while(fs.existsSync(path)) {
r_id = Math.floor(Math.random()*36000);
path = 'public/sound/demo' + r_id + '.wav';
}
var fileWriter = new wav.FileWriter(path, {
channels: 1,
sampleRate: 48000,
bitDepth: 16
});
client.on('stream', function(stream, meta) {
stream.pipe(fileWriter);
stream.on('end', function() {
fileWriter.end();
app.set('r_id', path);
});
});
});