// var http = require('http2');
var http = require('http');
var express = require('express');
var sessions = require('client-sessions');
var WebSocketServer = require('ws').Server;
var Flow = require('./flow/flow');

// require and init core module
Flow(require('./core_server'));

module.exports = function (config) {

    var app = express();
    var server = http.createServer(/*config.ssl, */app);
    var wss = new WebSocketServer({server: server});
    var clientSession = sessions(config.session);

    // use encrypted client sessions
    app.use(clientSession);

    // emit flow events from http urls
    app.use('/:instance::event', function (req, res, next) {

        var stream = Flow.emit(req.params.instance + '/' + req.params.event, {req: req, res: res});

        // setup stream
        //stream.end = res.end;
        //stream.data(res.send);
        //stream.error(function (err, status) {res.status(status); res.send(err)});

        req.on('data', function (chunk) {
            stream.write(null, chunk);
        });
        req.on('error', function (chunk) {
            stream.write(chunk);
        });

        res.end('Flow emit: ' + req.params.instance + '/' + req.params.event);
    });

    // serve client module bundles
    app.get('/:module/client(.:fp)?.js', function(req, res) {

        // set longer cache age, if file is fingerprinted
        res.set(
            'Cache-Control',
            'public, max-age=' + req.params.fp ? config.static.fpMaxAge : config.static.maxAge
        );

        res.sendFile(config.paths.modules + '/' + req.params.module + '/M.js');
    });

    // static file server for public files
    app.use(express.static(config.paths.public));

    // send the initial document with engine client
    app.use(function (req, res) {
        res.set('Cache-Control', 'public, max-age=' + config.static.maxAge);
        res.sendFile(config.paths.modules + '/engine/lib/document.html');
    });

    // handle websocket connections
    wss.on('connection', function connection(socket) {

        // plug client session midleware
        clientSession(socket.upgradeReq, {}, function (err) {

            // setup flow on socket
            socket.app = app;
            Flow.socket(socket);
        });
    });

    // start http server
    server.listen(config.port, function () {
        console.log('Engine is listening on port', config.port);
    });
};

@@ -178,20 +178,23 @@
 er;%0Avar 
-Flow
+adapter
  = requi
@@ -203,59 +203,34 @@
 ('./
-flow/flow');%0A%0A// require and init core module%0A
+core_server');%0Avar 
 Flow
-(
+ = 
 requ
@@ -236,29 +236,26 @@
 uire('./
-core_server')
+flow/flow'
 );%0A%0Amodu
@@ -284,24 +284,62 @@
 (config) %7B%0A%0A
+    var flow = Flow(adapter(config));%0A
     var app 
@@ -655,577 +655,19 @@
 ', f
-unction (req, res, next) %7B%0A%0A        var stream = Flow.emit(req.params.instance + '/' + req.params.event, %7Breq: req, res: res%7D);%0A%0A        // setup stream%0A        //stream.end = res.end;%0A        //stream.data(res.send);%0A        //stream.error(function (err, status) %7Bres.status(status); res.send(err)%7D);%0A%0A        req.on('data', function (chunk) %7B%0A            stream.write(null, chunk);%0A        %7D);%0A        req.on('error', function (chunk) %7B%0A            stream.write(chunk);%0A        %7D);%0A%0A        res.end('Flow emit: ' + req.params.instance + '/' + req.params.event);%0A    %7D
+low.request
 );%0A%0A
@@ -1682,27 +1682,92 @@
     
-Flow.
 socket
-(socket)
+.onmessage = function (msg) %7B%0A                flow.message(msg);%0A            %7D
 ;%0A  

move flow request adapter to core server module