node-red/packages/node_modules/@node-red/editor-api/lib/editor/index.js

134 lines
5.1 KiB
JavaScript
Raw Normal View History

/**
* 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");
var auth = require("../auth");
2018-01-16 12:21:54 +01:00
var nodes = require("../admin/nodes"); // TODO: move /icons into here
var needsPermission;
2018-04-15 12:18:10 +02:00
var runtimeAPI;
var log = require("@node-red/util").log;
var i18n = require("@node-red/util").i18n;
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()
}
})
}
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;
needsPermission = auth.needsPermission;
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);
var ui = require("./ui");
2018-04-20 21:50:20 +02:00
ui.init(runtimeAPI);
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);
}
});
}
var defaultServerSettings = {
"x-powered-by": false
}
var serverSettings = Object.assign({},defaultServerSettings,settings.httpServerOptions||{});
for (var eOption in serverSettings) {
editorApp.set(eOption, serverSettings[eOption]);
}
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);
editorApp.get("/icons/:module/:icon",ui.icon);
editorApp.get("/icons/:scope/:module/:icon",ui.icon);
editorApp.get(/^\/resources\/((?:@[^\/]+\/)?[^\/]+)\/(.+)$/,ui.moduleResource);
var theme = require("./theme");
2021-01-26 14:35:40 +01:00
theme.init(settings, runtimeAPI);
editorApp.use("/theme",theme.app());
editorApp.use("/",ui.editorResources);
2017-09-20 11:30:07 +02:00
//Projects
var projects = require("./projects");
2018-04-20 21:50:20 +02:00
projects.init(runtimeAPI);
editorApp.use("/projects",projects.app());
// Locales
var locales = require("./locales");
2018-04-19 21:46:01 +02:00
locales.init(runtimeAPI);
editorApp.get(/^\/locales\/(.+)\/?$/,locales.get,apiUtil.errorHandler);
// Library
var library = require("./library");
2018-04-20 21:50:20 +02:00
library.init(runtimeAPI);
// editorApp.get("/library/:id",needsPermission("library.read"),library.getLibraryConfig);
editorApp.get(/^\/library\/([^\/]+)\/([^\/]+)(?:$|\/(.*))/,needsPermission("library.read"),library.getEntry);
editorApp.post(/^\/library\/([^\/]+)\/([^\/]+)\/(.*)/,needsPermission("library.write"),library.saveEntry);
// Credentials
var credentials = require("./credentials");
2018-04-15 12:18:10 +02:00
credentials.init(runtimeAPI);
editorApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,apiUtil.errorHandler);
2017-12-04 18:15:17 +01:00
// Settings
// Main /settings route is an admin route - see lib/admin/settings.js
2017-12-04 18:15:17 +01:00
// 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
return editorApp;
}
},
start: function() {
var catalogPath = path.resolve(path.join(path.dirname(require.resolve("@node-red/editor-client")),"locales"));
return i18n.registerMessageCatalogs([
{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
}