const server = require('server')
const pkg = require('./package.json')

const { get } = server.router
const { render, status } = server.reply

const opts = {
  port: 3000,
  public: './docz-deploy',
  views: './docz-deploy',
  env: 'production'
}

const isFileRoute = (url = '') => ['.js', '.css', '.txt'].some((ext) => url.includes(ext))
const routeCheck = (ctx) => (isFileRoute(ctx.url) ? status(404).send('Not found') : render('index.html'))

const getDate = () => {
  const d = new Date()
  const YYYY = d.getFullYear()
  const MM = `${d.getMonth()}`.padStart(2, '0')
  const DD = `${d.getDate()}`.padStart(2, '0')
  const hh = d.getHours()
  const mm = d.getMinutes()
  const ss = d.getSeconds()
  const SSS = `${d.getMilliseconds()}`.padStart(2, '0')

  return `${YYYY}-${MM}-${DD} ${hh}:${mm}:${ss}.${SSS}`
}

server(opts, [get(routeCheck)]).then((ctx) => {
  console.log(`${getDate()} [INFO] Started ${pkg.name} v${pkg.version} application on ${ctx.options.env}`)
})
