const github = require('./lib/github')
const view = require('./lib/view')
const fail = require('./lib/fail')
const webdevquiz = require('./lib/webdevquiz')

var feeds

// Feed cache
require('./lib/feeds')
  .then(res => feeds = res)

module.exports = {
  '/api/github/xhr/:command': context => new Promise((res, rej) => {
    if (github.xhr[context.params.command]) {
      github.xhr[context.params.command]()
        .then(json => {
          context.body = json
          res()
        })
        .catch(err => fail(err))
    } else {
      rej()
    }
  }),
  '/api/view/xhr/:name': context => new Promise((res, rej) => {
    view.xhr(context.params.name)
      .then(html => {
        context.response = html
        context.response.type = 'text/plain'
        res()
      })
      .catch(e => {
        console.error(e)
        rej()
      })
  }),
  '/feed/:type': context => new Promise((res, rej) => {
    if (context.params.type === 'atom.xml') {
      context.body = feeds[0]
      res()
    } else if (context.params.type === 'rss.xml') {
      context.body = feeds[1]
      res()
    } else {
      rej()
    }
  }),
  '/webdevquiz': context => new Promise ((res, rej) => {
    webdevquiz(context)
    res()
  }),
  '/:p': context => new Promise((res, rej) => {
    // TODO Render 404
    context.response.status = 404
    res()
  })
}


@@ -1217,24 +1217,178 @@
 res()%0A  %7D),%0A
+  '/ip': context =%3E new Promise((res, rej) =%3E %7B%0A    context.response.status = 200%0A    context.body = context.req.connection.remoteAddress%0A    res()%0A  %7D),%0A
   '/:p': con

Add /ip