var process = require('process')
var restify = require('restify')
var request = require('request')
var qs = require('querystring')
var _ = { defaults: require('lodash/object/defaults') }
;require('dotenv').load({silent: true})
;require('checkenv').check()
var convert = {
  query: require('./query'),
  response: require('./response')
}

var DEBUG = process.env.DEBUG || false
var PORT = process.env.PORT || 8080
var PROXY_TO = process.env.PROXY_TO.replace(/\/$/, '') // remove trailing slash
var DEFAULT_PARAMS = process.env.DEFAULT_PARAMS ? qs.parse(process.env.DEFAULT_PARAMS) : null

var server = restify.createServer({ name: require('./package.json').name })
server.use(restify.queryParser())

// Catch all requests
server.get(/^\/(.*)/, function (req, res, next) {
  // Construct url
  var convertedQuery = _.defaults(convert.query(req.query), DEFAULT_PARAMS)
  var path = req.params[0].replace(/\/$/, '').replace(/query$/, '') // remove trailing slash and trailing /query
  var url = PROXY_TO + '/' + path + '/query'

  if (DEBUG) console.log(url, convertedQuery)

  // Pass along converted request
  request({
    url: url,
    qs: convertedQuery,
    json: true
  }, function (err, response, body) {
    if (err) {
      console.error('Error making request', response)
    } else {
      // Give the response back
      res.send(response.statusCode, convert.response(body))
    }
  })
})

server.listen(PORT, function () {
  console.log('%s listening at %s', server.name, server.url)
})

@@ -1322,16 +1322,155 @@
 k%0A      
+if (body.error) %7B%0A        body.error.error = true  // from soda2 spec%0A        res.send(body.error.code, body.error)%0A      %7D else %7B%0A        
 res.send
@@ -1473,21 +1473,16 @@
 send(res
-ponse
 .statusC
@@ -1510,16 +1510,24 @@
 (body))%0A
+      %7D%0A
     %7D%0A  

Handle http errors properly. Closes #15