66*/
77
88'use strict' ;
9- // for express
9+
1010var express = require ( 'express' ) ,
1111 bodyParser = require ( 'body-parser' ) ,
1212 cons = require ( 'consolidate' ) ,
@@ -25,21 +25,21 @@ var server = http.createServer(app);
2525var wflib , rcl ;
2626var plugins = [ ] ;
2727var Engine = require ( '../engine2' ) ;
28- var engine = { } ; // engine.i contains the engine object for workflow instance 'i'
28+ var engine = { } ; // engine[i] contains the engine object for workflow instance 'i'
2929//var $ = require('jquery');
3030
3131// global data
3232var contentType = 'text/html' ;
33- //var baseUrl = 'http://localhost:'+process.env.PORT;
34- var baseUrl = '' ; // with empty baseUrl all links are relative; I couldn't get hostname to be rendered properly in htmls
3533// parse application/x-www-form-urlencoded
3634app . use ( bodyParser . urlencoded ( { extended : false } ) )
3735// parse application/json
3836app . use ( bodyParser . json ( ) )
3937app . disable ( 'strict routing' ) ;
38+
4039/////////////////////////////////////////////////////////////////
4140//// REST API for HyperFlow workflows ////
4241/////////////////////////////////////////////////////////////////
42+
4343// returns a list of all workflow instances (aka 'apps')
4444app . get ( '/apps' , function ( req , res ) {
4545 var renderHTML = function ( ) {
@@ -58,10 +58,11 @@ app.get('/apps', function(req, res) {
5858 'application/json' : renderJSON
5959 } ) ;
6060} ) ;
61+
6162// creates a new workflow instance ('app')
6263// body can be:
63- // - a valid workflow description in JSON
64- // - or a complete workflow directory packed as zip
64+ // - or a complete workflow directory packed as zip (TODO)
65+ // - docopt JSON (from 'hflow run' command) ( workflow directory must be available locally)
6566// FIXME: validate workflow description
6667// FIXME: add proper/more detailed error info instead of "badRequest(res)"
6768app . post ( '/apps' , function ( req , res ) {
@@ -129,6 +130,7 @@ app.post('/apps', function (req, res) {
129130 zip . extractAllTo ( wfDir ) ;
130131 // Make sure this works correctly both when the zip contains a directory, or just files
131132 process . chdir ( wfDir ) ;
133+
132134 var files = fs . readdirSync ( wfDir ) ;
133135 if ( files . length == 1 ) {
134136 var fstats = fs . lstatSync ( files [ 0 ] ) ;
@@ -137,7 +139,9 @@ app.post('/apps', function (req, res) {
137139 process . chdir ( wfDir ) ;
138140 }
139141 }
142+
140143 wffile = pathtool . join ( wfDir , "workflow.json" ) ;
144+
141145 //onsole.log("WF FILE:", wffile);
142146 // if there is a "package.json" file, install dependencies (npm install -d)
143147 // TODO: improve error checking etc.
@@ -183,7 +187,8 @@ app.post('/apps', function (req, res) {
183187 // locally -- we simply run it!
184188 if ( ctype == "application/json" ) {
185189 let opts = req . body ;
186- hflowRun ( opts , function ( engine ) {
190+ hflowRun ( opts , function ( engineInstance , appId , wfName ) {
191+ engine [ appId ] = engineInstance ;
187192 res . status ( 201 ) . send ( null ) ;
188193 } ) ;
189194 return ;
@@ -386,8 +391,8 @@ app.put('/apps/:i/sigs/:name/remotesinks', function(req, res) {
386391////////////////////////////////////////////////////////////////////////
387392//// REST API (END) /////
388393////////////////////////////////////////////////////////////////////////
389- /* validate user (from db) via HTTP Basic Auth */
390394
395+ /* validate user (from db) via HTTP Basic Auth */
391396function validateUser ( req , res , next ) {
392397 var parts , auth , scheme , credentials ;
393398 var view , options ;
@@ -444,49 +449,6 @@ function acceptsXml(req) {
444449 return ctype ;
445450}
446451
447- /* compute the current date/time as a simple date */
448- function today ( ) {
449- var y , m , d , dt ;
450- dt = new Date ( ) ;
451- y = String ( dt . getFullYear ( ) ) ;
452- m = String ( dt . getMonth ( ) + 1 ) ;
453- if ( m . length === 1 ) {
454- m = '0' + m ;
455- }
456- d = String ( dt . getDate ( ) ) ;
457- if ( d . length === 1 ) {
458- d = '0' + d . toString ( ) ;
459- }
460- return y + '-' + m + '-' + d ;
461- }
462-
463- /* compute the current date/time */
464- function now ( ) {
465- var y , m , d , h , i , s , dt ;
466- dt = new Date ( ) ;
467- y = String ( dt . getFullYear ( ) ) ;
468- m = String ( dt . getMonth ( ) + 1 ) ;
469- if ( m . length === 1 ) {
470- m = '0' + m ;
471- }
472- d = String ( dt . getDate ( ) ) ;
473- if ( d . length === 1 ) {
474- d = '0' + d . toString ( ) ;
475- }
476- h = String ( dt . getHours ( ) + 1 ) ;
477- if ( h . length === 1 ) {
478- h = '0' + h ;
479- }
480- i = String ( dt . getMinutes ( ) + 1 ) ;
481- if ( i . length === 1 ) {
482- i = '0' + i ;
483- }
484- s = String ( dt . getSeconds ( ) + 1 ) ;
485- if ( s . length === 1 ) {
486- s = '0' + s ;
487- }
488- return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s ;
489- }
490452
491453/* return standard 403 response */
492454function forbidden ( res ) {
0 commit comments