Added API: ears, ears_reset, ears_random, sound, sound_control, tts.
Code clean-up.
This commit is contained in:
222
handlers.js
222
handlers.js
@@ -25,44 +25,61 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
var querystring = require('querystring');
|
var querystring = require('querystring');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
var log = require('./log');
|
var log = require('./log');
|
||||||
|
|
||||||
function start(res, req) {
|
/**
|
||||||
log.trace('start: begin');
|
* Handler for home page.
|
||||||
|
* @param res - result
|
||||||
|
* @param req - request
|
||||||
|
*/
|
||||||
|
function homepage(res, req) {
|
||||||
|
log.trace('homepage: begin');
|
||||||
|
|
||||||
var body = '<html>'+
|
var body = '<html>'
|
||||||
'<head>'+
|
+ '<head>'
|
||||||
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+
|
+ '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'
|
||||||
'<title>OpenKarotz Emulator</title>'+
|
+ '<title>OpenKarotz Emulator</title>'
|
||||||
'</head>'+
|
+ '</head>'
|
||||||
'<body>'+
|
+ '<body>'
|
||||||
'<h1>OpenKarotz Emulator</h1>'+
|
+ '<h1>OpenKarotz Emulator</h1>'
|
||||||
'<h2>API</h2>'+
|
+ '<h2>API</h2>'
|
||||||
'<ul>'+
|
+ '<ul>'
|
||||||
'<li><a target="results" href="/cgi-bin/sleep">sleep</a></li>'+
|
+ '<li><a target="results" href="/cgi-bin/sleep">sleep</a></li>'
|
||||||
'<li><a target="results" href="/cgi-bin/wakeup?silent=0">wakeup</a> (<a target="results" href="/cgi-bin/wakeup?silent=1">silent</a>)</li>'+
|
+ '<li><a target="results" href="/cgi-bin/wakeup?silent=0">wakeup</a>, <a target="results" href="/cgi-bin/wakeup?silent=1">wakeup(silent)</a></li>'
|
||||||
'<li><a target="results" href="/cgi-bin/reboot">reboot</a></li>'+
|
+ '<li><a target="results" href="/cgi-bin/reboot">reboot</a></li>'
|
||||||
'<li><a target="results" href="/cgi-bin/status">status</a></li>'+
|
+ '<li><a target="results" href="/cgi-bin/status">status</a></li>'
|
||||||
'<li><a target="results" href="/cgi-bin/get_version">get_version</a></li>'+
|
+ '<li><a target="results" href="/cgi-bin/get_version">get_version</a></li>'
|
||||||
'<li><a target="results" href="/cgi-bin/get_free_space">get_free_space</a></li>'+
|
+ '<li><a target="results" href="/cgi-bin/get_free_space">get_free_space</a></li>'
|
||||||
'<li><a target="results" href="/cgi-bin/leds">leds</a></li>'+
|
+ '<li><a target="results" href="/cgi-bin/leds">leds</a></li>'
|
||||||
'</ul>'+
|
+ '<li><a target="results" href="/cgi-bin/ears">ears</a></li>'
|
||||||
'</body>'+
|
+ '<li><a target="results" href="/cgi-bin/ears_reset">ears_reset</a></li>'
|
||||||
'</html>';
|
+ '<li><a target="results" href="/cgi-bin/ears_random">ears_random</a></li>'
|
||||||
|
+ '<li><a target="results" href="/cgi-bin/sound?id=bip">sound(id)</a>, <a target="results" href="/cgi-bin/sound?url=http://play/sound">sound(url)</a></li>'
|
||||||
|
+ '<li><a target="results" href="/cgi-bin/sound_control?cmd=quit">sound_control(quit)</a>, <a target="results" href="/cgi-bin/sound_control?cmd=pause">sound_control(pause)</a></li>'
|
||||||
|
+ '<li><a target="results" href="/cgi-bin/tts?text=Hello%20World">tts</a></li>'
|
||||||
|
+ '</ul>' + '</body>' + '</html>';
|
||||||
|
|
||||||
res.writeHead(200, {'Content-Type' : 'text/html'});
|
res.writeHead(200, {
|
||||||
|
'Content-Type' : 'text/html'
|
||||||
|
});
|
||||||
res.write(body);
|
res.write(body);
|
||||||
res.end();
|
res.end();
|
||||||
log.trace('start: end');
|
log.trace('homepage: end');
|
||||||
}
|
}
|
||||||
exports.start = start;
|
exports.homepage = homepage;
|
||||||
|
|
||||||
function sendResponse(res, data) {
|
function sendResponse(res, data) {
|
||||||
log.trace('sendResponse: ' + data);
|
log.trace('sendResponse: ' + data);
|
||||||
res.writeHead(200, {'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*'});
|
res.writeHead(200, {
|
||||||
|
'Content-Type' : 'text/plain',
|
||||||
|
'Access-Control-Allow-Origin' : '*'
|
||||||
|
});
|
||||||
res.write(data);
|
res.write(data);
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
@@ -78,9 +95,12 @@ function getParameter(req, param, defaultValue) {
|
|||||||
|
|
||||||
function sleep(res, req) {
|
function sleep(res, req) {
|
||||||
log.trace('sleep: begin');
|
log.trace('sleep: begin');
|
||||||
|
|
||||||
|
// TODO: update sleep state
|
||||||
var data1 = '{"return":"0"}';
|
var data1 = '{"return":"0"}';
|
||||||
var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is already sleeping."}';
|
// var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is
|
||||||
// TODO: maintain sleep state
|
// already sleeping."}';
|
||||||
|
|
||||||
sendResponse(res, data1);
|
sendResponse(res, data1);
|
||||||
log.trace('sleep: end');
|
log.trace('sleep: end');
|
||||||
}
|
}
|
||||||
@@ -89,7 +109,10 @@ exports.sleep = sleep;
|
|||||||
function wakeup(res, req) {
|
function wakeup(res, req) {
|
||||||
log.trace('wakeup: begin');
|
log.trace('wakeup: begin');
|
||||||
var silent = getParameter(req, "silent");
|
var silent = getParameter(req, "silent");
|
||||||
|
|
||||||
|
// TODO: update sleep state
|
||||||
var data = '{"return":"0","silent":"' + silent + '"}';
|
var data = '{"return":"0","silent":"' + silent + '"}';
|
||||||
|
|
||||||
sendResponse(res, data);
|
sendResponse(res, data);
|
||||||
log.trace('wakeup: end');
|
log.trace('wakeup: end');
|
||||||
}
|
}
|
||||||
@@ -97,7 +120,10 @@ exports.wakeup = wakeup;
|
|||||||
|
|
||||||
function reboot(res, req) {
|
function reboot(res, req) {
|
||||||
log.trace('reboot: begin');
|
log.trace('reboot: begin');
|
||||||
|
|
||||||
|
// TODO: update sleep state
|
||||||
var data = '{"return":"0"}';
|
var data = '{"return":"0"}';
|
||||||
|
|
||||||
sendResponse(res, data);
|
sendResponse(res, data);
|
||||||
log.trace('reboot: end');
|
log.trace('reboot: end');
|
||||||
}
|
}
|
||||||
@@ -105,7 +131,10 @@ exports.reboot = reboot;
|
|||||||
|
|
||||||
function status(res, req) {
|
function status(res, req) {
|
||||||
log.trace('status: begin');
|
log.trace('status: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
var data = '{"version":"201","ears_disabled":"0","sleep":"0","sleep_time":"0","led_color":"0000FF","led_pulse":"1","tts_cache_size":"4","usb_free_space":"-1","karotz_free_space":"148.4M","eth_mac":"00:00:00:00:00:00","wlan_mac":"01:23:45:67:89:AB","nb_tags":"4","nb_moods":"305","nb_sounds":"14","nb_stories":"0","karotz_percent_used_space":"37","usb_percent_used_space":""}';
|
var data = '{"version":"201","ears_disabled":"0","sleep":"0","sleep_time":"0","led_color":"0000FF","led_pulse":"1","tts_cache_size":"4","usb_free_space":"-1","karotz_free_space":"148.4M","eth_mac":"00:00:00:00:00:00","wlan_mac":"01:23:45:67:89:AB","nb_tags":"4","nb_moods":"305","nb_sounds":"14","nb_stories":"0","karotz_percent_used_space":"37","usb_percent_used_space":""}';
|
||||||
|
|
||||||
sendResponse(res, data);
|
sendResponse(res, data);
|
||||||
log.trace('status: end');
|
log.trace('status: end');
|
||||||
}
|
}
|
||||||
@@ -129,20 +158,143 @@ exports.get_free_space = get_free_space;
|
|||||||
|
|
||||||
function leds(res, req) {
|
function leds(res, req) {
|
||||||
log.trace('leds: begin');
|
log.trace('leds: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
// var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is
|
||||||
|
// sleeping."}';
|
||||||
|
|
||||||
var color = getParameter(req, "color", "00FF00");
|
var color = getParameter(req, "color", "00FF00");
|
||||||
var secondary_color = getParameter(req, "secondary_color", "000000");
|
var color2 = getParameter(req, "color2", "000000");
|
||||||
var pulse = getParameter(req, "pulse", "0");
|
var pulse = getParameter(req, "pulse", "0");
|
||||||
var no_memory = getParameter(req, "no_memory", "0");
|
var no_memory = getParameter(req, "no_memory", "0");
|
||||||
var speed = getParameter(req, "speed", "");
|
var speed = getParameter(req, "speed", "");
|
||||||
|
// TODO: handle blink parameter?
|
||||||
|
|
||||||
var data = '{"color":"' + color +
|
var data1 = '{"color":"' + color + '","secondary_color":"' + color2
|
||||||
'","secondary_color":"' + secondary_color +
|
+ '","pulse":"' + pulse + '","no_memory":"' + no_memory
|
||||||
'","pulse":"' + pulse +
|
+ '","speed":"' + speed + '","return":"0"}';
|
||||||
'","no_memory":"' + no_memory +
|
sendResponse(res, data1);
|
||||||
'","speed":"' + speed +
|
|
||||||
'","return":"0"}';
|
|
||||||
sendResponse(res, data);
|
|
||||||
log.trace('leds: end');
|
log.trace('leds: end');
|
||||||
}
|
}
|
||||||
exports.leds = leds;
|
exports.leds = leds;
|
||||||
|
|
||||||
|
function ears(res, req) {
|
||||||
|
log.trace('ears: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
// var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is
|
||||||
|
// sleeping."}';
|
||||||
|
// var data3 = '{"return":"1","msg":"Unable to perform action, ears
|
||||||
|
// disabled."}';
|
||||||
|
|
||||||
|
var left = getParameter(req, "left", "0");
|
||||||
|
var right = getParameter(req, "right", "0");
|
||||||
|
// var noreset = getParameter(req, "noreset", "0"); // Unused
|
||||||
|
|
||||||
|
var data1 = '{"left":"' + left + '","right":"' + right + '","return":"0"}';
|
||||||
|
sendResponse(res, data1);
|
||||||
|
log.trace('ears: end');
|
||||||
|
}
|
||||||
|
exports.ears = ears;
|
||||||
|
|
||||||
|
function ears_reset(res, req) {
|
||||||
|
log.trace('ears_reset: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
var data1 = '{"return":"0"}';
|
||||||
|
// var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is
|
||||||
|
// sleeping."}';
|
||||||
|
// var data3 = '{"return":"1","msg":"Unable to perform action, ears
|
||||||
|
// disabled."}';
|
||||||
|
|
||||||
|
sendResponse(res, data1);
|
||||||
|
log.trace('ears_reset: end');
|
||||||
|
}
|
||||||
|
exports.ears_reset = ears_reset;
|
||||||
|
|
||||||
|
function ears_random(res, req) {
|
||||||
|
log.trace('ears_random: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
// var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is
|
||||||
|
// sleeping."}';
|
||||||
|
// var data3 = '{"return":"1","msg":"Unable to perform action, ears
|
||||||
|
// disabled."}';
|
||||||
|
|
||||||
|
var left = Math.floor((Math.random() * 15) + 1);
|
||||||
|
var right = Math.floor((Math.random() * 15) + 1);
|
||||||
|
|
||||||
|
var data1 = '{"left":"' + left + '","right":"' + right + '","return":"0"}';
|
||||||
|
|
||||||
|
sendResponse(res, data1);
|
||||||
|
log.trace('ears_random: end');
|
||||||
|
}
|
||||||
|
exports.ears_random = ears_random;
|
||||||
|
|
||||||
|
function sound(res, req) {
|
||||||
|
log.trace('sound: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
var data = '{"return":"1"}';
|
||||||
|
// var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is
|
||||||
|
// sleeping."}';
|
||||||
|
|
||||||
|
var id = getParameter(req, "id");
|
||||||
|
var url = getParameter(req, "url");
|
||||||
|
|
||||||
|
if (id && url) {
|
||||||
|
data = '{"return":"1","msg":"You cannot use ID and URL parameters at the same time."}';
|
||||||
|
} else if ((id === undefined) && (url === undefined)) {
|
||||||
|
data = '{"return":"1","msg":"No sound to play."}';
|
||||||
|
} else if (id) {
|
||||||
|
data = '{"return":"0"}';
|
||||||
|
// data = '{"return":"1","msg":"Unable to find sound : ' + id + '"}';
|
||||||
|
} else {
|
||||||
|
data = '{"return":"0"}';
|
||||||
|
}
|
||||||
|
|
||||||
|
sendResponse(res, data);
|
||||||
|
log.trace('sound: end');
|
||||||
|
}
|
||||||
|
exports.sound = sound;
|
||||||
|
|
||||||
|
function sound_control(res, req) {
|
||||||
|
log.trace('sound_control: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
var data = '{"return":"1","msg":"Unable to perform action, rabbit is sleeping."}';
|
||||||
|
|
||||||
|
var cmd = getParameter(req, "cmd");
|
||||||
|
if (cmd === undefined) {
|
||||||
|
data = '{"return":"1","msg":"No command specified."}';
|
||||||
|
} else {
|
||||||
|
data = '{"return":"0","cmd":"' + cmd + '"}';
|
||||||
|
}
|
||||||
|
|
||||||
|
sendResponse(res, data);
|
||||||
|
log.trace('sound_control: end');
|
||||||
|
}
|
||||||
|
exports.sound_control = sound_control;
|
||||||
|
|
||||||
|
function tts(res, req) {
|
||||||
|
log.trace('tts: begin');
|
||||||
|
|
||||||
|
// TODO: check sleep state
|
||||||
|
var data = '{"return":"1","msg":"Unable to perform action, rabbit is sleeping."}';
|
||||||
|
|
||||||
|
var text = getParameter(req, "text");
|
||||||
|
var voice = getParameter(req, "voice", "margaux");
|
||||||
|
// var speed = getParameter(req, "speed"); // Unused
|
||||||
|
var nocache = getParameter(req, "nocache", 0);
|
||||||
|
// var engine = getParameter(req, "engine"); // Unused
|
||||||
|
|
||||||
|
if (text === undefined) {
|
||||||
|
data = '{"return":"1","msg":"Missing mandatory parameter(s)."}';
|
||||||
|
} else {
|
||||||
|
data = '{"played":"1","cache":"' + (nocache == 0 ? 1 : 0) + '","return":"0","voice":"' + voice + '"}';
|
||||||
|
}
|
||||||
|
|
||||||
|
sendResponse(res, data);
|
||||||
|
log.trace('tts: end');
|
||||||
|
}
|
||||||
|
exports.tts = tts;
|
||||||
|
98
index.js
98
index.js
@@ -1,45 +1,53 @@
|
|||||||
/*!
|
/*!
|
||||||
* OpenKarotz Emulator
|
* OpenKarotz Emulator
|
||||||
*
|
*
|
||||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* http://opensource.org/licenses/MIT
|
* http://opensource.org/licenses/MIT
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var log = require('./log');
|
'use strict';
|
||||||
var server = require('./server');
|
|
||||||
var router = require('./router');
|
var log = require('./log');
|
||||||
var handlers = require('./handlers')
|
var server = require('./server');
|
||||||
|
var router = require('./router');
|
||||||
var handle = {}
|
var handlers = require('./handlers');
|
||||||
handle['/'] = handlers.start;
|
|
||||||
handle['/cgi-bin'] = handlers.start;
|
var handle = {};
|
||||||
handle['/cgi-bin/sleep'] = handlers.sleep;
|
handle['/'] = handlers.homepage;
|
||||||
handle['/cgi-bin/wakeup'] = handlers.wakeup;
|
handle['/cgi-bin'] = handlers.homepage;
|
||||||
handle['/cgi-bin/reboot'] = handlers.reboot;
|
handle['/cgi-bin/sleep'] = handlers.sleep;
|
||||||
handle['/cgi-bin/status'] = handlers.status;
|
handle['/cgi-bin/wakeup'] = handlers.wakeup;
|
||||||
handle['/cgi-bin/get_version'] = handlers.get_version;
|
handle['/cgi-bin/reboot'] = handlers.reboot;
|
||||||
handle['/cgi-bin/get_free_space'] = handlers.get_free_space;
|
handle['/cgi-bin/status'] = handlers.status;
|
||||||
handle['/cgi-bin/leds'] = handlers.leds;
|
handle['/cgi-bin/get_version'] = handlers.get_version;
|
||||||
|
handle['/cgi-bin/get_free_space'] = handlers.get_free_space;
|
||||||
log.info('OpenKarotz Emulator');
|
handle['/cgi-bin/leds'] = handlers.leds;
|
||||||
server.start(router.route, handle);
|
handle['/cgi-bin/ears'] = handlers.ears;
|
||||||
|
handle['/cgi-bin/ears_reset'] = handlers.ears_reset;
|
||||||
|
handle['/cgi-bin/ears_random'] = handlers.ears_random;
|
||||||
|
handle['/cgi-bin/sound'] = handlers.sound;
|
||||||
|
handle['/cgi-bin/sound_control'] = handlers.sound_control;
|
||||||
|
handle['/cgi-bin/tts'] = handlers.tts;
|
||||||
|
|
||||||
|
log.info('OpenKarotz Emulator');
|
||||||
|
server.start(router.route, handle);
|
||||||
|
2
log.js
2
log.js
@@ -25,6 +25,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
function trace(message) {
|
function trace(message) {
|
||||||
console.log(new Date() + ' [trace] ' + message);
|
console.log(new Date() + ' [trace] ' + message);
|
||||||
}
|
}
|
||||||
|
56
package.json
56
package.json
@@ -1,28 +1,28 @@
|
|||||||
{
|
{
|
||||||
"name": "OpenKarotz Emulator",
|
"name": "OpenKarotz Emulator",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"description": "HTTP server to emulate OpenKarotz",
|
"description": "HTTP server to emulate OpenKarotz",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"readmeFilename": "README.md",
|
"readmeFilename": "README.md",
|
||||||
"homepage": "http://github.com/hobbe/openkarotz-emulator",
|
"homepage": "http://github.com/hobbe/openkarotz-emulator",
|
||||||
"author": "Olivier Bagot (http://github.com/hobbe)",
|
"author": "Olivier Bagot (http://github.com/hobbe)",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"openkarotz",
|
"openkarotz",
|
||||||
"emulator",
|
"emulator",
|
||||||
"node.js"
|
"node.js"
|
||||||
],
|
],
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"Olivier Bagot (http://github.com/hobbe)"
|
"Olivier Bagot (http://github.com/hobbe)"
|
||||||
],
|
],
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "http://github.com/hobbe/openkarotz-emulator/issues"
|
"url": "http://github.com/hobbe/openkarotz-emulator/issues"
|
||||||
},
|
},
|
||||||
"license": {
|
"license": {
|
||||||
"type": "MIT",
|
"type": "MIT",
|
||||||
"url": "http://github.com/hobbe/openkarotz-emulator/raw/master/LICENSE"
|
"url": "http://github.com/hobbe/openkarotz-emulator/raw/master/LICENSE"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/hobbe/openkarotz-emulator.git"
|
"url": "git://github.com/hobbe/openkarotz-emulator.git"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
90
router.js
90
router.js
@@ -1,44 +1,46 @@
|
|||||||
/*!
|
/*!
|
||||||
* OpenKarotz Emulator
|
* OpenKarotz Emulator
|
||||||
*
|
*
|
||||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* http://opensource.org/licenses/MIT
|
* http://opensource.org/licenses/MIT
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var log = require('./log');
|
'use strict';
|
||||||
|
|
||||||
function route(pathname, handle, res, req) {
|
var log = require('./log');
|
||||||
|
|
||||||
log.trace('Route a request for ' + pathname);
|
function route(pathname, handle, res, req) {
|
||||||
|
|
||||||
if (typeof handle[pathname] === 'function') {
|
log.trace('Route a request for ' + pathname);
|
||||||
return handle[pathname](res, req);
|
|
||||||
} else {
|
if (typeof handle[pathname] === 'function') {
|
||||||
log.failure('No request handler found for ' + pathname);
|
return handle[pathname](res, req);
|
||||||
res.writeHead(404, {'Content-Type' : 'text/plain'});
|
} else {
|
||||||
res.write('404 Not Found');
|
log.failure('No request handler found for ' + pathname);
|
||||||
res.end();
|
res.writeHead(404, {'Content-Type' : 'text/plain'});
|
||||||
}
|
res.write('404 Not Found');
|
||||||
}
|
res.end();
|
||||||
|
}
|
||||||
exports.route = route
|
}
|
||||||
|
|
||||||
|
exports.route = route;
|
||||||
|
96
server.js
96
server.js
@@ -1,47 +1,49 @@
|
|||||||
/*!
|
/*!
|
||||||
* OpenKarotz Emulator
|
* OpenKarotz Emulator
|
||||||
*
|
*
|
||||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* http://opensource.org/licenses/MIT
|
* http://opensource.org/licenses/MIT
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var http = require('http')
|
'use strict';
|
||||||
var url = require('url');
|
|
||||||
var log = require('./log');
|
var http = require('http');
|
||||||
var port = 80;
|
var url = require('url');
|
||||||
|
var log = require('./log');
|
||||||
function start(route, handle) {
|
var port = 80;
|
||||||
|
|
||||||
function onRequest(req, res) {
|
function start(route, handle) {
|
||||||
var pathname = url.parse(req.url).pathname;
|
|
||||||
|
function onRequest(req, res) {
|
||||||
if (pathname !== '/favicon.ico') {
|
var pathname = url.parse(req.url).pathname;
|
||||||
route(pathname, handle, res, req)
|
|
||||||
}
|
if (pathname !== '/favicon.ico') {
|
||||||
}
|
route(pathname, handle, res, req);
|
||||||
|
}
|
||||||
http.createServer(onRequest).listen(port)
|
}
|
||||||
log.info('Server has started on http://localhost:' + port)
|
|
||||||
}
|
http.createServer(onRequest).listen(port);
|
||||||
|
log.info('Server has started on http://localhost:' + port);
|
||||||
exports.start = start
|
}
|
||||||
|
|
||||||
|
exports.start = start;
|
||||||
|
Reference in New Issue
Block a user