2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var util = require("util");
23 redApp = require("./server").app;
24 storage = require("./storage");
26 // -------- Flow Library --------
27 redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) {
29 req.on('data', function(chunk) {
30 fullBody += chunk.toString();
32 req.on('end', function() {
33 storage.saveFlow(req.params[0],fullBody).then(function() {
35 }).otherwise(function(err) {
36 util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
37 if (err.message.indexOf('forbidden') === 0) {
46 redApp.get("/library/flows",function(req,res) {
47 storage.getAllFlows().then(function(flows) {
52 redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) {
53 storage.getFlow(req.params[0]).then(function(data) {
54 res.set('Content-Type', 'application/json');
56 }).otherwise(function(err) {
58 util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
59 if (err.message.indexOf('forbidden') === 0) {
68 // ------------------------------
71 function createLibrary(type) {
73 redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
74 var path = req.params[1]||"";
75 storage.getLibraryEntry(type,path).then(function(result) {
76 if (typeof result === "string") {
77 res.writeHead(200, {'Content-Type': 'text/plain'});
83 }).otherwise(function(err) {
85 util.log("[red] Error loading library entry '"+path+"' : "+err);
86 if (err.message.indexOf('forbidden') === 0) {
95 redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
96 var path = req.params[0];
98 req.on('data', function(chunk) {
99 fullBody += chunk.toString();
101 req.on('end', function() {
102 storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
104 }).otherwise(function(err) {
105 util.log("[red] Error saving library entry '"+path+"' : "+err);
106 if (err.message.indexOf('forbidden') === 0) {
116 module.exports.init = init;
117 module.exports.register = createLibrary;