-
Notifications
You must be signed in to change notification settings - Fork 55
/
server.js
122 lines (97 loc) · 3.02 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
// Developed by Michel Buffa
const fs = require("fs");
// We need to use the express framework: have a real web server that knows how to send mime types etc.
const express = require("express");
const path = require("path");
// Init globals variables for each module required
const app = express(),
http = require("http"),
server = http.createServer(app);
// Config
const PORT = process.env.PORT,
TRACKS_PATH = "./client/multitrack",
addrIP = process.env.IP;
if (PORT == 8009) {
app.use(function(req, res, next) {
const user = auth(req);
if (user === undefined || user["name"] !== "super" || user["pass"] !== "secret") {
res.statusCode = 401;
res.setHeader("WWW-Authenticate", 'Basic realm="Super duper secret area"');
res.end("Unauthorized");
} else {
next();
}
});
}
app.use(express.static(path.resolve(__dirname, "client")));
// launch the http server on given port
server.listen(PORT || 3000, addrIP || "0.0.0.0", () => {
const addr = server.address();
console.log("MT5 server listening at", addr.address + ":" + addr.port);
});
// routing
app.get("/", (req, res) => res.sendfile(__dirname + "/index.html"));
// routing
app.get("/track", async (req, res) => {
const trackList = await getTracks();
if (!trackList) {
return res.send(404, "No track found");
}
res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify(trackList));
res.end();
});
// routing
app.get("/track/:id", async (req, res) => {
const id = req.params.id;
const track = await getTrack(id);
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();
});
const getTracks = async () => {
const directories = await getFiles(TRACKS_PATH);
return directories.filter(dir => !dir.match(/^.DS_Store$/));
};
const endsWith = (str, suffix) => str.indexOf(suffix, str.length - suffix.length) !== -1;
isASoundFile = fileName => {
if (endsWith(fileName, ".mp3")) return true;
if (endsWith(fileName, ".ogg")) return true;
if (endsWith(fileName, ".wav")) return true;
if (endsWith(fileName, ".m4a")) return true;
return false;
};
const getTrack = async id =>
new Promise(async (resolve, reject) => {
if (!id) reject("Need to provide an ID");
const fileNames = await getFiles(`${TRACKS_PATH}/${id}`);
if (!fileNames) {
reject(null);
}
fileNames.sort();
const track = {
id: id,
instruments: fileNames
.filter(fileName => isASoundFile(fileName))
.map(fileName => ({
name: fileName.match(/(.*)\.[^.]+$/, "")[1],
sound: fileName
}))
};
resolve(track);
});
const getFiles = async dirName =>
new Promise((resolve, reject) =>
fs.readdir(dirName, function(error, directoryObject) {
if (error) {
reject(error);
}
if (directoryObject !== undefined) {
directoryObject.sort();
}
resolve(directoryObject);
})
);