#!/usr/bin/env node
"use strict";

require("babel-register");

const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const express = require("express");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const http = require("http");
const _ = require("lodash");

// Setup Config
const getConfig = require("../config/config").getConfig;
const feature = require("../config/feature");
const config = getConfig();
feature.setConfig(config);

if (!feature.isEnabled("firefox.webSocketConnection")) {
  const firefoxProxy = require("./firefox-proxy");
  firefoxProxy({ logging: feature.isEnabled("logging.firefoxProxy") });
}

function httpGet(url, onResponse) {
  return http.get(url, (response) => {
    let body = '';
    response.on('data', function(d) {
      body += d;
    });
    response.on('end', function() {
      onResponse(body);
    });
  });
}

const app = express();

// Webpack middleware
const webpackConfig = require("../webpack.config");
const compiler = webpack(webpackConfig);

app.use(webpackDevMiddleware(compiler, {
  publicPath: webpackConfig.output.publicPath,
  noInfo: true,
  stats: { colors: true }
}));

if(feature.isEnabled("hotReloading")) {
  app.use(webpackHotMiddleware(compiler));
}

// Static middleware

app.use(express.static("public/js/test/examples"));
app.use(express.static("public"));

// Routes
app.get("/", function(req, res) {
  res.sendFile(path.join(__dirname, "../index.html"));
});

app.get("/get", function(req, res) {
  const httpReq = httpGet(
    req.query.url,
    body => res.json(JSON.parse(body))
  );

  httpReq.on('error', function (err) {
    res.status(500).send(err.code);
  });
})

// Listen
app.listen(8000, "localhost", function(err, result) {
  if (err) {
    console.log(err);
  }

  console.log("Development Server Listening at http://localhost:8000");
});

@@ -90,34 +90,8 @@
 %22);%0A
-const fs = require(%22fs%22);%0A
 cons
@@ -319,37 +319,8 @@
 p%22);
-%0Aconst _ = require(%22lodash%22);
 %0A%0A//
@@ -769,10 +769,10 @@
 y = 
-''
+%22%22
 ;%0A  
@@ -789,14 +789,14 @@
 .on(
-'
+%22
 data
-'
+%22
 , fu
@@ -852,33 +852,20 @@
 .on(
-'end', function() %7B%0A     
+%22end%22, () =%3E
  onR
@@ -877,23 +877,16 @@
 se(body)
-;%0A    %7D
 );%0A  %7D);
@@ -1167,16 +1167,17 @@
 %7D));%0A%0Aif
+ 
 (feature
@@ -1250,16 +1250,138 @@
 ler));%0A%7D
+ else %7B%0A  console.log(%22Hot Reloading can be enabled by adding %22 +%0A  %22%5C%22hotReloading%5C%22: true to your local.json config%22);%0A%7D
 %0A%0A// Sta
@@ -1395,17 +1395,16 @@
 dleware%0A
-%0A
 app.use(
@@ -1732,37 +1732,23 @@
 .on(
-'
+%22
 error
-', function (err) %7B%0A   
+%22, err =%3E
  res
@@ -1778,18 +1778,14 @@
 ode)
-;%0A  %7D
 );%0A%7D)
+;
 %0A%0A//
@@ -1810,25 +1810,23 @@
 (8000, %22
-localhost
+0.0.0.0
 %22, funct

allow development server to be publicly reachable