1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/api/editor/index.js

126 lines
4.8 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("../../util").log; // TODO: separate module
var i18n = require("../../util").i18n; // TODO: separate module
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);
}
});
}
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);
var theme = require("./theme");
2018-04-20 21:50:20 +02:00
theme.init(settings);
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);
2018-01-16 12:25:13 +01:00
editorApp.get('/locales/nodes',locales.getAllNodes,apiUtil.errorHandler);
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/flows",needsPermission("library.read"),library.getAll,apiUtil.errorHandler);
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
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
return editorApp;
}
},
start: function() {
var catalogPath = path.resolve(path.join(__dirname,"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
}