var koa = require("koa"),
    bodyparser = require("koa-body"),
    gzip = require("koa-gzip"),
    mount = require("koa-mount"),
    // views = require("koa-views"),
    crypto = require("crypto"),
    ejs = require("koa-ejs"),
    statics = require("koa-static"),
    path = require("path"),
    hc = require("hc"),
    co = require("co"),
    session = require("koa-sess"),
    methodOverride = require("koa-methodoverride"),
    thunkify = require("thunkify");



var getConfigURL = function() {
  var configURL = {
    development: "http://code.elfvision.com/snippets/8/raw",
    test: "",
    production: ""
  };

  var env = process.env.NODE_ENV || "development";
  
  return configURL[env] + "?private_token=" + process.env.PRIVATE_TOKEN;
};


co(function*() {
  
  var conf = yield thunkify(hc)({//load config
    source: new hc.sources.HTTP(getConfigURL())
  });
  
  var app = koa();
  
  var routes = require("./routes"),
      passport = require("./utils/passport");

  // generate rotation keys for sign
  app.keys = (function(i) {
    var ret = [];
    while(i--) {
      ret.push(crypto.randomBytes(256).toString());
    }
    return ret;
  }(3));
  app.use(gzip());
  app.use(bodyparser());
  app.use(methodOverride());
  app.use(session({
    store: new (require("koa-redis"))(conf.redis)
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  
  
  ejs(app, {
    root: path.join(__dirname, "./views"),
    layout: "layouts/basic",
    viewExt: "ejs",
    cache: process.env.NODE_ENV === "production",
    locals: require("./utils/view_helpers")
  });

  app.use(mount("/live", routes.live.middleware()));
  app.use(routes.home.middleware());
  app.use(routes.user.middleware());
  app.use(routes.spec.middleware());
  app.use(routes.sandbox.middleware());
  app.use(routes.repo.middleware());
  
  app.use(statics(path.join(__dirname, "../assets/public")));
  app.on("error", function(e) {
    console.log(e.stack);
  });
  
  var srv = app.listen(process.env.PORT || 9090, function() {
    var address = srv.address();
    console.log("WebSpec server is up and runnign at " + address.port);
  });

})();
@@ -460,17 +460,23 @@
 fy%22);%0A%0A%0A
-%0A
+// %0A// 
 var getC
@@ -499,16 +499,19 @@
 ion() %7B%0A
+// 
   var co
@@ -522,20 +522,23 @@
 URL = %7B%0A
+//
     
+ 
 developm
@@ -586,16 +586,19 @@
 8/raw%22,%0A
+// 
     test
@@ -603,20 +603,23 @@
 st: %22%22,%0A
+//
     
+ 
 producti
@@ -629,14 +629,23 @@
  %22%22%0A
+// 
   %7D;%0A
-%0A
+// %0A// 
   va
@@ -691,19 +691,25 @@
 pment%22;%0A
-  %0A
+//   %0A// 
   return
@@ -777,12 +777,21 @@
 EN;%0A
-%7D;%0A%0A
+// %7D;%0A// %0A// 
 %0Aco(
@@ -806,22 +806,63 @@
 *() %7B%0A  
-%0A  
 var
+ conf;%0A  %0A  if(process.env.CONFIG_URL) %7B%0A   
  conf = 
@@ -899,16 +899,18 @@
 fig%0A    
+  
 source: 
@@ -933,29 +933,85 @@
 TTP(
-getConfigURL())%0A  %7D);
+process.env.CONFIG_URL)%0A    %7D);%0A  %7D else %7B%0A    conf = yield thunkify(hc);%0A  %7D
 %0A  %0A

fix url parsing