<commit_before>/*
** HyperFlow engine
** Author: <NAME> (2012-2014)
**
** HyperFlow server implementing the REST API for HyperFlow workflows.
*/

'use strict';

/**
 * Module dependencies.
 */


var redisURL = process.env.REDIS_URL ? {url: process.env.REDIS_URL} : undefined;
// for express
var express = require('express'),
    cons = require('consolidate'),
    spawn = require('child_process').spawn,
    http = require('http'),
    os = require('os'),
    fs = require('fs'),
    which = require('which'),
    pathtool = require('path'),
    AdmZip = require('adm-zip'),
    app = express();

var redis = require('redis'),
    rcl = redisURL ? redis.createClient(redisURL): redis.createClient();

var server = http.createServer(app);
var wflib = require('../wflib').init(rcl);
var plugins = [];
var Engine = require('../engine2');
var engine = {}; // engine.i contains the engine object for workflow instance 'i'
var request = require('request');

var timers = require('timers');


//var $ = require('jquery');

var _ = require('underscore');

// global data
var contentType = 'text/html';
//var baseUrl = 'http://localhost:'+process.env.PORT;
var baseUrl = ''; // with empty baseUrl all links are relative; I couldn't get hostname to be rendered properly in htmls

// Configuration
app.configure(function() {
        //app.use(express.compress());
	app.engine('ejs', cons.ejs);
	app.set('views', __dirname + '/views');
	app.set('view engine', 'ejs');
	app.use(express.bodyParser({strict: false}));
	app.use(express.methodOverride());
	app.use(app.router);
	app.use(express.static(__dirname + '/public'));
	app.disable('strict routing');
});

app.configure('development', function() {
	app.use(express.errorHandler({
		dumpExceptions: true,
		showStack: true
	}));
});

app.configure('production', function() {
	app.use(express.errorHandler());
});

/////////////////////////////////////////////////////////////////
////           REST API for HyperFlow workflows              ////
/////////////////////////////////////////////////////////////////

// returns a list of all workflow instances (aka 'apps')
app.get('/apps', function(req, res) {
    var renderHTML = function() {
        var ctype = acceptsXml(req);
        res.header('content-type', ctype);
        res.send('GET /apps');
        //res.render ... TODO
    }
    var renderJSON = function() {
        res.header('content-type', 'text/plain');
        res.send('GET /apps');
        // res.render ... TODO
    }
    res.format({
        'text/html': renderHTML,
        'application/json': renderJSON
    });
});

// creates a new workflow instance ('app')
// body can be:
// - a valid workflow description in JSON
// - or a complete workflow directory packed as zip
// FIXME: validate workflow description
// FIXME: add proper/more detailed error info instead of "badRequest(res)"
app.post('/apps', function(req, res) {
    var ctype = req.headers["content-type"];

    function makeId6() {
	var id = [];
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

	for (var i=0; i<6; i++ ) {
	    id[i] = possible.charAt(Math.floor(Math.random() * possible.length));
	}

	return id.join("");
    }

    var runWorkflow = function(wfDir, appId) {
	var config = {"emulate":"false", "workdir": wfDir};
	engine[appId] = new Engine(config, wflib, appId, function(err) {
	    if (err) return badRequest(res);
		plugins.forEach(function(plugin) {
			plugin.init(rcl, wflib, engine);
		});
	    engine[appId].runInstance(function(err) {
		if (err) return badRequest(res);
		res.header('Location', req.url + '/' + appId);
		res.send(201, null);
		//res.redirect(req.url + '/' + appId, 302);
		// TODO: implement sending all input signals (just like -s flag in runwf.js)<commit_msg>Fix plugin initialization in hyperflow-server.<commit_after>/*
** HyperFlow engine
** Author: <NAME> (2012-2014)
**
** HyperFlow server implementing the REST API for HyperFlow workflows.
*/

'use strict';

/**
 * Module dependencies.
 */


var redisURL = process.env.REDIS_URL ? {url: process.env.REDIS_URL} : undefined;
// for express
var express = require('express'),
    cons = require('consolidate'),
    spawn = require('child_process').spawn,
    http = require('http'),
    os = require('os'),
    fs = require('fs'),
    which = require('which'),
    pathtool = require('path'),
    AdmZip = require('adm-zip'),
    app = express();

var redis = require('redis'),
    rcl = redisURL ? redis.createClient(redisURL): redis.createClient();

var server = http.createServer(app);
var wflib = require('../wflib').init(rcl);
var plugins = [];
var Engine = require('../engine2');
var engine = {}; // engine.i contains the engine object for workflow instance 'i'
var request = require('request');

var timers = require('timers');


//var $ = require('jquery');

var _ = require('underscore');

// global data
var contentType = 'text/html';
//var baseUrl = 'http://localhost:'+process.env.PORT;
var baseUrl = ''; // with empty baseUrl all links are relative; I couldn't get hostname to be rendered properly in htmls

// Configuration
app.configure(function() {
        //app.use(express.compress());
	app.engine('ejs', cons.ejs);
	app.set('views', __dirname + '/views');
	app.set('view engine', 'ejs');
	app.use(express.bodyParser({strict: false}));
	app.use(express.methodOverride());
	app.use(app.router);
	app.use(express.static(__dirname + '/public'));
	app.disable('strict routing');
});

app.configure('development', function() {
	app.use(express.errorHandler({
		dumpExceptions: true,
		showStack: true
	}));
});

app.configure('production', function() {
	app.use(express.errorHandler());
});

/////////////////////////////////////////////////////////////////
////           REST API for HyperFlow workflows              ////
/////////////////////////////////////////////////////////////////

// returns a list of all workflow instances (aka 'apps')
app.get('/apps', function(req, res) {
    var renderHTML = function() {
        var ctype = acceptsXml(req);
        res.header('content-type', ctype);
        res.send('GET /apps');
        //res.render ... TODO
    }
    var renderJSON = function() {
        res.header('content-type', 'text/plain');
        res.send('GET /apps');
        // res.render ... TODO
    }
    res.format({
        'text/html': renderHTML,
        'application/json': renderJSON
    });
});

// creates a new workflow instance ('app')
// body can be:
// - a valid workflow description in JSON
// - or a complete workflow directory packed as zip
// FIXME: validate workflow description
// FIXME: add proper/more detailed error info instead of "badRequest(res)"
app.post('/apps', function(req, res) {
    var ctype = req.headers["content-type"];

    function makeId6() {
	var id = [];
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

	for (var i=0; i<6; i++ ) {
	    id[i] = possible.charAt(Math.floor(Math.random() * possible.length));
	}

	return id.join("");
    }

    var runWorkflow = function(wfDir, appId) {
	var config = {"emulate":"false", "workdir": wfDir};
	engine[appId] = new Engine(config, wflib, appId, function(err) {
	    if (err) return badRequest(res);
		plugins.forEach(function(plugin) {
			plugin.init(rcl, wflib, engine[appId]);
		});
	    engine[appId].runInstance(function(err) {
		if (err) return badRequest(res);
		res.header('Location', req.url + '/' + appId);
		res.send(201, null);
		//res.redirect(req.url + '/' + appId, 302);
		// TODO: implement sending all input signals (just like -s flag in runwf.js)
{'fraction_non_alphanumeric': 0.2004754358161648, 'fraction_numerical': 0.007395668251452721, 'mean_word_length': 3.0346297282898242, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1330651', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2315, 'n_words': 589}