<commit_before>const httpProxy = require('http-proxy')
const Docker = require('./docker')

const proxy = httpProxy.createProxyServer()

const table = {}

module.exports = async (ctx, next) => {
  const { headers: { host }, req, res, app: { env } } = ctx
  const debug = env !== 'production'
  const xipio = /\.xip\.io$/.test(host)
  const vhost = host.replace(/\.?127\.0\.0\.1\.xip\.io$/, '')
  if(xipio && debug && vhost) {
    const [owner, repo, branch, commitId] = vhost.split('.')
    const docker = new Docker({
      owner, repo, branch, commitId
    })
    const status = await docker.getContainerStatus()
    if(status) {
      if(status === 'exited') {
        await docker.container.start()
      }
      if(!table[vhost]) {
        const inspect = await docker.container.inspect()
        const { NetworkSettings: { Ports }} = inspect
        // TODO: ensure requiring port
        const port = Object.keys(Ports).map(port => {
          // const [number] = port.split('/')
          return Ports[port][0].HostPort
        })[0]
        table[vhost] = port
      }
      const port = table[vhost]
      const target = `http://localhost:${port}`
      req.headers.host = target
      await new Promise((resolve, reject) => {
        proxy.web(req, res, {
          target
        }, e => {
          if(e) reject(e)
          resolve()
        })
      })
    } else {
      // redirect to preview url
      // ctx.redirect('')
    }
  } else {
    await next()
  }
}<commit_msg>Fix how to get virtual host<commit_after>const httpProxy = require('http-proxy')
const Docker = require('./docker')

const proxy = httpProxy.createProxyServer()

const table = {}

const { HOST, PORT } = process.env

module.exports = async (ctx, next) => {
  const { headers: { host }, req, res } = ctx
  const vhost = host.split('.').slice(0, 4)
  if(vhost.length === 4) {
    const [owner, repo, branch, commitId] = vhost  
    const docker = new Docker({
      owner, repo, branch, commitId
    })
    const status = await docker.getContainerStatus()
    if(status) {
      if(status === 'exited') {
        await docker.container.start()
      }
      if(!table[vhost]) {
        const inspect = await docker.container.inspect()
        const { IPAddress } = inspect.NetworkSettings.Networks.bridge
        const { NetworkSettings: { Ports }} = inspect
        // TODO: ensure requiring port
        const port = Object.keys(Ports).map(p => parseInt(p))[0]
        table[vhost] = `http://${IPAddress}:${port}`
      }
      const target = table[vhost]
      req.headers.host = target
      try {
        await new Promise((resolve, reject) => {
          proxy.web(req, res, { target }, e => e ? reject(e) :resolve())
        })
      } catch(e) {
        ctx.body = await docker.container.logs()
      }
    } else {
      // redirect to preview url
      const redirectHost = HOST || 'localhost'
      ctx.redirect(`${ctx.protocol}://${redirectHost}:${PORT}`)
    }
  } else {
    await next()
  }
}
{'fraction_non_alphanumeric': 0.15798263193052772, 'fraction_numerical': 0.004008016032064128, 'mean_word_length': 2.287596048298573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1114860', 'n_tokens_mistral': 940, 'n_tokens_neox': 921, 'n_words': 226}