var url = require('url')
var fs = require('fs');
var express = require('express');
var _  = require('lodash');
var superagent = require('superagent');
var net = require('net');
var http = require('http');
var WS = require('ws');

var WebSocketServer = WS.Server;
var host = process.env.HOST;
var port;
var indexData;
var app = express();
var ms = process.env.MS;


app.use(express.static('dist'));
  
var server = app.listen(process.env.PORT || 3000, function () {
  port = server.address().port;
  process.env.HOST = host;
  process.env.PORT = port;
    indexData = _.template(fs.readFileSync('index.tpl'))(process.env);

});

app.get('/', function(req, res) {
  res.send(indexData);
});

  var wss = new WebSocketServer({server: server});
  
  app.get('/apis/*', function(req, response) {
      var path = req.params[0];
      var jsonData={};

        var options = {
        socketPath: "/var/run/docker.sock",
        path: ('/' + path),
        method: 'GET'
      };
    var req = http.request(options, (res) => {
      var data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        jsonData['objects'] = JSON.parse(data.toString());
        response.json(jsonData);
      });
    });
    req.on('error', (e) => {
      console.log(`problem with request: ${e.message}`);
      console.log(e.stack);
    });
      req.end();

  });

// wss.on('connection',function connection(ws){
//     //   var jsonData={};
//     //   var options = {
//     //     socketPath: "/var/run/docker.sock",
//     //     path: ('/events'),
//     //     method: 'GET'
//     //   };
//     // var req = http.request(options, (res) => {
//     //   res.on('data', (chunk) => {
//     //     ws.send(chunk.toString());
//     //   });
//     //   res.on('end', () => {
//     //     ws.json(jsonData);
//     //   });
//     // });
//     // req.on('error', (e) => {
//     //   console.log(`problem with request: ${e.message}`);
//     //   console.log(e.stack);
//     // });
//     //   req.end();
//     while(true)
//     {setTimeout(function() {
//       ws.send('message');
//     }, 5000 );}
//   });

@@ -683,16 +683,262 @@
 );%0A%7D);%0A%0A
+console.log(process.env.DOCKER_HOST)%0A%0A  if(process.env.DOCKER_HOST) %7B%0A     try %7B%0A%09   dh = process.env.DOCKER_HOST.split(%22:%22);%0A%09   var docker_host = dh%5B0%5D; %0A%09   var docker_port = dh%5B1%5D;%0A     %7D %0A%09 catch (err) %7B%0A%09   console.log(err.stack)%0A     %7D%0A%09%7D%0A
   var ws
@@ -1085,19 +1085,16 @@
 ata=%7B%7D;%0A
-%0A  
       va
@@ -1111,110 +1111,220 @@
 = %7B%0A
-        socketPath: %22/var/run/docker.sock%22,%0A        path: ('/' + path),%0A        method: 'GET'%0A 
+%09%09  path: ('/' + path),%0A%09%09  method: 'GET'%0A%09  %7D%0A%0A    if(docker_host) %7B%0A        options.host = docker_host;%0A%09%09    options.port = docker_port;%0A%09  %7D%0A%09  else %7B%0A%09%09    options.socketPath = '/var/run/docker.sock';%0A
     
- %7D;
+%7D%0A
 %0A   

Add TCP daemon support.