2014-11-04 12:34:49 +01:00
|
|
|
/**
|
2016-03-12 01:03:50 +01:00
|
|
|
* Copyright 2014, 2016 IBM Corp.
|
2014-11-04 12:34:49 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var express = require("express");
|
2015-07-15 23:43:24 +02:00
|
|
|
var bodyParser = require("body-parser");
|
2014-11-04 12:34:49 +01:00
|
|
|
var util = require('util');
|
2014-12-10 15:16:07 +01:00
|
|
|
var path = require('path');
|
2014-11-06 23:59:48 +01:00
|
|
|
var passport = require('passport');
|
2015-11-12 10:03:03 +01:00
|
|
|
var when = require('when');
|
2016-05-31 15:55:03 +02:00
|
|
|
var cors = require('cors');
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
var ui = require("./ui");
|
|
|
|
var nodes = require("./nodes");
|
|
|
|
var flows = require("./flows");
|
2015-12-06 23:49:51 +01:00
|
|
|
var flow = require("./flow");
|
2014-11-04 12:34:49 +01:00
|
|
|
var library = require("./library");
|
2014-11-06 23:59:48 +01:00
|
|
|
var info = require("./info");
|
2015-04-13 01:11:11 +02:00
|
|
|
var theme = require("./theme");
|
2015-04-26 22:28:16 +02:00
|
|
|
var locales = require("./locales");
|
2015-11-04 12:13:02 +01:00
|
|
|
var credentials = require("./credentials");
|
2015-11-12 10:03:03 +01:00
|
|
|
var comms = require("./comms");
|
2014-11-06 23:59:48 +01:00
|
|
|
|
|
|
|
var auth = require("./auth");
|
2015-02-06 00:43:35 +01:00
|
|
|
var needsPermission = auth.needsPermission;
|
2014-11-04 12:34:49 +01:00
|
|
|
|
2015-11-22 00:12:39 +01:00
|
|
|
var i18n;
|
2015-11-11 23:11:02 +01:00
|
|
|
var log;
|
|
|
|
var adminApp;
|
|
|
|
var nodeApp;
|
2015-11-24 23:38:42 +01:00
|
|
|
var server;
|
2016-03-12 01:03:50 +01:00
|
|
|
var runtime;
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
var errorHandler = function(err,req,res,next) {
|
2015-11-03 12:23:37 +01:00
|
|
|
if (err.message === "request entity too large") {
|
|
|
|
log.error(err);
|
|
|
|
} else {
|
|
|
|
console.log(err.stack);
|
|
|
|
}
|
|
|
|
log.audit({event: "api.error",error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
2014-11-06 11:00:25 +01:00
|
|
|
};
|
2014-11-04 12:34:49 +01:00
|
|
|
|
2016-03-12 01:03:50 +01:00
|
|
|
var ensureRuntimeStarted = function(req,res,next) {
|
|
|
|
if (!runtime.isStarted()) {
|
|
|
|
log.error("Node-RED runtime not started");
|
|
|
|
res.status(503).send("Not started");
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(_server,_runtime) {
|
2015-11-24 23:38:42 +01:00
|
|
|
server = _server;
|
2016-03-12 01:03:50 +01:00
|
|
|
runtime = _runtime;
|
2015-11-11 23:11:02 +01:00
|
|
|
var settings = runtime.settings;
|
2015-11-22 00:12:39 +01:00
|
|
|
i18n = runtime.i18n;
|
2015-11-11 23:11:02 +01:00
|
|
|
log = runtime.log;
|
|
|
|
if (settings.httpNodeRoot !== false) {
|
|
|
|
nodeApp = express();
|
2015-01-28 23:41:13 +01:00
|
|
|
}
|
2015-11-11 23:11:02 +01:00
|
|
|
if (settings.httpAdminRoot !== false) {
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server,runtime);
|
2015-11-11 23:11:02 +01:00
|
|
|
adminApp = express();
|
|
|
|
auth.init(runtime);
|
|
|
|
credentials.init(runtime);
|
|
|
|
flows.init(runtime);
|
2015-12-06 23:49:51 +01:00
|
|
|
flow.init(runtime);
|
2015-11-11 23:11:02 +01:00
|
|
|
info.init(runtime);
|
|
|
|
library.init(adminApp,runtime);
|
|
|
|
locales.init(runtime);
|
|
|
|
nodes.init(runtime);
|
|
|
|
|
|
|
|
// Editor
|
|
|
|
if (!settings.disableEditor) {
|
|
|
|
ui.init(runtime);
|
|
|
|
var editorApp = express();
|
2016-03-12 01:03:50 +01:00
|
|
|
editorApp.get("/",ensureRuntimeStarted,ui.ensureSlash,ui.editor);
|
2015-11-11 23:11:02 +01:00
|
|
|
editorApp.get("/icons/:icon",ui.icon);
|
2016-01-08 14:36:49 +01:00
|
|
|
theme.init(runtime);
|
2015-11-11 23:11:02 +01:00
|
|
|
if (settings.editorTheme) {
|
2016-01-08 14:36:49 +01:00
|
|
|
editorApp.use("/theme",theme.app());
|
2015-11-11 23:11:02 +01:00
|
|
|
}
|
|
|
|
editorApp.use("/",ui.editorResources);
|
|
|
|
adminApp.use(editorApp);
|
|
|
|
}
|
|
|
|
var maxApiRequestSize = settings.apiMaxLength || '1mb';
|
|
|
|
adminApp.use(bodyParser.json({limit:maxApiRequestSize}));
|
|
|
|
adminApp.use(bodyParser.urlencoded({limit:maxApiRequestSize,extended:true}));
|
|
|
|
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get("/auth/login",auth.login,errorHandler);
|
2015-11-11 23:11:02 +01:00
|
|
|
|
|
|
|
if (settings.adminAuth) {
|
|
|
|
//TODO: all passport references ought to be in ./auth
|
|
|
|
adminApp.use(passport.initialize());
|
|
|
|
adminApp.post("/auth/token",
|
|
|
|
auth.ensureClientSecret,
|
|
|
|
auth.authenticateClient,
|
|
|
|
auth.getToken,
|
|
|
|
auth.errorHandler
|
|
|
|
);
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.post("/auth/revoke",needsPermission(""),auth.revoke,errorHandler);
|
2015-11-11 23:11:02 +01:00
|
|
|
}
|
2016-05-31 15:55:03 +02:00
|
|
|
if (settings.httpAdminCors) {
|
|
|
|
var corsHandler = cors(settings.httpAdminCors);
|
|
|
|
adminApp.use(corsHandler);
|
|
|
|
}
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
// Flows
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get("/flows",needsPermission("flows.read"),flows.get,errorHandler);
|
|
|
|
adminApp.post("/flows",needsPermission("flows.write"),flows.post,errorHandler);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get("/flow/:id",needsPermission("flows.read"),flow.get,errorHandler);
|
|
|
|
adminApp.post("/flow",needsPermission("flows.write"),flow.post,errorHandler);
|
|
|
|
adminApp.delete("/flow/:id",needsPermission("flows.write"),flow.delete,errorHandler);
|
|
|
|
adminApp.put("/flow/:id",needsPermission("flows.write"),flow.put,errorHandler);
|
2015-12-06 23:49:51 +01:00
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
// Nodes
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll,errorHandler);
|
|
|
|
adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post,errorHandler);
|
2014-11-06 11:00:25 +01:00
|
|
|
|
2016-05-17 22:56:03 +02:00
|
|
|
adminApp.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.read"),nodes.getModule,errorHandler);
|
|
|
|
adminApp.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.write"),nodes.putModule,errorHandler);
|
|
|
|
adminApp.delete(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.write"),nodes.delete,errorHandler);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2016-05-17 22:56:03 +02:00
|
|
|
adminApp.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,needsPermission("nodes.read"),nodes.getSet,errorHandler);
|
|
|
|
adminApp.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,needsPermission("nodes.write"),nodes.putSet,errorHandler);
|
2014-11-05 23:41:05 +01:00
|
|
|
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,errorHandler);
|
2015-11-04 12:13:02 +01:00
|
|
|
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get(/locales\/(.+)\/?$/,locales.get,errorHandler);
|
2015-04-26 22:28:16 +02:00
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
// Library
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.post(new RegExp("/library/flows\/(.*)"),needsPermission("library.write"),library.post,errorHandler);
|
|
|
|
adminApp.get("/library/flows",needsPermission("library.read"),library.getAll,errorHandler);
|
|
|
|
adminApp.get(new RegExp("/library/flows\/(.*)"),needsPermission("library.read"),library.get,errorHandler);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
// Settings
|
2015-12-29 23:19:32 +01:00
|
|
|
adminApp.get("/settings",needsPermission("settings.read"),info.settings,errorHandler);
|
2015-07-08 18:07:14 +02:00
|
|
|
|
2015-11-11 23:11:02 +01:00
|
|
|
// Error Handler
|
2015-12-29 23:19:32 +01:00
|
|
|
//adminApp.use(errorHandler);
|
2015-11-11 23:11:02 +01:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2015-11-12 10:03:03 +01:00
|
|
|
function start() {
|
2015-11-22 00:12:39 +01:00
|
|
|
return i18n.registerMessageCatalog("editor",path.resolve(path.join(__dirname,"locales")),"editor.json").then(function(){
|
|
|
|
comms.start();
|
|
|
|
});
|
2015-11-12 10:03:03 +01:00
|
|
|
}
|
|
|
|
function stop() {
|
|
|
|
comms.stop();
|
|
|
|
return when.resolve();
|
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
module.exports = {
|
2015-11-11 23:11:02 +01:00
|
|
|
init: init,
|
2015-11-12 10:03:03 +01:00
|
|
|
start: start,
|
|
|
|
stop: stop,
|
2015-11-11 23:11:02 +01:00
|
|
|
library: {
|
|
|
|
register: library.register
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
needsPermission: auth.needsPermission
|
|
|
|
},
|
2015-11-12 10:03:03 +01:00
|
|
|
comms: {
|
|
|
|
publish: comms.publish
|
|
|
|
},
|
2015-11-24 23:38:42 +01:00
|
|
|
get adminApp() { return adminApp; },
|
|
|
|
get nodeApp() { return nodeApp; },
|
|
|
|
get server() { return server; }
|
2014-11-06 11:00:25 +01:00
|
|
|
};
|