forked from squallooo/MT5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
132 lines (103 loc) · 2.89 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
// Developed by Michel Buffa
var fs = require("fs");
// We need to use the express framework: have a real web server that knows how to send mime types etc.
var express=require('express');
var path = require('path');
// Init globals variables for each module required
var app = express()
, http = require('http')
, server = http.createServer(app);
// Config
var PORT = process.env.PORT,
TRACKS_PATH = './client/multitrack/',
addrIP = process.env.IP;
//User validation
var auth = express.basicAuth(function(user, pass) {
return (user == "super" && pass == "secret");
},'Super duper secret area');
if(PORT == 8009) {
app.use(auth);
}
// Indicate where static files are located
/*app.configure(function () {
app.use(express.static(__dirname + '/client/'));
}); */
app.use(express.static(path.resolve(__dirname, 'client')));
// launch the http server on given port
server.listen(PORT || 3000, addrIP || "0.0.0.0", function(){
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// routing
app.get('/track', function (req, res) {
function sendTracks(trackList) {
if (!trackList)
return res.send(404, 'No track found');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(trackList));
res.end();
}
getTracks(sendTracks);
//
});
// routing
app.get('/track/:id', function (req, res) {
var id = req.params.id;
function sendTrack(track) {
if (!track)
return res.send(404, 'Track not found with id "' + id + '"');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(track));
res.end();
}
getTrack(id, sendTrack);
});
function getTracks(callback) {
getFiles(TRACKS_PATH, callback);
}
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function isASoundFile(fileName) {
if(endsWith(fileName, ".mp3")) return true;
if(endsWith(fileName, ".ogg")) return true;
if(endsWith(fileName, ".wav")) return true;
return false;
}
function getTrack(id, callback) {
//console.log("id = " + id);
if(!id) return;
getFiles(TRACKS_PATH + id, function(fileNames) {
if(! fileNames) {
callback(null);
return;
}
var track = {
id: id,
instruments: []
};
fileNames.sort();
for (var i = 0; i < fileNames.length; i++) {
// filter files that are not sound files
if(!isASoundFile(fileNames[i])) continue;
var instrument = fileNames[i].match(/(.*)\.[^.]+$/, '')[1];
track.instruments.push({
name: instrument,
sound: fileNames[i]
});
}
callback(track);
})
}
function getFiles(dirName, callback) {
fs.readdir(dirName, function(error, directoryObject) {
if(directoryObject !== undefined) {
directoryObject.sort();
}
callback(directoryObject);
});
}