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/index.js

153 lines
5.0 KiB
JavaScript
Raw Normal View History

2014-11-04 12:34:49 +01:00
/**
* Copyright 2014 IBM Corp.
*
* 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');
var when = require('when');
2014-11-04 12:34:49 +01:00
var ui = require("./ui");
var nodes = require("./nodes");
var flows = require("./flows");
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");
var credentials = require("./credentials");
var comms = require("./comms");
2014-11-06 23:59:48 +01:00
var auth = require("./auth");
var needsPermission = auth.needsPermission;
2014-11-04 12:34:49 +01:00
var log;
var adminApp;
var nodeApp;
2014-11-04 12:34:49 +01:00
var errorHandler = function(err,req,res,next) {
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-04 12:34:49 +01:00
function init(server,runtime) {
var settings = runtime.settings;
log = runtime.log;
if (settings.httpNodeRoot !== false) {
nodeApp = express();
}
if (settings.httpAdminRoot !== false) {
comms.init(server,runtime);
adminApp = express();
auth.init(runtime);
credentials.init(runtime);
flows.init(runtime);
info.init(runtime);
library.init(adminApp,runtime);
locales.init(runtime);
nodes.init(runtime);
// Editor
if (!settings.disableEditor) {
ui.init(runtime);
var editorApp = express();
editorApp.get("/",ui.ensureSlash,ui.editor);
editorApp.get("/icons/:icon",ui.icon);
if (settings.editorTheme) {
editorApp.use("/theme",theme.init(runtime));
}
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}));
adminApp.get("/auth/login",auth.login);
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
);
adminApp.post("/auth/revoke",needsPermission(""),auth.revoke);
}
2014-11-06 23:59:48 +01:00
// Flows
adminApp.get("/flows",needsPermission("flows.read"),flows.get);
adminApp.post("/flows",needsPermission("flows.write"),flows.post);
// Nodes
adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll);
adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post);
adminApp.get("/nodes/:mod",needsPermission("nodes.read"),nodes.getModule);
adminApp.put("/nodes/:mod",needsPermission("nodes.write"),nodes.putModule);
adminApp.delete("/nodes/:mod",needsPermission("nodes.write"),nodes.delete);
adminApp.get("/nodes/:mod/:set",needsPermission("nodes.read"),nodes.getSet);
adminApp.put("/nodes/:mod/:set",needsPermission("nodes.write"),nodes.putSet);
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get);
adminApp.get(/locales\/(.+)\/?$/,locales.get);
2015-04-26 22:28:16 +02:00
// Library
adminApp.post(new RegExp("/library/flows\/(.*)"),needsPermission("library.write"),library.post);
adminApp.get("/library/flows",needsPermission("library.read"),library.getAll);
adminApp.get(new RegExp("/library/flows\/(.*)"),needsPermission("library.read"),library.get);
// Settings
adminApp.get("/settings",needsPermission("settings.read"),info.settings);
// Error Handler
adminApp.use(errorHandler);
}
2014-11-04 12:34:49 +01:00
}
function start() {
comms.start();
return when.resolve();
}
function stop() {
comms.stop();
return when.resolve();
}
2014-11-04 12:34:49 +01:00
module.exports = {
init: init,
start: start,
stop: stop,
library: {
register: library.register
},
auth: {
needsPermission: auth.needsPermission
},
comms: {
publish: comms.publish
},
adminApp: function() { return adminApp; },
nodeApp: function() { return nodeApp; }
};