node-red/red/api/index.js

95 lines
2.5 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");
var util = require('util');
2014-11-06 23:59:48 +01:00
var passport = require('passport');
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");
var auth = require("./auth");
2014-11-04 12:34:49 +01:00
var settings = require("../settings");
var errorHandler = function(err,req,res,next) {
//TODO: standardize json response
res.send(400,err.toString());
};
2014-11-04 12:34:49 +01:00
function init(adminApp) {
2014-11-06 23:59:48 +01:00
var apiApp = express();
2014-11-04 12:34:49 +01:00
adminApp.use(express.json());
2014-11-06 23:59:48 +01:00
adminApp.use(express.urlencoded());
//TODO: all passport references ought to be in ./auth
apiApp.use(passport.initialize());
apiApp.use(auth.authenticate);
apiApp.post("/auth/token",
auth.ensureClientSecret,
auth.authenticateClient,
auth.getToken,
auth.errorHandler
);
2014-11-11 11:15:02 +01:00
apiApp.get("/auth/login",auth.login);
2014-11-06 23:59:48 +01:00
// Flows
apiApp.get("/flows",flows.get);
apiApp.post("/flows",flows.post);
// Nodes
apiApp.get("/nodes",nodes.getAll);
apiApp.post("/nodes",nodes.post);
2014-11-06 23:59:48 +01:00
apiApp.get("/nodes/:mod",nodes.getModule);
apiApp.put("/nodes/:mod",nodes.putModule);
apiApp.delete("/nodes/:mod",nodes.delete);
apiApp.get("/nodes/:mod/:set",nodes.getSet);
apiApp.put("/nodes/:mod/:set",nodes.putSet);
2014-11-06 23:59:48 +01:00
// Library
library.init(apiApp);
apiApp.post(new RegExp("/library/flows\/(.*)"),library.post);
apiApp.get("/library/flows",library.getAll);
apiApp.get(new RegExp("/library/flows\/(.*)"),library.get);
// Settings
apiApp.get("/settings",info.settings);
2014-11-04 12:34:49 +01:00
// Editor
if (!settings.disableEditor) {
adminApp.get("/",ui.ensureSlash);
adminApp.get("/icons/:icon",ui.icon);
adminApp.use("/",ui.editor);
}
2014-11-06 23:59:48 +01:00
adminApp.use(apiApp);
// Error Handler
2014-11-04 12:34:49 +01:00
adminApp.use(errorHandler);
}
module.exports = {
init: init
};