domotz-remote-pawn
Version:
Domotz Agent
89 lines (76 loc) • 3.61 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
var create = function (express, status, configuration, resourceLocator) {
var router = express.Router();
var api_resources = [];
api_resources.push(['status', 'api/status', ['get']]);
api_resources.push(['location', 'api/location', ['get']]);
api_resources.push(['user', 'api/user', ['get', 'post']]);
api_resources.push(['region', 'api/region', ['get']]);
api_resources.push(['accepted-conditions', 'api/acceptedConditions', ['get', 'put']]);
api_resources.push(['available-licence', 'api/availableLicence', ['get']]);
api_resources.push(['country', 'api/country', ['get']]);
api_resources.push(['user/:user_id/agent', 'api/agent', ['post']]);
api_resources.push(['user/:user_id/agent/:agent_id', 'api/agent', ['put']]);
if (status.get() === status.CONFIGURED) {
api_resources.push(['agent/' + configuration.id, 'api/coupling', ['get']]);
api_resources.push(['agent', 'api/agent', ['get']]);
//api_resources.push(['agent', 'api/agent', ['get', 'delete']]); delete api moved to portal
api_resources.push(['net-info', 'api/netinfo', ['get']]);
api_resources.push(['mtr', 'api/mtr', ['get']]);
api_resources.push(['ndt-server', 'api/ndtServer', ['get']]);
api_resources.push(['network-speed?', 'api/networkSpeed', ['get']]);
api_resources.push(['connectivity', 'api/connectivity', ['get']]);
api_resources.push(['device', 'api/device', ['get']]);
}
console.info('WEBAPP - +------------- INTERNAL API ----------------------+');
api_resources.forEach(function (route) {
route[2].forEach(function (method) {
var moduleName = '../request_handlers/' + route[1];
var path = route[0];
console.info('WEBAPP - Create route: ' + method.toUpperCase() + ' ' + path + '. File: ' + moduleName);
var handler = require(moduleName)[method](resourceLocator, _onException, _onSuccess);
router[method]('/api/v1/' + path, handler);
});
});
console.info('WEBAPP - +-------------------------------------------------+');
return router;
};
module.exports.create = create;
function _onSuccess(res, data, plain) {
console.debug("Success, providing response");
if (!data) {
res.status(204);
} else if (plain) {
res.status(200).send(data);
} else {
res.status(200).send(JSON.stringify(data));
}
res.end();
}
function _onException(res, err) {
if (err === undefined) {
err = {
message: "Unknown error, see logs for further details",
status: 500
};
}
console.warn('Internal Error: ' + err.status + ' - ' + JSON.stringify(err.message || '[ No error message ]'));
console.error(err.stack);
res.status(err.status || 500).send(JSON.stringify(err.message || {}));
res.end();
}