import path from 'path'
import Parent from '../base/serve.mjs'
import fs from 'fs/promises'
import config from '../base/config.mjs'

export default class ServeHandler extends Parent {
  loadTemplate(indexFile) {
    this.template = indexFile.toString()
  }

  async serveIndex(ctx) {
    if (config.get('NODE_ENV') === 'development') {
      let indexFile = await fs.readFile(path.join(this.root, 'index.html'))
      this.loadTemplate(indexFile)
    }

    ctx.body = this.template
    ctx.type = 'text/html; charset=utf-8'
  }
}
