forked from jishi/node-sonos-http-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (50 loc) · 1.37 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
'use strict';
var http = require('http');
var SonosDiscovery = require('sonos-discovery');
var SonosHttpAPI = require('./lib/sonos-http-api.js');
var nodeStatic = require('node-static');
var fs = require('fs');
var path = require('path');
var webroot = path.resolve(__dirname, 'static');
var settings = {
port: 5005,
cacheDir: './cache',
webroot: webroot
};
// Create webroot + tts if not exist
if (!fs.existsSync(webroot)) {
fs.mkdirSync(webroot);
}
if (!fs.existsSync(webroot + '/tts/')) {
fs.mkdirSync(webroot + '/tts/');
}
// load user settings
try {
var userSettings = require(path.resolve(__dirname, 'settings.json'));
} catch (e) {
console.log('no settings file found, will only use default settings');
}
if (userSettings) {
for (var i in userSettings) {
settings[i] = userSettings[i];
}
}
var fileServer = new nodeStatic.Server(webroot);
var discovery = new SonosDiscovery(settings);
var api = new SonosHttpAPI(discovery, settings);
var server = http.createServer(function (req, res) {
req.addListener('end', function () {
fileServer.serve(req, res, function (err) {
// If error, route it.
if (!err) {
return;
}
if (req.method === 'GET') {
api.requestHandler(req, res);
}
});
}).resume();
});
server.listen(settings.port, function () {
console.log('http server listening on port', settings.port);
});