add route function

This commit is contained in:
AJ ONeal 2018-10-15 21:06:59 -06:00
parent be7a895dc7
commit 26939a62cf
1 changed files with 80 additions and 53 deletions

View File

@ -599,63 +599,90 @@ function handleApi(req, res) {
)); ));
} }
if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) { function route() {
getConfigOnly(); if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) {
return; getConfigOnly();
return;
}
if (/\b(init|config)\b/.test(opts.pathname)) {
initOrConfig();
return;
}
if (/restart/.test(opts.pathname)) {
restart();
return;
}
//
// Check for proper config
//
if (!state.config.relay || !state.config.email || !state.config.agreeTos) {
invalidConfig();
return;
}
//
// With proper config
//
if (/http/.test(opts.pathname)) {
controllers.http(req, res, opts);
return;
}
if (/tcp/.test(opts.pathname)) {
controllers.tcp(req, res, opts);
return;
}
if (/save|commit/.test(opts.pathname)) {
saveAndCommit();
return;
}
if (/ssh/.test(opts.pathname)) {
controllers.ssh(req, res, opts);
return;
}
if (/enable/.test(opts.pathname)) {
enable();
return;
}
if (/disable/.test(opts.pathname)) {
disable();
return;
}
if (/status/.test(opts.pathname)) {
getStatus();
return;
}
if (/list/.test(opts.pathname)) {
listSuccess();
return;
}
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}}));
} }
if (/\b(init|config)\b/.test(opts.pathname)) {
initOrConfig(); if (!req.headers['content-length'] && !req.headers['content-type']) {
return; route();
}
if (/restart/.test(opts.pathname)) {
restart();
return;
}
//
// Check for proper config
//
if (!state.config.relay || !state.config.email || !state.config.agreeTos) {
invalidConfig();
return;
}
//
// With proper config
//
if (/http/.test(opts.pathname)) {
controllers.http(req, res, opts);
return;
}
if (/tcp/.test(opts.pathname)) {
controllers.tcp(req, res, opts);
return;
}
if (/save|commit/.test(opts.pathname)) {
saveAndCommit();
return;
}
if (/ssh/.test(opts.pathname)) {
controllers.ssh(req, res, opts);
return;
}
if (/enable/.test(opts.pathname)) {
enable();
return;
}
if (/disable/.test(opts.pathname)) {
disable();
return;
}
if (/status/.test(opts.pathname)) {
getStatus();
return;
}
if (/list/.test(opts.pathname)) {
listSuccess();
return; return;
} }
res.setHeader('Content-Type', 'application/json'); var body = '';
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}})); req.on('readable', function () {
var data;
while (true) {
data = req.read();
if (!data) { break; }
body += data.toString();
}
});
req.on('end', function () {
try {
opts.body = JSON.parse(body);
} catch(e) {
res.statusCode = 400;
res.end('{"error":{"message":"POST body is not valid json"}}');
return;
}
route();
});
} }
function serveControlsHelper() { function serveControlsHelper() {
controlServer = http.createServer(handleRemoteClient); controlServer = http.createServer(handleRemoteClient);