2017-08-22 23:26:29 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* 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");
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var comms = require("./comms");
|
|
|
|
var library = require("./library");
|
2017-12-04 18:15:17 +01:00
|
|
|
var info = require("./settings");
|
2017-08-22 23:26:29 +02:00
|
|
|
|
|
|
|
var auth = require("../auth");
|
2018-01-16 12:21:54 +01:00
|
|
|
var nodes = require("../admin/nodes"); // TODO: move /icons into here
|
2018-01-16 17:18:18 +01:00
|
|
|
var needsPermission;
|
2018-04-15 12:18:10 +02:00
|
|
|
var runtimeAPI;
|
2018-04-19 12:23:08 +02:00
|
|
|
var log = require("../../util").log; // TODO: separate module
|
|
|
|
var i18n = require("../../util").i18n; // TODO: separate module
|
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
var apiUtil = require("../util");
|
|
|
|
|
|
|
|
var ensureRuntimeStarted = function(req,res,next) {
|
2018-04-23 12:21:02 +02:00
|
|
|
runtimeAPI.isStarted().then( started => {
|
|
|
|
if (!started) {
|
|
|
|
log.error("Node-RED runtime not started");
|
|
|
|
res.status(503).send("Not started");
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
2017-08-22 23:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-04-23 12:21:02 +02:00
|
|
|
init: function(server, settings, _runtimeAPI) {
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI = _runtimeAPI;
|
2018-01-16 17:18:18 +01:00
|
|
|
needsPermission = auth.needsPermission;
|
2017-08-22 23:26:29 +02:00
|
|
|
if (!settings.disableEditor) {
|
2018-04-15 12:18:10 +02:00
|
|
|
info.init(runtimeAPI);
|
2018-04-23 12:21:02 +02:00
|
|
|
comms.init(server,settings,runtimeAPI);
|
2017-08-22 23:26:29 +02:00
|
|
|
|
|
|
|
var ui = require("./ui");
|
2018-04-19 12:23:08 +02:00
|
|
|
|
2018-04-20 21:50:20 +02:00
|
|
|
ui.init(runtimeAPI);
|
2018-04-19 12:23:08 +02:00
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
var editorApp = express();
|
|
|
|
if (settings.requireHttps === true) {
|
|
|
|
editorApp.enable('trust proxy');
|
|
|
|
editorApp.use(function (req, res, next) {
|
|
|
|
if (req.secure) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
res.redirect('https://' + req.headers.host + req.originalUrl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
editorApp.get("/",ensureRuntimeStarted,ui.ensureSlash,ui.editor);
|
2018-01-16 12:21:54 +01:00
|
|
|
|
2018-01-16 12:25:13 +01:00
|
|
|
editorApp.get("/icons",needsPermission("nodes.read"),nodes.getIcons,apiUtil.errorHandler);
|
2017-08-22 23:26:29 +02:00
|
|
|
editorApp.get("/icons/:module/:icon",ui.icon);
|
|
|
|
editorApp.get("/icons/:scope/:module/:icon",ui.icon);
|
|
|
|
|
|
|
|
var theme = require("./theme");
|
2018-04-20 21:50:20 +02:00
|
|
|
theme.init(settings);
|
2017-08-22 23:26:29 +02:00
|
|
|
editorApp.use("/theme",theme.app());
|
|
|
|
editorApp.use("/",ui.editorResources);
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
//Projects
|
2017-12-17 00:43:08 +01:00
|
|
|
var projects = require("./projects");
|
2018-04-20 21:50:20 +02:00
|
|
|
projects.init(runtimeAPI);
|
2017-12-17 00:43:08 +01:00
|
|
|
editorApp.use("/projects",projects.app());
|
2017-08-22 23:26:29 +02:00
|
|
|
|
|
|
|
// Locales
|
|
|
|
var locales = require("./locales");
|
2018-04-19 21:46:01 +02:00
|
|
|
locales.init(runtimeAPI);
|
2017-08-22 23:26:29 +02:00
|
|
|
editorApp.get(/locales\/(.+)\/?$/,locales.get,apiUtil.errorHandler);
|
|
|
|
|
|
|
|
// Library
|
|
|
|
var library = require("./library");
|
2018-04-20 21:50:20 +02:00
|
|
|
library.init(runtimeAPI);
|
2017-08-22 23:26:29 +02:00
|
|
|
editorApp.get("/library/flows",needsPermission("library.read"),library.getAll,apiUtil.errorHandler);
|
2018-04-18 18:09:31 +02:00
|
|
|
editorApp.get(/library\/([^\/]+)(?:$|\/(.*))/,needsPermission("library.read"),library.getEntry);
|
|
|
|
editorApp.post(/library\/([^\/]+)\/(.*)/,needsPermission("library.write"),library.saveEntry);
|
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
|
|
|
|
// Credentials
|
|
|
|
var credentials = require("./credentials");
|
2018-04-15 12:18:10 +02:00
|
|
|
credentials.init(runtimeAPI);
|
2017-08-22 23:26:29 +02:00
|
|
|
editorApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,apiUtil.errorHandler);
|
|
|
|
|
2017-12-04 18:15:17 +01:00
|
|
|
// Settings
|
|
|
|
editorApp.get("/settings",needsPermission("settings.read"),info.runtimeSettings,apiUtil.errorHandler);
|
|
|
|
// User Settings
|
|
|
|
editorApp.get("/settings/user",needsPermission("settings.read"),info.userSettings,apiUtil.errorHandler);
|
|
|
|
// User Settings
|
|
|
|
editorApp.post("/settings/user",needsPermission("settings.write"),info.updateUserSettings,apiUtil.errorHandler);
|
2017-12-07 15:11:24 +01:00
|
|
|
// SSH keys
|
2018-04-24 16:01:49 +02:00
|
|
|
editorApp.use("/settings/user/keys",needsPermission("settings.write"),info.sshkeys());
|
2017-12-04 18:15:17 +01:00
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
return editorApp;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
var catalogPath = path.resolve(path.join(__dirname,"locales"));
|
2018-04-19 12:23:08 +02:00
|
|
|
return i18n.registerMessageCatalogs([
|
2017-08-22 23:26:29 +02:00
|
|
|
{namespace: "editor", dir: catalogPath, file:"editor.json"},
|
|
|
|
{namespace: "jsonata", dir: catalogPath, file:"jsonata.json"},
|
|
|
|
{namespace: "infotips", dir: catalogPath, file:"infotips.json"}
|
|
|
|
]).then(function(){
|
|
|
|
comms.start();
|
|
|
|
});
|
|
|
|
},
|
2018-04-23 12:21:02 +02:00
|
|
|
stop: comms.stop
|
2017-08-22 23:26:29 +02:00
|
|
|
}
|