-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.js
135 lines (105 loc) · 3.17 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
'use strict';
var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require('path');
var mdns = require('mdns');
var requireFu = require('require-fu');
var express = require('express');
var request = require('request');
var discovery = require('./discovery');
var server = this;
var webroot = path.resolve(__dirname, 'static');
//Settings
var settings = {
port: 5006,
cacheDir: './cache',
webroot: webroot,
packagesDir: __dirname + '/package'
};
// 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];
}
}
// Create webroot + tts if not exist
if (!fs.existsSync(webroot)) {
fs.mkdirSync(webroot);
}
if (!fs.existsSync(webroot + '/tts/')) {
fs.mkdirSync(webroot + '/tts/');
}
var app = express();
var api = this;
// this handles registering of all actions
var services = [];
this.registerRestService = function (action, handler) {
console.log("Registered: " + action);
services.push(action);
app.get(action, function (req, res) {
var json = "";
//TODO: check if handler is known
json = handler(discovery, req, res);
if (json === false) {
res.status(500).json({ message: 'error' });
} else if (json === true) {
res.json({ message: 'success' });
}
});
};
this.registerDeviceRestService = function (action, handler) {
action = '/:deviceName' + action;
console.log("Registered: " + action);
services.push(action);
app.get(action, function (req, res) {
var json = "";
var deviceName = req.params.deviceName;
var device = discovery.getDevice(deviceName);
if (device == undefined) {
res.json({message:'No Device found with name ' + deviceName});
return;
}
//TODO: check if handler is known
json = handler(device, req, res);
if (json === false) {
res.status(500).json({ message: 'error' });
} else if (json === true) {
res.json({ message: 'success' });
}
});
};
this.registerServerRestService = function (action, handler) {
console.log("Registered: " + action);
services.push(action);
app.get(action, function (req, res) {
var json = "";
//TODO: check if handler is known
json = handler(server, req, res);
if (json === false) {
res.status(500).json({ message: 'error' });
} else if (json === true) {
res.json({ message: 'success' });
}
});
};
this.getRegisteredServices = function() {
return services;
};
//load packages
requireFu(settings.packagesDir)(this);
//TODO
//app.use(express.static('public'));
//app.use(express.static('files'));
app.use(express.static('static'));
var httpServer = http.createServer(app);
httpServer.listen(settings.port, function () {
var port = httpServer.address().port;
console.log('HTTP REST server listening on port', port);
});
discovery.search();